Author: echatellier Date: 2011-06-06 18:46:07 +0200 (Mon, 06 Jun 2011) New Revision: 468 Url: http://nuiton.org/repositories/revision/sandbox/468 Log: Initial commit. Added: bobobrowselucenefacets/pom.xml bobobrowselucenefacets/src/ bobobrowselucenefacets/src/main/ bobobrowselucenefacets/src/main/java/ bobobrowselucenefacets/src/main/java/org/ bobobrowselucenefacets/src/main/java/org/bobo/ bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneFacets.java Modified: bobobrowselucenefacets/ Property changes on: bobobrowselucenefacets ___________________________________________________________________ Added: svn:ignore + .settings target .classpath .project Added: bobobrowselucenefacets/pom.xml =================================================================== --- bobobrowselucenefacets/pom.xml (rev 0) +++ bobobrowselucenefacets/pom.xml 2011-06-06 16:46:07 UTC (rev 468) @@ -0,0 +1,19 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <groupId>TestBobo</groupId> + <artifactId>TestBobo</artifactId> + <version>0.0.1-SNAPSHOT</version> + + <dependencies> + <dependency> + <groupId>org.apache.lucene</groupId> + <artifactId>lucene-core</artifactId> + <version>3.2.0</version> + </dependency> + <dependency> + <groupId>com.browseengine</groupId> + <artifactId>bobo-browse</artifactId> + <version>2.5.0</version> + </dependency> + </dependencies> +</project> \ No newline at end of file Added: bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneFacets.java =================================================================== --- bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneFacets.java (rev 0) +++ bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneFacets.java 2011-06-06 16:46:07 UTC (rev 468) @@ -0,0 +1,183 @@ +/* + * #%L + * + * + * $Id$ + * $HeadURL$ + * %% + * Copyright (C) 2011 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.Arrays; +import java.util.List; +import java.util.Map; + +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.index.CorruptIndexException; +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.index.IndexWriter; +import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.queryParser.ParseException; +import org.apache.lucene.queryParser.QueryParser; +import org.apache.lucene.search.Query; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.FSDirectory; +import org.apache.lucene.store.LockObtainFailedException; +import org.apache.lucene.store.NIOFSDirectory; +import org.apache.lucene.util.Version; + +import com.browseengine.bobo.api.BoboBrowser; +import com.browseengine.bobo.api.BoboIndexReader; +import com.browseengine.bobo.api.Browsable; +import com.browseengine.bobo.api.BrowseException; +import com.browseengine.bobo.api.BrowseFacet; +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; +import com.browseengine.bobo.facets.FacetHandler; +import com.browseengine.bobo.facets.impl.SimpleFacetHandler; + +/** + * TODO add comment here. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ +public class TestLuceneFacets { + + protected static Analyzer analyser = new StandardAnalyzer(Version.LUCENE_32); + + public static void main(String... args) throws IOException, ParseException, BrowseException { + create(); + search(); + } + + /** + * Get lucene index directory. + * + * @return lucene index directory + * @throws IOException + */ + protected static Directory getIndexDirectory() throws IOException { + File path = new File("/tmp/bobo/index"); + Directory directory = new NIOFSDirectory(path); + return directory; + } + + public static void create() throws CorruptIndexException, LockObtainFailedException, IOException { + + IndexWriter writer = new IndexWriter(getIndexDirectory(), new IndexWriterConfig(Version.LUCENE_32, analyser)); + 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)); + doc1.add(new Field("category", "truc", Store.YES, Index.ANALYZED)); + writer.addDocument(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)); + doc2.add(new Field("category", "machin", Store.YES, Index.ANALYZED)); + writer.addDocument(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)); + doc3.add(new Field("category", "bidule", Store.YES, Index.ANALYZED)); + writer.addDocument(doc3); + + + writer.optimize(); + writer.close(); + } + + public static void search() throws IOException, ParseException, BrowseException { + // define facet handlers + + // color facet handler + SimpleFacetHandler colorHandler = new SimpleFacetHandler("color"); + + // category facet handler + SimpleFacetHandler categoryHandler = new SimpleFacetHandler("category"); + + List<FacetHandler<?>> handlerList = Arrays.asList(new FacetHandler<?>[] { + colorHandler, categoryHandler }); + + // 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); + + // add a selection + //BrowseSelection sel = new BrowseSelection("color"); + //sel.addValue("red"); + //br.addSelection(sel); + + // parse a query + QueryParser parser = new QueryParser(Version.LUCENE_32, "content", analyser); + Query q = parser.parse("name:tutu"); + br.setQuery(q); + + // add the facet output specs + FacetSpec colorSpec = new FacetSpec(); + colorSpec.setOrderBy(FacetSortSpec.OrderHitsDesc); + + FacetSpec categorySpec = new FacetSpec(); + categorySpec.setMinHitCount(2); + categorySpec.setOrderBy(FacetSortSpec.OrderHitsDesc); + + br.setFacetSpec("color", colorSpec); + br.setFacetSpec("category", categorySpec); + + // perform browse + Browsable browser = new BoboBrowser(boboReader); + BrowseResult result = browser.browse(br); + + int totalHits = result.getNumHits(); + BrowseHit[] hits = result.getHits(); + + Map<String, FacetAccessible> facetMap = result.getFacetMap(); + + FacetAccessible colorFacets = facetMap.get("color"); + List<BrowseFacet> facetVals = colorFacets.getFacets(); + System.out.println("Facet " + facetVals); + } +} Property changes on: bobobrowselucenefacets/src/main/java/org/bobo/TestLuceneFacets.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision HeadURL