Author: echatellier Date: 2011-09-22 12:23:59 +0200 (Thu, 22 Sep 2011) New Revision: 505 Url: http://nuiton.org/repositories/revision/sandbox/505 Log: Improve example Added: bobobrowselucenefacets/src/main/resources/ bobobrowselucenefacets/src/main/resources/log4j.properties Modified: bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneDateFacets.java bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneFacets.java bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneGroupsFacets.java Modified: bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneDateFacets.java =================================================================== --- bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneDateFacets.java 2011-09-15 08:53:37 UTC (rev 504) +++ bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneDateFacets.java 2011-09-22 10:23:59 UTC (rev 505) @@ -87,7 +87,7 @@ */ public class TestLuceneDateFacets { - protected static Analyzer analyser = new StandardAnalyzer(Version.LUCENE_32); + protected static Analyzer analyser = new StandardAnalyzer(Version.LUCENE_34); public static void main(String... args) throws IOException, ParseException, BrowseException { create(); @@ -108,19 +108,18 @@ public static void create() throws CorruptIndexException, LockObtainFailedException, IOException { - IndexWriter writer = new IndexWriter(getIndexDirectory(), new IndexWriterConfig(Version.LUCENE_33, analyser)); + IndexWriter writer = new IndexWriter(getIndexDirectory(), new IndexWriterConfig(Version.LUCENE_34, analyser)); writer.deleteAll(); // get dates Calendar nowCalendar = Calendar.getInstance(); Date now = nowCalendar.getTime(); - + nowCalendar.add(Calendar.DAY_OF_YEAR, 1); Date nowplus1 = nowCalendar.getTime(); - + nowCalendar.add(Calendar.DAY_OF_YEAR, 7); Date nowplus7 = nowCalendar.getTime(); - Document doc1 = new Document(); doc1.add(new Field("id", "1", Store.YES, Index.ANALYZED)); @@ -192,7 +191,7 @@ br.addSelection(sel); // parse a query - QueryParser parser = new QueryParser(Version.LUCENE_33, "content", analyser); + QueryParser parser = new QueryParser(Version.LUCENE_34, "content", analyser); Query q = parser.parse("*:*"); br.setQuery(q); Modified: bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneFacets.java =================================================================== --- bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneFacets.java 2011-09-15 08:53:37 UTC (rev 504) +++ bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneFacets.java 2011-09-22 10:23:59 UTC (rev 505) @@ -75,7 +75,7 @@ */ public class TestLuceneFacets { - protected static Analyzer analyser = new StandardAnalyzer(Version.LUCENE_32); + protected static Analyzer analyser = new StandardAnalyzer(Version.LUCENE_34); public static void main(String... args) throws IOException, ParseException, BrowseException { create(); @@ -96,7 +96,7 @@ public static void create() throws CorruptIndexException, LockObtainFailedException, IOException { - IndexWriter writer = new IndexWriter(getIndexDirectory(), new IndexWriterConfig(Version.LUCENE_33, analyser)); + IndexWriter writer = new IndexWriter(getIndexDirectory(), new IndexWriterConfig(Version.LUCENE_34, analyser)); writer.deleteAll(); Document doc1 = new Document(); @@ -151,7 +151,7 @@ //br.addSelection(sel); // parse a query - QueryParser parser = new QueryParser(Version.LUCENE_33, "content", analyser); + QueryParser parser = new QueryParser(Version.LUCENE_34, "content", analyser); Query q = parser.parse("name:tutu"); br.setQuery(q); Modified: bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneGroupsFacets.java =================================================================== --- bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneGroupsFacets.java 2011-09-15 08:53:37 UTC (rev 504) +++ bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneGroupsFacets.java 2011-09-22 10:23:59 UTC (rev 505) @@ -43,7 +43,10 @@ import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; +import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; +import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.store.NIOFSDirectory; @@ -57,6 +60,7 @@ import com.browseengine.bobo.api.BrowseHit; import com.browseengine.bobo.api.BrowseRequest; import com.browseengine.bobo.api.BrowseResult; +import com.browseengine.bobo.api.BrowseSelection; import com.browseengine.bobo.api.FacetAccessible; import com.browseengine.bobo.api.FacetSpec; import com.browseengine.bobo.api.FacetSpec.FacetSortSpec; @@ -67,7 +71,7 @@ * Test les facets sur les range le collecteurs basé sur les groupes, en lien * avec bobo. * - * Based on http://lucene.apache.org/java/3_3_0/api/contrib-grouping/org/apache/lucene/s... + * Based on http://lucene.apache.org/java/3_4_0/api/contrib-grouping/org/apache/lucene/s... * * @author chatellier * @version $Revision$ @@ -77,13 +81,15 @@ */ public class TestLuceneGroupsFacets { - protected static Analyzer analyser = new StandardAnalyzer(Version.LUCENE_33); + protected static Analyzer analyser = new StandardAnalyzer(Version.LUCENE_34); public static void main(String... args) throws IOException, ParseException, BrowseException { create(); - search(); + //searchLucene(); + //searchFacet(); + searchGroup(); } - + /** * Get lucene index directory. * @@ -98,63 +104,122 @@ public static void create() throws CorruptIndexException, LockObtainFailedException, IOException { - IndexWriter writer = new IndexWriter(getIndexDirectory(), new IndexWriterConfig(Version.LUCENE_33, analyser)); + IndexWriter writer = new IndexWriter(getIndexDirectory(), new IndexWriterConfig(Version.LUCENE_34, analyser)); writer.deleteAll(); Document doc1 = new Document(); doc1.add(new Field("id", "1", Store.YES, Index.ANALYZED)); - doc1.add(new Field("subject", "s1", Store.YES, Index.ANALYZED)); - doc1.add(new Field("title", "serie : bing bang theory", Store.YES, Index.ANALYZED)); - doc1.add(new Field("color", "red", Store.YES, Index.ANALYZED)); + doc1.add(new Field("brand", "alpha romeo", Store.YES, Index.ANALYZED)); + doc1.add(new Field("model", "mito", Store.YES, Index.ANALYZED)); + doc1.add(new Field("color", "rouge", Store.YES, Index.ANALYZED)); writer.addDocument(doc1); - + Document doc2 = new Document(); doc2.add(new Field("id", "2", Store.YES, Index.ANALYZED)); - doc2.add(new Field("subject", "s1", Store.YES, Index.ANALYZED)); - doc2.add(new Field("title", "serie : dr house ", Store.YES, Index.ANALYZED)); - doc2.add(new Field("color", "blue", Store.YES, Index.ANALYZED)); + doc2.add(new Field("brand", "renault", Store.YES, Index.ANALYZED)); + doc2.add(new Field("model", "clio 2", Store.YES, Index.ANALYZED)); + doc2.add(new Field("color", "gris", Store.YES, Index.ANALYZED)); + doc2.setBoost(2f); // c'est pas important ca :D writer.addDocument(doc2); - + Document doc3 = new Document(); doc3.add(new Field("id", "3", Store.YES, Index.ANALYZED)); - doc3.add(new Field("subject", "s1", Store.YES, Index.ANALYZED)); - doc3.add(new Field("title", "serie : dexter", Store.YES, Index.ANALYZED)); - doc3.add(new Field("color", "red", Store.YES, Index.ANALYZED)); + doc3.add(new Field("brand", "renault", Store.YES, Index.ANALYZED)); + doc3.add(new Field("model", "scenic", Store.YES, Index.ANALYZED)); + doc3.add(new Field("color", "gris", Store.YES, Index.ANALYZED)); writer.addDocument(doc3); - + Document doc4 = new Document(); doc4.add(new Field("id", "4", Store.YES, Index.ANALYZED)); - doc4.add(new Field("subject", "s2", Store.YES, Index.ANALYZED)); - doc4.add(new Field("title", "serie : true blood", Store.YES, Index.ANALYZED)); - doc4.add(new Field("color", "blue", Store.YES, Index.ANALYZED)); + doc4.add(new Field("brand", "renault", Store.YES, Index.ANALYZED)); + doc4.add(new Field("model", "clio 2", Store.YES, Index.ANALYZED)); + doc4.add(new Field("color", "blanc", Store.YES, Index.ANALYZED)); writer.addDocument(doc4); - + Document doc5 = new Document(); doc5.add(new Field("id", "5", Store.YES, Index.ANALYZED)); - doc5.add(new Field("subject", "s2", Store.YES, Index.ANALYZED)); - doc5.add(new Field("title", "film : independance day", Store.YES, Index.ANALYZED)); - doc5.add(new Field("color", "red", Store.YES, Index.ANALYZED)); + doc5.add(new Field("brand", "saab", Store.YES, Index.ANALYZED)); + doc5.add(new Field("model", "93", Store.YES, Index.ANALYZED)); + doc5.add(new Field("color", "noir", Store.YES, Index.ANALYZED)); writer.addDocument(doc5); + + Document doc6 = new Document(); + doc6.add(new Field("id", "6", Store.YES, Index.ANALYZED)); + doc6.add(new Field("brand", "citroen", Store.YES, Index.ANALYZED)); + doc6.add(new Field("model", "C4", Store.YES, Index.ANALYZED)); + doc6.add(new Field("color", "gris", Store.YES, Index.ANALYZED)); + writer.addDocument(doc6); + + Document doc7 = new Document(); + doc7.add(new Field("id", "7", Store.YES, Index.ANALYZED)); + doc7.add(new Field("brand", "fiat", Store.YES, Index.ANALYZED)); + doc7.add(new Field("model", "panda", Store.YES, Index.ANALYZED)); + doc7.add(new Field("color", "gris", Store.YES, Index.ANALYZED)); + writer.addDocument(doc7); + Document doc8 = new Document(); + doc8.add(new Field("id", "8", Store.YES, Index.ANALYZED)); + doc8.add(new Field("brand", "citroen", Store.YES, Index.ANALYZED)); + doc8.add(new Field("model", "AX", Store.YES, Index.ANALYZED)); + doc8.add(new Field("color", "gris", Store.YES, Index.ANALYZED)); + writer.addDocument(doc8); + Document doc9 = new Document(); + doc9.add(new Field("id", "9", Store.YES, Index.ANALYZED)); + doc9.add(new Field("brand", "renault", Store.YES, Index.ANALYZED)); + doc9.add(new Field("model", "clio 3 break", Store.YES, Index.ANALYZED)); + doc9.add(new Field("color", "gris", Store.YES, Index.ANALYZED)); + writer.addDocument(doc9); + + Document doc10 = new Document(); + doc10.add(new Field("id", "10", Store.YES, Index.ANALYZED)); + doc10.add(new Field("brand", "peugeot", Store.YES, Index.ANALYZED)); + doc10.add(new Field("model", "307", Store.YES, Index.ANALYZED)); + doc10.add(new Field("color", "noir", Store.YES, Index.ANALYZED)); + writer.addDocument(doc10); + + Document doc11 = new Document(); + doc11.add(new Field("id", "11", Store.YES, Index.ANALYZED)); + doc11.add(new Field("brand", "fiat", Store.YES, Index.ANALYZED)); + 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 search() throws IOException, ParseException, BrowseException { + public static void searchLucene() throws IOException, ParseException, BrowseException { - // subject facet handler - /*LinkedHashSet<String> subjectFields = new LinkedHashSet<String>(); - subjectFields.add("subject"); - SimpleGroupbyFacetHandler groupByFacet = new SimpleGroupbyFacetHandler("groupby", subjectFields);*/ + // opening a lucene index + IndexSearcher searcher = new IndexSearcher(getIndexDirectory(), true); + + // parse a query + QueryParser parser = new QueryParser(Version.LUCENE_34, "text", analyser); + Query q = parser.parse("*:*"); + + // perform + TopDocs results = searcher.search(q, 10); + + // display results + System.out.println("Nombre total de resultat de la requette : " + results.totalHits); + for (ScoreDoc scoreDoc : results.scoreDocs) { + int docId = scoreDoc.doc; + Document doc = searcher.doc(docId); + System.out.println("Resultat : " + doc.get("brand") + " " + doc.get("model") + " " + doc.get("color")); + } + + searcher.close(); + } + + public static void searchFacet() throws IOException, ParseException, BrowseException { + + // liste des champs facetisés SimpleFacetHandler colorFacet = new SimpleFacetHandler("color"); - SimpleFacetHandler subjectFacet = new SimpleFacetHandler("subject"); + List<FacetHandler<?>> handlerList = Arrays.asList(new FacetHandler<?>[] { colorFacet }); - List<FacetHandler<?>> handlerList = Arrays.asList(new FacetHandler<?>[] { colorFacet, subjectFacet }); - // opening a lucene index IndexReader reader = IndexReader.open(getIndexDirectory(), true); - // decorate it with a bobo index reader BoboIndexReader boboReader = BoboIndexReader.getInstance(reader, handlerList); @@ -162,60 +227,110 @@ BrowseRequest br = new BrowseRequest(); br.setCount(10); br.setOffset(0); - br.setGroupBy("subject"); // parse a query - QueryParser parser = new QueryParser(Version.LUCENE_33, "text", analyser); - //Query q = parser.parse("*:*"); - Query q = parser.parse("title:serie"); + QueryParser parser = new QueryParser(Version.LUCENE_34, "text", analyser); + Query q = parser.parse("*:*"); br.setQuery(q); // add the facet output specs FacetSpec dateSpec = new FacetSpec(); dateSpec.setOrderBy(FacetSortSpec.OrderHitsDesc); br.setFacetSpec("color", dateSpec); - - //FacetSpec groupBySpec = new FacetSpec(); - //subjectSpec.setOrderBy(FacetSortSpec.OrderHitsDesc); - //groupBySpec.setExpandSelection(true); - //subjectSpec.setMaxCount(1); - //subjectSpec.setMinHitCount(2); - //br.setFacetSpec("groupby", groupBySpec); - - //FacetSpec subject2Spec = new FacetSpec(); - //subjectSpec.setOrderBy(FacetSortSpec.OrderHitsDesc); - //subjectSpec.setExpandSelection(true); - //br.setFacetSpec("subject", subject2Spec); - /*BrowseSelection sel = new BrowseSelection("groupby"); - sel.addValue("s1"); + /*BrowseSelection sel = new BrowseSelection("color"); + sel.addValue("gris"); br.addSelection(sel);*/ // perform browse Browsable browser = new BoboBrowser(boboReader); BrowseResult result = browser.browse(br); - Map<String, FacetAccessible> facetMap = result.getFacetMap(); int totalHits = result.getNumHits(); - System.out.println("Total hit count : " + totalHits); - System.out.println("Total getNumGroups : " + result.getNumGroups()); + System.out.println("Nombre total de resultat de la requette : " + totalHits); BrowseHit[] hits = result.getHits(); for (BrowseHit hit : hits) { - hit.getDocid(); - System.out.println("Group value : " + hit.getGroupValue() + " : " + hit.getGroupHitsCount()); + int docId = hit.getDocid(); + Document doc = reader.document(docId); + System.out.println("Resultat : " + doc.get("brand") + " " + doc.get("model") + " " + doc.get("color")); } - //FacetAccessible subjectFacets = facetMap.get("subject"); - //System.out.println(subjectFacets); - + Map<String, FacetAccessible> facetMap = result.getFacetMap(); if (facetMap != null) { FacetAccessible dateFacets = facetMap.get("color"); List<BrowseFacet> facetVals = dateFacets.getFacets(); //List<BrowseFacet> facetVals = result.getGroupAccessible().getFacets(); for (BrowseFacet facetVal : facetVals) { - System.out.println("Facet subject " + facetVal.getValue() + "(" + facetVal.getFacetValueHitCount() + ")"); + System.out.println("Facette " + facetVal.getValue() + "(" + facetVal.getFacetValueHitCount() + ")"); } } } + + public static void searchGroup() throws IOException, ParseException, BrowseException { + + // liste des champs facetisés + SimpleFacetHandler colorFacet = new SimpleFacetHandler("color"); + SimpleFacetHandler brandFacet = new SimpleFacetHandler("brand"); + List<FacetHandler<?>> handlerList = Arrays.asList(new FacetHandler<?>[] { colorFacet, brandFacet }); + + // opening a lucene index + IndexReader reader = IndexReader.open(getIndexDirectory(), true); + // decorate it with a bobo index reader + BoboIndexReader boboReader = BoboIndexReader.getInstance(reader, handlerList); + + // creating a browse request + BrowseRequest br = new BrowseRequest(); + br.setCount(10); + br.setOffset(0); + br.setGroupBy("brand"); + + // parse a query + QueryParser parser = new QueryParser(Version.LUCENE_34, "text", analyser); + Query q = parser.parse("*:*"); + br.setQuery(q); + + // add the facet output specs + FacetSpec dateSpec = new FacetSpec(); + dateSpec.setOrderBy(FacetSortSpec.OrderHitsDesc); + br.setFacetSpec("color", dateSpec); + + /*BrowseSelection sel = new BrowseSelection("color"); + sel.addValue("gris"); + br.addSelection(sel);*/ + + // perform browse + Browsable browser = new BoboBrowser(boboReader); + BrowseResult result = browser.browse(br); + + + int totalHits = result.getNumHits(); + int numGroups = result.getNumGroups(); + System.out.println("Nombre total de resultat de la requette : " + totalHits); + System.out.println("Nombre de groupe trouvé : " + numGroups); + + BrowseHit[] hits = result.getHits(); + for (BrowseHit hit : hits) { + int docId = hit.getDocid(); + Document doc = reader.document(docId); + + if (hit.getGroupValue() != null) { + System.out.println("Nom du groupe : " + hit.getGroupValue()); + System.out.println(" meilleur élement : " + doc.get("brand") + " " + doc.get("model") + " " + doc.get("color")); + } + else { + System.out.println("Resultat : " + doc.get("brand") + " " + doc.get("model") + " " + doc.get("color")); + } + } + + /*Map<String, FacetAccessible> facetMap = result.getFacetMap(); + if (facetMap != null) { + FacetAccessible dateFacets = facetMap.get("color"); + List<BrowseFacet> facetVals = dateFacets.getFacets(); + //List<BrowseFacet> facetVals = result.getGroupAccessible().getFacets(); + for (BrowseFacet facetVal : facetVals) { + System.out.println("Facette " + facetVal.getValue() + "(" + facetVal.getFacetValueHitCount() + ")"); + } + }*/ + } } Added: bobobrowselucenefacets/src/main/resources/log4j.properties =================================================================== --- bobobrowselucenefacets/src/main/resources/log4j.properties (rev 0) +++ bobobrowselucenefacets/src/main/resources/log4j.properties 2011-09-22 10:23:59 UTC (rev 505) @@ -0,0 +1,7 @@ +# Global logging configuration +log4j.rootLogger=ERROR, stdout + +# Console output +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c:%L - %m%n