Author: echatellier Date: 2012-05-10 12:00:14 +0200 (Thu, 10 May 2012) New Revision: 540 Url: http://nuiton.org/repositories/revision/sandbox/540 Log: Facetisation en lucene pur Added: bobobrowselucenefacets/src/main/java/org/bobo/TestOnlyLuceneFacets.java Modified: bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneGroupsFacets.java Modified: bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneGroupsFacets.java =================================================================== --- bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneGroupsFacets.java 2012-05-10 09:34:06 UTC (rev 539) +++ bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneGroupsFacets.java 2012-05-10 10:00:14 UTC (rev 540) @@ -184,15 +184,15 @@ doc11.add(new Field("model", "500", Store.YES, Index.ANALYZED)); doc11.add(new Field("color", "blanc", Store.YES, Index.ANALYZED)); writer.addDocument(doc11); - - writer.optimize(); + writer.close(); } public static void searchLucene() throws IOException, ParseException, BrowseException { // opening a lucene index - IndexSearcher searcher = new IndexSearcher(getIndexDirectory(), true); + IndexReader indexReader = IndexReader.open(getIndexDirectory()); + IndexSearcher searcher = new IndexSearcher(indexReader); // parse a query QueryParser parser = new QueryParser(Version.LUCENE_35, "text", analyser); Added: bobobrowselucenefacets/src/main/java/org/bobo/TestOnlyLuceneFacets.java =================================================================== --- bobobrowselucenefacets/src/main/java/org/bobo/TestOnlyLuceneFacets.java (rev 0) +++ bobobrowselucenefacets/src/main/java/org/bobo/TestOnlyLuceneFacets.java 2012-05-10 10:00:14 UTC (rev 540) @@ -0,0 +1,191 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2012 Codelutin, Chatellier Eric + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.bobo; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.standard.StandardAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.document.Field.Index; +import org.apache.lucene.document.Field.Store; +import org.apache.lucene.facet.index.CategoryDocumentBuilder; +import org.apache.lucene.facet.search.DrillDown; +import org.apache.lucene.facet.search.FacetsCollector; +import org.apache.lucene.facet.search.params.CountFacetRequest; +import org.apache.lucene.facet.search.params.FacetSearchParams; +import org.apache.lucene.facet.search.results.FacetResult; +import org.apache.lucene.facet.search.results.FacetResultNode; +import org.apache.lucene.facet.taxonomy.CategoryPath; +import org.apache.lucene.facet.taxonomy.TaxonomyReader; +import org.apache.lucene.facet.taxonomy.TaxonomyWriter; +import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader; +import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter; +import org.apache.lucene.index.CorruptIndexException; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.IndexWriterConfig.OpenMode; +import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.queryParser.QueryParser; +import org.apache.lucene.search.IndexSearcher; +import org.apache.lucene.search.MultiCollector; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.TopScoreDocCollector; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.LockObtainFailedException; +import org.apache.lucene.store.NIOFSDirectory; +import org.apache.lucene.util.Version; + +/** + * See http://lucene.apache.org/core/old_versioned_docs/versions/3_5_0/api/contrib-... + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class TestOnlyLuceneFacets { + protected static Analyzer analyser = new StandardAnalyzer(Version.LUCENE_35); + + public static void main(String... args) throws IOException, ParseException { + create(); + search(); + } + + /** + * Get lucene index directory. + * + * @return lucene index directory + * @throws IOException + */ + protected static Directory getIndexDirectory() throws IOException { + File path = new File("/tmp/lucene/index"); + Directory directory = new NIOFSDirectory(path); + return directory; + } + + /** + * Get lucene taxo directory. + * + * @return lucene index directory + * @throws IOException + */ + protected static Directory getTaxoDirectory() throws IOException { + File path = new File("/tmp/lucene/taxo"); + Directory directory = new NIOFSDirectory(path); + return directory; + } + + public static void create() throws CorruptIndexException, LockObtainFailedException, IOException { + + IndexWriter writer = new IndexWriter(getIndexDirectory(), new IndexWriterConfig(Version.LUCENE_35, analyser)); + TaxonomyWriter taxo = new DirectoryTaxonomyWriter(getTaxoDirectory(), OpenMode.CREATE); + + writer.deleteAll(); + + Document doc1 = new Document(); + doc1.add(new Field("name", "tutu is red", Store.YES, Index.ANALYZED)); + doc1.add(new Field("color", "red", Store.YES, Index.ANALYZED)); //nsa + doc1.add(new Field("category", "truc", Store.NO, Index.ANALYZED)); //nsa + addDocumentInIndex(writer, taxo, doc1); + + Document doc2 = new Document(); + doc2.add(new Field("name", "tutu is blue 2", Store.YES, Index.ANALYZED)); + doc2.add(new Field("color", "blue", Store.YES, Index.ANALYZED)); //nsa + doc2.add(new Field("category", "machin", Store.NO, Index.ANALYZED)); //nsa + addDocumentInIndex(writer, taxo, doc2); + + Document doc3 = new Document(); + doc3.add(new Field("name", "tutu is blue", Store.YES, Index.ANALYZED)); + doc3.add(new Field("color", "blue", Store.YES, Index.ANALYZED)); //nsa + doc3.add(new Field("category", "bidule", Store.NO, Index.ANALYZED)); //nsa + addDocumentInIndex(writer, taxo, doc3); + + writer.close(); + taxo.close(); + } + + protected static void addDocumentInIndex(IndexWriter writer, TaxonomyWriter taxo, Document doc) throws IOException { + + // declare facets + List<CategoryPath> categories = new ArrayList<CategoryPath>(); + categories.add(new CategoryPath("color", doc.get("color"))); + categories.add(new CategoryPath("category", doc.get("category"))); + + // link facet to doc + CategoryDocumentBuilder categoryDocBuilder = new CategoryDocumentBuilder(taxo); + categoryDocBuilder.setCategoryPaths(categories); + categoryDocBuilder.build(doc); + + // add to to index + writer.addDocument(doc); + } + + public static void search() throws IOException, ParseException { + + // opening a lucene index + IndexReader indexReader = IndexReader.open(getIndexDirectory()); + IndexSearcher searcher = new IndexSearcher(indexReader); + TaxonomyReader taxo = new DirectoryTaxonomyReader(getTaxoDirectory()); + + // create query + QueryParser parser = new QueryParser(Version.LUCENE_35, "content", analyser); + Query q = parser.parse("name:tutu"); + + // navigation par facette + //q = DrillDown.query(q, new CategoryPath("color", "red")); + + // declare result facets + FacetSearchParams facetSearchParams = new FacetSearchParams(); + facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("color"), 2)); + facetSearchParams.addFacetRequest(new CountFacetRequest(new CategoryPath("category"), 2)); + + // perform search + TopScoreDocCollector topDocsCollector = TopScoreDocCollector.create(10, true); + FacetsCollector facetsCollector = new FacetsCollector(facetSearchParams, indexReader, taxo); + searcher.search(q, MultiCollector.wrap(topDocsCollector, facetsCollector)); + List<FacetResult> res = facetsCollector.getFacetResults(); + + for (FacetResult facetResult : res) { + System.out.println("Facet : " + facetResult.getFacetResultNode().getLabel(taxo) + + " (total " + facetResult.getFacetResultNode().getValue() + ")"); + for (FacetResultNode subNode : facetResult.getFacetResultNode().getSubResults()) { + System.out.println(" - " + subNode.getLabel(taxo) + " : " + subNode.getValue()); + } + } + + taxo.close(); + searcher.close(); + indexReader.close(); + } + +} Property changes on: bobobrowselucenefacets/src/main/java/org/bobo/TestOnlyLuceneFacets.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL