Lucene 4.3-facet Demo

Source: Internet
Author: User

 PackageCom.fox.facet;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;ImportOrg.apache.lucene.analysis.core.WhitespaceAnalyzer;Importorg.apache.lucene.document.Document;ImportOrg.apache.lucene.facet.index.FacetFields;ImportOrg.apache.lucene.facet.params.FacetSearchParams;Importorg.apache.lucene.facet.search.CountFacetRequest;ImportOrg.apache.lucene.facet.search.DrillDownQuery;ImportOrg.apache.lucene.facet.search.FacetResult;ImportOrg.apache.lucene.facet.search.FacetsCollector;ImportOrg.apache.lucene.facet.taxonomy.CategoryPath;ImportOrg.apache.lucene.facet.taxonomy.TaxonomyReader;ImportOrg.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;ImportOrg.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;ImportOrg.apache.lucene.index.DirectoryReader;ImportOrg.apache.lucene.index.IndexWriter;ImportOrg.apache.lucene.index.IndexWriterConfig;ImportOrg.apache.lucene.search.IndexSearcher;ImportOrg.apache.lucene.search.MatchAllDocsQuery;Importorg.apache.lucene.store.Directory;Importorg.apache.lucene.store.RAMDirectory;Importorg.apache.lucene.util.Version;/** Licensed to the Apache software Foundation (ASF) under one or more * Contributor license agreements. See the NOTICE file distributed with * This work for additional information regarding copyright ownership. * The ASF licenses this file to you under the Apache License, Version 2.0 * (the "License");  You are not a use of this file except in compliance with * the License. Obtain a copy of the License at * *http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable or agreed to writing, software * Distributed under the License is distribute D on ' As is ' BASIS, * without warranties or CONDITIONS of any KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//**shows simple usage of faceted indexing and search.*/ Public classSimplefacetsexample {Private FinalDirectory Indexdir =Newramdirectory (); Private FinalDirectory Taxodir =Newramdirectory (); /**Empty Constructor*/     Publicsimplefacetsexample () {}Private voidAdd (IndexWriter indexwriter, Facetfields facetfields, String ... categorypaths)throwsIOException {Document doc=NewDocument (); List<CategoryPath> paths =NewArraylist<categorypath>();  for(String categorypath:categorypaths) {Paths.add (NewCategorypath (Categorypath, '/'));        } facetfields.addfields (doc, paths);    Indexwriter.adddocument (DOC); }    /**Build The example index.*/    Private voidIndex ()throwsIOException {indexwriter indexwriter=NewIndexWriter (Indexdir,NewIndexwriterconfig (version.lucene_43,NewWhitespaceanalyzer (version.lucene_43)); //writes Facet ords to a separate directory from the main indexDirectorytaxonomywriter Taxowriter =NewDirectorytaxonomywriter (Taxodir); //Reused across documents, to add the necessary facets fieldsFacetfields Facetfields =NewFacetfields (Taxowriter); Add (IndexWriter, Facetfields,"Author/bob", "Publish DATE/2010/10/15"); Add (IndexWriter, Facetfields,"Author/lisa", "Publish date/2010/10/20"); Add (IndexWriter, Facetfields,"Author/lisa", "Publish DATE/2012/1/1"); Add (IndexWriter, Facetfields,"Author/susan", "Publish DATE/2012/1/7"); Add (IndexWriter, Facetfields,"Author/frank", "Publish DATE/1999/5/5");        Indexwriter.close ();    Taxowriter.close (); }    /**User runs a query and counts facets.*/    PrivateList<facetresult> Search ()throwsIOException {directoryreader Indexreader=Directoryreader.open (Indexdir); Indexsearcher Searcher=NewIndexsearcher (Indexreader); Taxonomyreader Taxoreader=NewDirectorytaxonomyreader (Taxodir); //Count both "Publish Date" and "Author" DimensionsFacetsearchparams FSP =NewFacetsearchparams (NewCountfacetrequest (NewCategorypath ("Publish Date"), 10),                NewCountfacetrequest (NewCategorypath ("Author"), 10)); //aggregatses the facet countsFacetscollector FC =facetscollector.create (FSP, Searcher.getindexreader (), taxoreader); //Matchalldocsquery is for "browsing" (counts facets//For all non-deleted docs in the index); Normally//You ' d use a "normal" query, and use the Multicollector to//Wrap collecting the "normal" hits and also facets:Searcher.search (Newmatchalldocsquery (), FC); //Retrieve ResultsList<facetresult> Facetresults =fc.getfacetresults ();        Indexreader.close ();        Taxoreader.close (); returnFacetresults; }    /**User drills down ' Publish date/2010 '.*/    PrivateList<facetresult> DrillDown ()throwsIOException {directoryreader Indexreader=Directoryreader.open (Indexdir); Indexsearcher Searcher=NewIndexsearcher (Indexreader); Taxonomyreader Taxoreader=NewDirectorytaxonomyreader (Taxodir); //Now user drills down on Publish date/2010:Facetsearchparams FSP =NewFacetsearchparams (NewCountfacetrequest (NewCategorypath ("Author"), 10)); Drilldownquery Q=NewDrilldownquery (Fsp.indexingparams,Newmatchalldocsquery ()); Q.add (NewCategorypath ("Publish date/2010", '/')); Facetscollector FC=facetscollector.create (FSP, Searcher.getindexreader (), taxoreader);        Searcher.search (q, FC); //Retrieve ResultsList<facetresult> Facetresults =fc.getfacetresults ();        Indexreader.close ();        Taxoreader.close (); returnFacetresults; }    /**Runs The search example.*/     PublicList<facetresult> Runsearch ()throwsIOException {index (); returnsearch (); }    /**Runs The drill-down example.*/     PublicList<facetresult> Rundrilldown ()throwsIOException {index (); returnDrillDown (); }    /**Runs The search and drill-down examples and prints the results.*/     Public Static voidMain (string[] args)throwsException {System.out.println ("Facet Counting Example:"); System.out.println ("-----------------------"); List<FacetResult> results =Newsimplefacetsexample (). Runsearch ();  for(Facetresult res:results) {System.out.println (res); } System.out.println ("\ n"); System.out.println ("Facet Drill-down Example (Publish date/2010):"); System.out.println ("---------------------------------------------"); Results=Newsimplefacetsexample (). Rundrilldown ();  for(Facetresult res:results) {System.out.println (res); }    }}

Result:

Facet Counting Example:-----------------------request:publish Date nres=10 nlbl=10Num valid descendants (up to specified depth):3Publish Date (0.0) Publish Date/2012 (2.0) Publish Date/2010 (2.0) Publish Date/1999 (1.0) Request:author nres=10 nlbl=10Num valid descendants (up to specified depth):4Author (0.0) Author/lisa (2.0) Author/frank (1.0) Author/susan (1.0) Author/bob (1.0) Facet Drill-down Example (Publish date/2010):---------------------------------------------Request:author nres=10 nlbl=10Num valid descendants (up to specified depth):2Author (0.0) Author/lisa (1.0) Author/bob (1.0)

Lucene 4.3-facet Demo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.