mirror of https://github.com/apache/lucene.git
LUCENE-5339: cutover demo to new APIs
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene5339@1545637 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0255424864
commit
488af6a0ff
|
@ -1,33 +1,5 @@
|
||||||
package org.apache.lucene.demo.facet;
|
package org.apache.lucene.demo.facet;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
|
||||||
import org.apache.lucene.document.Document;
|
|
||||||
import org.apache.lucene.facet.associations.AssociationsFacetFields;
|
|
||||||
import org.apache.lucene.facet.associations.CategoryAssociation;
|
|
||||||
import org.apache.lucene.facet.associations.CategoryAssociationsContainer;
|
|
||||||
import org.apache.lucene.facet.associations.CategoryFloatAssociation;
|
|
||||||
import org.apache.lucene.facet.associations.CategoryIntAssociation;
|
|
||||||
import org.apache.lucene.facet.associations.SumFloatAssociationFacetRequest;
|
|
||||||
import org.apache.lucene.facet.associations.SumIntAssociationFacetRequest;
|
|
||||||
import org.apache.lucene.facet.index.FacetFields;
|
|
||||||
import org.apache.lucene.facet.params.FacetSearchParams;
|
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
|
||||||
import org.apache.lucene.facet.search.FacetsCollector;
|
|
||||||
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
|
||||||
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
|
|
||||||
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
|
|
||||||
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
|
|
||||||
import org.apache.lucene.index.DirectoryReader;
|
|
||||||
import org.apache.lucene.index.IndexWriter;
|
|
||||||
import org.apache.lucene.index.IndexWriterConfig;
|
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
|
||||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
|
||||||
import org.apache.lucene.store.Directory;
|
|
||||||
import org.apache.lucene.store.RAMDirectory;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
@ -45,50 +17,36 @@ import org.apache.lucene.store.RAMDirectory;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
||||||
|
import org.apache.lucene.document.Document;
|
||||||
|
import org.apache.lucene.facet.simple.Facets;
|
||||||
|
import org.apache.lucene.facet.simple.FacetsConfig;
|
||||||
|
import org.apache.lucene.facet.simple.FloatAssociationFacetField;
|
||||||
|
import org.apache.lucene.facet.simple.IntAssociationFacetField;
|
||||||
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
|
import org.apache.lucene.facet.simple.SimpleFacetsCollector;
|
||||||
|
import org.apache.lucene.facet.simple.TaxonomyFacetSumFloatAssociations;
|
||||||
|
import org.apache.lucene.facet.simple.TaxonomyFacetSumIntAssociations;
|
||||||
|
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
||||||
|
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.DirectoryReader;
|
||||||
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
import org.apache.lucene.index.IndexWriterConfig;
|
||||||
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
|
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||||
|
import org.apache.lucene.store.Directory;
|
||||||
|
import org.apache.lucene.store.RAMDirectory;
|
||||||
|
|
||||||
/** Shows example usage of category associations. */
|
/** Shows example usage of category associations. */
|
||||||
public class AssociationsFacetsExample {
|
public class AssociationsFacetsExample {
|
||||||
|
|
||||||
/**
|
|
||||||
* Categories per document, {@link #ASSOCIATIONS} hold the association value
|
|
||||||
* for each category.
|
|
||||||
*/
|
|
||||||
public static FacetLabel[][] CATEGORIES = {
|
|
||||||
// Doc #1
|
|
||||||
{ new FacetLabel("tags", "lucene") ,
|
|
||||||
new FacetLabel("genre", "computing")
|
|
||||||
},
|
|
||||||
|
|
||||||
// Doc #2
|
|
||||||
{ new FacetLabel("tags", "lucene"),
|
|
||||||
new FacetLabel("tags", "solr"),
|
|
||||||
new FacetLabel("genre", "computing"),
|
|
||||||
new FacetLabel("genre", "software")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Association values for each category. */
|
|
||||||
public static CategoryAssociation[][] ASSOCIATIONS = {
|
|
||||||
// Doc #1 associations
|
|
||||||
{
|
|
||||||
/* 3 occurrences for tag 'lucene' */
|
|
||||||
new CategoryIntAssociation(3),
|
|
||||||
/* 87% confidence level of genre 'computing' */
|
|
||||||
new CategoryFloatAssociation(0.87f)
|
|
||||||
},
|
|
||||||
|
|
||||||
// Doc #2 associations
|
|
||||||
{
|
|
||||||
/* 1 occurrence for tag 'lucene' */
|
|
||||||
new CategoryIntAssociation(1),
|
|
||||||
/* 2 occurrences for tag 'solr' */
|
|
||||||
new CategoryIntAssociation(2),
|
|
||||||
/* 75% confidence level of genre 'computing' */
|
|
||||||
new CategoryFloatAssociation(0.75f),
|
|
||||||
/* 34% confidence level of genre 'software' */
|
|
||||||
new CategoryFloatAssociation(0.34f),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
private final Directory indexDir = new RAMDirectory();
|
private final Directory indexDir = new RAMDirectory();
|
||||||
private final Directory taxoDir = new RAMDirectory();
|
private final Directory taxoDir = new RAMDirectory();
|
||||||
|
|
||||||
|
@ -97,58 +55,79 @@ public class AssociationsFacetsExample {
|
||||||
|
|
||||||
/** Build the example index. */
|
/** Build the example index. */
|
||||||
private void index() throws IOException {
|
private void index() throws IOException {
|
||||||
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
|
IndexWriterConfig iwc = new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
|
||||||
new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));
|
new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER));
|
||||||
|
IndexWriter indexWriter = new IndexWriter(indexDir, iwc);
|
||||||
|
|
||||||
// Writes facet ords to a separate directory from the main index
|
// Writes facet ords to a separate directory from the main index
|
||||||
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
|
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
|
||||||
|
|
||||||
// Reused across documents, to add the necessary facet fields
|
// Reused across documents, to add the necessary facet fields
|
||||||
FacetFields facetFields = new AssociationsFacetFields(taxoWriter);
|
FacetsConfig config = getConfig(taxoWriter);
|
||||||
|
|
||||||
for (int i = 0; i < CATEGORIES.length; i++) {
|
Document doc = new Document();
|
||||||
Document doc = new Document();
|
// 3 occurrences for tag 'lucene'
|
||||||
CategoryAssociationsContainer associations = new CategoryAssociationsContainer();
|
doc.add(new IntAssociationFacetField(3, "tags", "lucene"));
|
||||||
for (int j = 0; j < CATEGORIES[i].length; j++) {
|
// 87% confidence level of genre 'computing'
|
||||||
associations.setAssociation(CATEGORIES[i][j], ASSOCIATIONS[i][j]);
|
doc.add(new FloatAssociationFacetField(0.87f, "genre", "computing"));
|
||||||
}
|
indexWriter.addDocument(config.build(doc));
|
||||||
facetFields.addFields(doc, associations);
|
|
||||||
indexWriter.addDocument(doc);
|
doc = new Document();
|
||||||
}
|
// 1 occurrence for tag 'lucene'
|
||||||
|
doc.add(new IntAssociationFacetField(1, "tags", "lucene"));
|
||||||
|
// 2 occurrence for tag 'solr'
|
||||||
|
doc.add(new IntAssociationFacetField(2, "tags", "solr"));
|
||||||
|
// 75% confidence level of genre 'computing'
|
||||||
|
doc.add(new FloatAssociationFacetField(0.75f, "genre", "computing"));
|
||||||
|
// 34% confidence level of genre 'software'
|
||||||
|
doc.add(new FloatAssociationFacetField(0.34f, "genre", "software"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
indexWriter.close();
|
indexWriter.close();
|
||||||
taxoWriter.close();
|
taxoWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** It's fine if taxoWriter is null (i.e., at search time) */
|
||||||
|
private FacetsConfig getConfig(TaxonomyWriter taxoWriter) {
|
||||||
|
FacetsConfig config = new FacetsConfig(taxoWriter);
|
||||||
|
config.setMultiValued("tags", true);
|
||||||
|
config.setIndexFieldName("tags", "$tags");
|
||||||
|
config.setMultiValued("genre", true);
|
||||||
|
config.setIndexFieldName("genre", "$genre");
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
/** User runs a query and aggregates facets by summing their association values. */
|
/** User runs a query and aggregates facets by summing their association values. */
|
||||||
private List<FacetResult> sumAssociations() throws IOException {
|
private List<SimpleFacetResult> sumAssociations() throws IOException {
|
||||||
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
||||||
IndexSearcher searcher = new IndexSearcher(indexReader);
|
IndexSearcher searcher = new IndexSearcher(indexReader);
|
||||||
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
||||||
|
FacetsConfig config = getConfig(null);
|
||||||
|
|
||||||
FacetLabel tags = new FacetLabel("tags");
|
SimpleFacetsCollector sfc = new SimpleFacetsCollector();
|
||||||
FacetLabel genre = new FacetLabel("genre");
|
|
||||||
FacetSearchParams fsp = new FacetSearchParams(new SumIntAssociationFacetRequest(tags, 10),
|
|
||||||
new SumFloatAssociationFacetRequest(genre, 10));
|
|
||||||
FacetsCollector fc = FacetsCollector.create(fsp, indexReader, taxoReader);
|
|
||||||
|
|
||||||
// MatchAllDocsQuery is for "browsing" (counts facets
|
// MatchAllDocsQuery is for "browsing" (counts facets
|
||||||
// for all non-deleted docs in the index); normally
|
// for all non-deleted docs in the index); normally
|
||||||
// you'd use a "normal" query, and use MultiCollector to
|
// you'd use a "normal" query, and use MultiCollector to
|
||||||
// wrap collecting the "normal" hits and also facets:
|
// wrap collecting the "normal" hits and also facets:
|
||||||
searcher.search(new MatchAllDocsQuery(), fc);
|
searcher.search(new MatchAllDocsQuery(), sfc);
|
||||||
|
|
||||||
|
Facets tags = new TaxonomyFacetSumIntAssociations("$tags", taxoReader, config, sfc);
|
||||||
|
Facets genre = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, sfc);
|
||||||
|
|
||||||
// Retrieve results
|
// Retrieve results
|
||||||
List<FacetResult> facetResults = fc.getFacetResults();
|
List<SimpleFacetResult> results = new ArrayList<SimpleFacetResult>();
|
||||||
|
results.add(tags.getTopChildren(10, "tags"));
|
||||||
|
results.add(genre.getTopChildren(10, "genre"));
|
||||||
|
|
||||||
indexReader.close();
|
indexReader.close();
|
||||||
taxoReader.close();
|
taxoReader.close();
|
||||||
|
|
||||||
return facetResults;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Runs summing association example. */
|
/** Runs summing association example. */
|
||||||
public List<FacetResult> runSumAssociations() throws IOException {
|
public List<SimpleFacetResult> runSumAssociations() throws IOException {
|
||||||
index();
|
index();
|
||||||
return sumAssociations();
|
return sumAssociations();
|
||||||
}
|
}
|
||||||
|
@ -157,10 +136,8 @@ public class AssociationsFacetsExample {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
System.out.println("Sum associations example:");
|
System.out.println("Sum associations example:");
|
||||||
System.out.println("-------------------------");
|
System.out.println("-------------------------");
|
||||||
List<FacetResult> results = new AssociationsFacetsExample().runSumAssociations();
|
List<SimpleFacetResult> results = new AssociationsFacetsExample().runSumAssociations();
|
||||||
for (FacetResult res : results) {
|
System.out.println("tags: " + results.get(0));
|
||||||
System.out.println(res);
|
System.out.println("genre: " + results.get(1));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,11 +13,12 @@ import org.apache.lucene.document.TextField;
|
||||||
import org.apache.lucene.expressions.Expression;
|
import org.apache.lucene.expressions.Expression;
|
||||||
import org.apache.lucene.expressions.SimpleBindings;
|
import org.apache.lucene.expressions.SimpleBindings;
|
||||||
import org.apache.lucene.expressions.js.JavascriptCompiler;
|
import org.apache.lucene.expressions.js.JavascriptCompiler;
|
||||||
import org.apache.lucene.facet.index.FacetFields;
|
import org.apache.lucene.facet.simple.FacetField;
|
||||||
import org.apache.lucene.facet.params.FacetSearchParams;
|
import org.apache.lucene.facet.simple.Facets;
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
import org.apache.lucene.facet.simple.FacetsConfig;
|
||||||
import org.apache.lucene.facet.search.FacetsCollector;
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
import org.apache.lucene.facet.search.SumValueSourceFacetRequest;
|
import org.apache.lucene.facet.simple.SimpleFacetsCollector;
|
||||||
|
import org.apache.lucene.facet.simple.TaxonomyFacetSumValueSource;
|
||||||
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
||||||
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
|
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
|
||||||
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
|
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
|
||||||
|
@ -57,14 +58,6 @@ public class ExpressionAggregationFacetsExample {
|
||||||
/** Empty constructor */
|
/** Empty constructor */
|
||||||
public ExpressionAggregationFacetsExample() {}
|
public ExpressionAggregationFacetsExample() {}
|
||||||
|
|
||||||
private void add(IndexWriter indexWriter, FacetFields facetFields, String text, String category, long popularity) throws IOException {
|
|
||||||
Document doc = new Document();
|
|
||||||
doc.add(new TextField("c", text, Store.NO));
|
|
||||||
doc.add(new NumericDocValuesField("popularity", popularity));
|
|
||||||
facetFields.addFields(doc, Collections.singletonList(new FacetLabel(category, '/')));
|
|
||||||
indexWriter.addDocument(doc);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Build the example index. */
|
/** Build the example index. */
|
||||||
private void index() throws IOException {
|
private void index() throws IOException {
|
||||||
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
|
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
|
||||||
|
@ -74,20 +67,30 @@ public class ExpressionAggregationFacetsExample {
|
||||||
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
|
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
|
||||||
|
|
||||||
// Reused across documents, to add the necessary facet fields
|
// Reused across documents, to add the necessary facet fields
|
||||||
FacetFields facetFields = new FacetFields(taxoWriter);
|
FacetsConfig config = new FacetsConfig(taxoWriter);
|
||||||
|
|
||||||
add(indexWriter, facetFields, "foo bar", "A/B", 5L);
|
Document doc = new Document();
|
||||||
add(indexWriter, facetFields, "foo foo bar", "A/C", 3L);
|
doc.add(new TextField("c", "foo bar", Store.NO));
|
||||||
|
doc.add(new NumericDocValuesField("popularity", 5L));
|
||||||
|
doc.add(new FacetField("A", "B"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new TextField("c", "foo foo bar", Store.NO));
|
||||||
|
doc.add(new NumericDocValuesField("popularity", 3L));
|
||||||
|
doc.add(new FacetField("A", "C"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
indexWriter.close();
|
indexWriter.close();
|
||||||
taxoWriter.close();
|
taxoWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** User runs a query and aggregates facets. */
|
/** User runs a query and aggregates facets. */
|
||||||
private List<FacetResult> search() throws IOException, ParseException {
|
private SimpleFacetResult search() throws IOException, ParseException {
|
||||||
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
||||||
IndexSearcher searcher = new IndexSearcher(indexReader);
|
IndexSearcher searcher = new IndexSearcher(indexReader);
|
||||||
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
||||||
|
FacetsConfig config = new FacetsConfig();
|
||||||
|
|
||||||
// Aggregate categories by an expression that combines the document's score
|
// Aggregate categories by an expression that combines the document's score
|
||||||
// and its popularity field
|
// and its popularity field
|
||||||
|
@ -96,29 +99,27 @@ public class ExpressionAggregationFacetsExample {
|
||||||
bindings.add(new SortField("_score", SortField.Type.SCORE)); // the score of the document
|
bindings.add(new SortField("_score", SortField.Type.SCORE)); // the score of the document
|
||||||
bindings.add(new SortField("popularity", SortField.Type.LONG)); // the value of the 'popularity' field
|
bindings.add(new SortField("popularity", SortField.Type.LONG)); // the value of the 'popularity' field
|
||||||
|
|
||||||
FacetSearchParams fsp = new FacetSearchParams(
|
|
||||||
new SumValueSourceFacetRequest(new FacetLabel("A"), 10, expr.getValueSource(bindings), true));
|
|
||||||
|
|
||||||
// Aggregates the facet values
|
// Aggregates the facet values
|
||||||
FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
|
SimpleFacetsCollector sfc = new SimpleFacetsCollector(true);
|
||||||
|
|
||||||
// MatchAllDocsQuery is for "browsing" (counts facets
|
// MatchAllDocsQuery is for "browsing" (counts facets
|
||||||
// for all non-deleted docs in the index); normally
|
// for all non-deleted docs in the index); normally
|
||||||
// you'd use a "normal" query, and use MultiCollector to
|
// you'd use a "normal" query, and use MultiCollector to
|
||||||
// wrap collecting the "normal" hits and also facets:
|
// wrap collecting the "normal" hits and also facets:
|
||||||
searcher.search(new MatchAllDocsQuery(), fc);
|
searcher.search(new MatchAllDocsQuery(), sfc);
|
||||||
|
|
||||||
// Retrieve results
|
// Retrieve results
|
||||||
List<FacetResult> facetResults = fc.getFacetResults();
|
Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, sfc, expr.getValueSource(bindings));
|
||||||
|
SimpleFacetResult result = facets.getTopChildren(10, "A");
|
||||||
|
|
||||||
indexReader.close();
|
indexReader.close();
|
||||||
taxoReader.close();
|
taxoReader.close();
|
||||||
|
|
||||||
return facetResults;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Runs the search example. */
|
/** Runs the search example. */
|
||||||
public List<FacetResult> runSearch() throws IOException, ParseException {
|
public SimpleFacetResult runSearch() throws IOException, ParseException {
|
||||||
index();
|
index();
|
||||||
return search();
|
return search();
|
||||||
}
|
}
|
||||||
|
@ -127,10 +128,8 @@ public class ExpressionAggregationFacetsExample {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
System.out.println("Facet counting example:");
|
System.out.println("Facet counting example:");
|
||||||
System.out.println("-----------------------");
|
System.out.println("-----------------------");
|
||||||
List<FacetResult> results = new ExpressionAggregationFacetsExample().runSearch();
|
SimpleFacetResult result = new ExpressionAggregationFacetsExample().runSearch();
|
||||||
for (FacetResult res : results) {
|
System.out.println(result);
|
||||||
System.out.println(res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,5 @@
|
||||||
package org.apache.lucene.demo.facet;
|
package org.apache.lucene.demo.facet;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
|
||||||
import org.apache.lucene.document.Document;
|
|
||||||
import org.apache.lucene.facet.index.FacetFields;
|
|
||||||
import org.apache.lucene.facet.params.CategoryListParams;
|
|
||||||
import org.apache.lucene.facet.params.FacetIndexingParams;
|
|
||||||
import org.apache.lucene.facet.params.FacetSearchParams;
|
|
||||||
import org.apache.lucene.facet.params.PerDimensionIndexingParams;
|
|
||||||
import org.apache.lucene.facet.search.CountFacetRequest;
|
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
|
||||||
import org.apache.lucene.facet.search.FacetsCollector;
|
|
||||||
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
|
||||||
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
|
|
||||||
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
|
|
||||||
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
|
|
||||||
import org.apache.lucene.index.DirectoryReader;
|
|
||||||
import org.apache.lucene.index.IndexWriter;
|
|
||||||
import org.apache.lucene.index.IndexWriterConfig;
|
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
|
||||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
|
||||||
import org.apache.lucene.store.Directory;
|
|
||||||
import org.apache.lucene.store.RAMDirectory;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
@ -45,33 +17,52 @@ import org.apache.lucene.store.RAMDirectory;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** Demonstrates indexing categories into different category lists. */
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
||||||
|
import org.apache.lucene.document.Document;
|
||||||
|
import org.apache.lucene.facet.simple.FacetField;
|
||||||
|
import org.apache.lucene.facet.simple.Facets;
|
||||||
|
import org.apache.lucene.facet.simple.FacetsConfig;
|
||||||
|
import org.apache.lucene.facet.simple.FastTaxonomyFacetCounts;
|
||||||
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
|
import org.apache.lucene.facet.simple.SimpleFacetsCollector;
|
||||||
|
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
||||||
|
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.DirectoryReader;
|
||||||
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
import org.apache.lucene.index.IndexWriterConfig;
|
||||||
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
|
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||||
|
import org.apache.lucene.store.Directory;
|
||||||
|
import org.apache.lucene.store.RAMDirectory;
|
||||||
|
|
||||||
|
/** Demonstrates indexing categories into different indexed fields. */
|
||||||
public class MultiCategoryListsFacetsExample {
|
public class MultiCategoryListsFacetsExample {
|
||||||
|
|
||||||
private final FacetIndexingParams indexingParams;
|
|
||||||
private final Directory indexDir = new RAMDirectory();
|
private final Directory indexDir = new RAMDirectory();
|
||||||
private final Directory taxoDir = new RAMDirectory();
|
private final Directory taxoDir = new RAMDirectory();
|
||||||
|
|
||||||
/** Creates a new instance and populates the catetory list params mapping. */
|
/** Creates a new instance and populates the catetory list params mapping. */
|
||||||
public MultiCategoryListsFacetsExample() {
|
public MultiCategoryListsFacetsExample() {
|
||||||
// index all Author facets in one category list and all Publish Date in another.
|
|
||||||
Map<FacetLabel,CategoryListParams> categoryListParams = new HashMap<FacetLabel,CategoryListParams>();
|
|
||||||
categoryListParams.put(new FacetLabel("Author"), new CategoryListParams("author"));
|
|
||||||
categoryListParams.put(new FacetLabel("Publish Date"), new CategoryListParams("pubdate"));
|
|
||||||
indexingParams = new PerDimensionIndexingParams(categoryListParams);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void add(IndexWriter indexWriter, FacetFields facetFields, String ... categoryPaths) throws IOException {
|
|
||||||
Document doc = new Document();
|
|
||||||
|
|
||||||
List<FacetLabel> paths = new ArrayList<FacetLabel>();
|
|
||||||
for (String categoryPath : categoryPaths) {
|
|
||||||
paths.add(new FacetLabel(categoryPath, '/'));
|
|
||||||
}
|
|
||||||
facetFields.addFields(doc, paths);
|
|
||||||
indexWriter.addDocument(doc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** It's fine if taxoWriter is null (i.e., at search time) */
|
||||||
|
private FacetsConfig getConfig(TaxonomyWriter taxoWriter) {
|
||||||
|
FacetsConfig config = new FacetsConfig(taxoWriter);
|
||||||
|
config.setIndexFieldName("Author", "author");
|
||||||
|
config.setIndexFieldName("Publish Date", "pubdate");
|
||||||
|
config.setHierarchical("Publish Date", true);
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
/** Build the example index. */
|
/** Build the example index. */
|
||||||
private void index() throws IOException {
|
private void index() throws IOException {
|
||||||
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
|
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
|
||||||
|
@ -80,50 +71,70 @@ public class MultiCategoryListsFacetsExample {
|
||||||
// Writes facet ords to a separate directory from the main index
|
// Writes facet ords to a separate directory from the main index
|
||||||
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
|
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
|
||||||
|
|
||||||
// Reused across documents, to add the necessary facet fields
|
FacetsConfig config = getConfig(taxoWriter);
|
||||||
FacetFields facetFields = new FacetFields(taxoWriter, indexingParams);
|
|
||||||
|
|
||||||
add(indexWriter, facetFields, "Author/Bob", "Publish Date/2010/10/15");
|
Document doc = new Document();
|
||||||
add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2010/10/20");
|
doc.add(new FacetField("Author", "Bob"));
|
||||||
add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2012/1/1");
|
doc.add(new FacetField("Publish Date", "2010", "10", "15"));
|
||||||
add(indexWriter, facetFields, "Author/Susan", "Publish Date/2012/1/7");
|
indexWriter.addDocument(config.build(doc));
|
||||||
add(indexWriter, facetFields, "Author/Frank", "Publish Date/1999/5/5");
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new FacetField("Author", "Lisa"));
|
||||||
|
doc.add(new FacetField("Publish Date", "2010", "10", "20"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new FacetField("Author", "Lisa"));
|
||||||
|
doc.add(new FacetField("Publish Date", "2012", "1", "1"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new FacetField("Author", "Susan"));
|
||||||
|
doc.add(new FacetField("Publish Date", "2012", "1", "7"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new FacetField("Author", "Frank"));
|
||||||
|
doc.add(new FacetField("Publish Date", "1999", "5", "5"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
indexWriter.close();
|
indexWriter.close();
|
||||||
taxoWriter.close();
|
taxoWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** User runs a query and counts facets. */
|
/** User runs a query and counts facets. */
|
||||||
private List<FacetResult> search() throws IOException {
|
private List<SimpleFacetResult> search() throws IOException {
|
||||||
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
||||||
IndexSearcher searcher = new IndexSearcher(indexReader);
|
IndexSearcher searcher = new IndexSearcher(indexReader);
|
||||||
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
||||||
|
FacetsConfig config = getConfig(null);
|
||||||
|
|
||||||
// Count both "Publish Date" and "Author" dimensions
|
SimpleFacetsCollector sfc = new SimpleFacetsCollector();
|
||||||
FacetSearchParams fsp = new FacetSearchParams(indexingParams,
|
|
||||||
new CountFacetRequest(new FacetLabel("Publish Date"), 10),
|
|
||||||
new CountFacetRequest(new FacetLabel("Author"), 10));
|
|
||||||
|
|
||||||
// Aggregatses the facet counts
|
|
||||||
FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
|
|
||||||
|
|
||||||
// MatchAllDocsQuery is for "browsing" (counts facets
|
// MatchAllDocsQuery is for "browsing" (counts facets
|
||||||
// for all non-deleted docs in the index); normally
|
// for all non-deleted docs in the index); normally
|
||||||
// you'd use a "normal" query, and use MultiCollector to
|
// you'd use a "normal" query, and use MultiCollector to
|
||||||
// wrap collecting the "normal" hits and also facets:
|
// wrap collecting the "normal" hits and also facets:
|
||||||
searcher.search(new MatchAllDocsQuery(), fc);
|
searcher.search(new MatchAllDocsQuery(), sfc);
|
||||||
|
|
||||||
// Retrieve results
|
// Retrieve results
|
||||||
List<FacetResult> facetResults = fc.getFacetResults();
|
List<SimpleFacetResult> results = new ArrayList<SimpleFacetResult>();
|
||||||
|
|
||||||
|
// Count both "Publish Date" and "Author" dimensions
|
||||||
|
Facets author = new FastTaxonomyFacetCounts("author", taxoReader, config, sfc);
|
||||||
|
results.add(author.getTopChildren(10, "Author"));
|
||||||
|
|
||||||
|
Facets pubDate = new FastTaxonomyFacetCounts("pubdate", taxoReader, config, sfc);
|
||||||
|
results.add(pubDate.getTopChildren(10, "Publish Date"));
|
||||||
|
|
||||||
indexReader.close();
|
indexReader.close();
|
||||||
taxoReader.close();
|
taxoReader.close();
|
||||||
|
|
||||||
return facetResults;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Runs the search example. */
|
/** Runs the search example. */
|
||||||
public List<FacetResult> runSearch() throws IOException {
|
public List<SimpleFacetResult> runSearch() throws IOException {
|
||||||
index();
|
index();
|
||||||
return search();
|
return search();
|
||||||
}
|
}
|
||||||
|
@ -132,10 +143,8 @@ public class MultiCategoryListsFacetsExample {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
System.out.println("Facet counting over multiple category lists example:");
|
System.out.println("Facet counting over multiple category lists example:");
|
||||||
System.out.println("-----------------------");
|
System.out.println("-----------------------");
|
||||||
List<FacetResult> results = new MultiCategoryListsFacetsExample().runSearch();
|
List<SimpleFacetResult> results = new MultiCategoryListsFacetsExample().runSearch();
|
||||||
for (FacetResult res : results) {
|
System.out.println("Author: " + results.get(0));
|
||||||
System.out.println(res);
|
System.out.println("Publish Date: " + results.get(1));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,13 +26,13 @@ import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.document.Field;
|
import org.apache.lucene.document.Field;
|
||||||
import org.apache.lucene.document.LongField;
|
import org.apache.lucene.document.LongField;
|
||||||
import org.apache.lucene.document.NumericDocValuesField;
|
import org.apache.lucene.document.NumericDocValuesField;
|
||||||
import org.apache.lucene.facet.params.FacetIndexingParams;
|
import org.apache.lucene.facet.simple.Facets;
|
||||||
import org.apache.lucene.facet.range.LongRange;
|
import org.apache.lucene.facet.simple.FacetsConfig;
|
||||||
import org.apache.lucene.facet.range.RangeAccumulator;
|
import org.apache.lucene.facet.simple.LongRange;
|
||||||
import org.apache.lucene.facet.range.RangeFacetRequest;
|
import org.apache.lucene.facet.simple.RangeFacetCounts;
|
||||||
import org.apache.lucene.facet.search.DrillDownQuery;
|
import org.apache.lucene.facet.simple.SimpleDrillDownQuery;
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
import org.apache.lucene.facet.search.FacetsCollector;
|
import org.apache.lucene.facet.simple.SimpleFacetsCollector;
|
||||||
import org.apache.lucene.index.DirectoryReader;
|
import org.apache.lucene.index.DirectoryReader;
|
||||||
import org.apache.lucene.index.IndexWriter;
|
import org.apache.lucene.index.IndexWriter;
|
||||||
import org.apache.lucene.index.IndexWriterConfig;
|
import org.apache.lucene.index.IndexWriterConfig;
|
||||||
|
@ -44,6 +44,7 @@ import org.apache.lucene.store.Directory;
|
||||||
import org.apache.lucene.store.RAMDirectory;
|
import org.apache.lucene.store.RAMDirectory;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Shows simple usage of dynamic range faceting. */
|
/** Shows simple usage of dynamic range faceting. */
|
||||||
public class RangeFacetsExample implements Closeable {
|
public class RangeFacetsExample implements Closeable {
|
||||||
|
|
||||||
|
@ -51,6 +52,10 @@ public class RangeFacetsExample implements Closeable {
|
||||||
private IndexSearcher searcher;
|
private IndexSearcher searcher;
|
||||||
private final long nowSec = System.currentTimeMillis();
|
private final long nowSec = System.currentTimeMillis();
|
||||||
|
|
||||||
|
final LongRange PAST_HOUR = new LongRange("Past hour", nowSec-3600, true, nowSec, true);
|
||||||
|
final LongRange PAST_SIX_HOURS = new LongRange("Past six hours", nowSec-6*3600, true, nowSec, true);
|
||||||
|
final LongRange PAST_DAY = new LongRange("Past day", nowSec-24*3600, true, nowSec, true);
|
||||||
|
|
||||||
/** Empty constructor */
|
/** Empty constructor */
|
||||||
public RangeFacetsExample() {}
|
public RangeFacetsExample() {}
|
||||||
|
|
||||||
|
@ -76,24 +81,27 @@ public class RangeFacetsExample implements Closeable {
|
||||||
indexWriter.close();
|
indexWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** User runs a query and counts facets. */
|
private FacetsConfig getConfig() {
|
||||||
public List<FacetResult> search() throws IOException {
|
return new FacetsConfig();
|
||||||
|
}
|
||||||
|
|
||||||
RangeFacetRequest<LongRange> rangeFacetRequest = new RangeFacetRequest<LongRange>("timestamp",
|
/** User runs a query and counts facets. */
|
||||||
new LongRange("Past hour", nowSec-3600, true, nowSec, true),
|
public SimpleFacetResult search() throws IOException {
|
||||||
new LongRange("Past six hours", nowSec-6*3600, true, nowSec, true),
|
|
||||||
new LongRange("Past day", nowSec-24*3600, true, nowSec, true));
|
// Aggregates the facet counts
|
||||||
// Aggregatses the facet counts
|
SimpleFacetsCollector sfc = new SimpleFacetsCollector();
|
||||||
FacetsCollector fc = FacetsCollector.create(new RangeAccumulator(rangeFacetRequest));
|
|
||||||
|
|
||||||
// MatchAllDocsQuery is for "browsing" (counts facets
|
// MatchAllDocsQuery is for "browsing" (counts facets
|
||||||
// for all non-deleted docs in the index); normally
|
// for all non-deleted docs in the index); normally
|
||||||
// you'd use a "normal" query, and use MultiCollector to
|
// you'd use a "normal" query, and use MultiCollector to
|
||||||
// wrap collecting the "normal" hits and also facets:
|
// wrap collecting the "normal" hits and also facets:
|
||||||
searcher.search(new MatchAllDocsQuery(), fc);
|
searcher.search(new MatchAllDocsQuery(), sfc);
|
||||||
|
|
||||||
// Retrieve results
|
Facets facets = new RangeFacetCounts("timestamp", sfc,
|
||||||
return fc.getFacetResults();
|
PAST_HOUR,
|
||||||
|
PAST_SIX_HOURS,
|
||||||
|
PAST_DAY);
|
||||||
|
return facets.getTopChildren(10, "timestamp");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** User drills down on the specified range. */
|
/** User drills down on the specified range. */
|
||||||
|
@ -101,7 +109,7 @@ public class RangeFacetsExample implements Closeable {
|
||||||
|
|
||||||
// Passing no baseQuery means we drill down on all
|
// Passing no baseQuery means we drill down on all
|
||||||
// documents ("browse only"):
|
// documents ("browse only"):
|
||||||
DrillDownQuery q = new DrillDownQuery(FacetIndexingParams.DEFAULT);
|
SimpleDrillDownQuery q = new SimpleDrillDownQuery(getConfig());
|
||||||
|
|
||||||
// Use FieldCacheRangeFilter; this will use
|
// Use FieldCacheRangeFilter; this will use
|
||||||
// NumericDocValues:
|
// NumericDocValues:
|
||||||
|
@ -124,15 +132,12 @@ public class RangeFacetsExample implements Closeable {
|
||||||
|
|
||||||
System.out.println("Facet counting example:");
|
System.out.println("Facet counting example:");
|
||||||
System.out.println("-----------------------");
|
System.out.println("-----------------------");
|
||||||
List<FacetResult> results = example.search();
|
System.out.println(example.search());
|
||||||
for (FacetResult res : results) {
|
|
||||||
System.out.println(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("\n");
|
System.out.println("\n");
|
||||||
System.out.println("Facet drill-down example (timestamp/Past six hours):");
|
System.out.println("Facet drill-down example (timestamp/Past six hours):");
|
||||||
System.out.println("---------------------------------------------");
|
System.out.println("---------------------------------------------");
|
||||||
TopDocs hits = example.drillDown((LongRange) ((RangeFacetRequest<LongRange>) results.get(0).getFacetRequest()).ranges[1]);
|
TopDocs hits = example.drillDown(example.PAST_SIX_HOURS);
|
||||||
System.out.println(hits.totalHits + " totalHits");
|
System.out.println(hits.totalHits + " totalHits");
|
||||||
|
|
||||||
example.close();
|
example.close();
|
||||||
|
|
|
@ -1,29 +1,5 @@
|
||||||
package org.apache.lucene.demo.facet;
|
package org.apache.lucene.demo.facet;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
|
||||||
import org.apache.lucene.document.Document;
|
|
||||||
import org.apache.lucene.facet.index.FacetFields;
|
|
||||||
import org.apache.lucene.facet.params.FacetSearchParams;
|
|
||||||
import org.apache.lucene.facet.search.CountFacetRequest;
|
|
||||||
import org.apache.lucene.facet.search.DrillDownQuery;
|
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
|
||||||
import org.apache.lucene.facet.search.FacetsCollector;
|
|
||||||
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
|
||||||
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
|
|
||||||
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader;
|
|
||||||
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
|
|
||||||
import org.apache.lucene.index.DirectoryReader;
|
|
||||||
import org.apache.lucene.index.IndexWriter;
|
|
||||||
import org.apache.lucene.index.IndexWriterConfig;
|
|
||||||
import org.apache.lucene.search.IndexSearcher;
|
|
||||||
import org.apache.lucene.search.MatchAllDocsQuery;
|
|
||||||
import org.apache.lucene.store.Directory;
|
|
||||||
import org.apache.lucene.store.RAMDirectory;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
@ -41,6 +17,32 @@ import org.apache.lucene.store.RAMDirectory;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
||||||
|
import org.apache.lucene.document.Document;
|
||||||
|
import org.apache.lucene.facet.simple.FacetField;
|
||||||
|
import org.apache.lucene.facet.simple.Facets;
|
||||||
|
import org.apache.lucene.facet.simple.FacetsConfig;
|
||||||
|
import org.apache.lucene.facet.simple.FastTaxonomyFacetCounts;
|
||||||
|
import org.apache.lucene.facet.simple.SimpleDrillDownQuery;
|
||||||
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
|
import org.apache.lucene.facet.simple.SimpleFacetsCollector;
|
||||||
|
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
||||||
|
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.DirectoryReader;
|
||||||
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
import org.apache.lucene.index.IndexWriterConfig;
|
||||||
|
import org.apache.lucene.search.IndexSearcher;
|
||||||
|
import org.apache.lucene.search.MatchAllDocsQuery;
|
||||||
|
import org.apache.lucene.store.Directory;
|
||||||
|
import org.apache.lucene.store.RAMDirectory;
|
||||||
|
|
||||||
/** Shows simple usage of faceted indexing and search. */
|
/** Shows simple usage of faceted indexing and search. */
|
||||||
public class SimpleFacetsExample {
|
public class SimpleFacetsExample {
|
||||||
|
|
||||||
|
@ -50,15 +52,11 @@ public class SimpleFacetsExample {
|
||||||
/** Empty constructor */
|
/** Empty constructor */
|
||||||
public SimpleFacetsExample() {}
|
public SimpleFacetsExample() {}
|
||||||
|
|
||||||
private void add(IndexWriter indexWriter, FacetFields facetFields, String ... categoryPaths) throws IOException {
|
/** It's fine if taxoWriter is null (i.e., at search time) */
|
||||||
Document doc = new Document();
|
private FacetsConfig getConfig(TaxonomyWriter taxoWriter) {
|
||||||
|
FacetsConfig config = new FacetsConfig(taxoWriter);
|
||||||
List<FacetLabel> paths = new ArrayList<FacetLabel>();
|
config.setHierarchical("Publish Date", true);
|
||||||
for (String categoryPath : categoryPaths) {
|
return config;
|
||||||
paths.add(new FacetLabel(categoryPath, '/'));
|
|
||||||
}
|
|
||||||
facetFields.addFields(doc, paths);
|
|
||||||
indexWriter.addDocument(doc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Build the example index. */
|
/** Build the example index. */
|
||||||
|
@ -69,81 +67,100 @@ public class SimpleFacetsExample {
|
||||||
// Writes facet ords to a separate directory from the main index
|
// Writes facet ords to a separate directory from the main index
|
||||||
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
|
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
|
||||||
|
|
||||||
// Reused across documents, to add the necessary facet fields
|
FacetsConfig config = getConfig(taxoWriter);
|
||||||
FacetFields facetFields = new FacetFields(taxoWriter);
|
|
||||||
|
|
||||||
add(indexWriter, facetFields, "Author/Bob", "Publish Date/2010/10/15");
|
Document doc = new Document();
|
||||||
add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2010/10/20");
|
doc.add(new FacetField("Author", "Bob"));
|
||||||
add(indexWriter, facetFields, "Author/Lisa", "Publish Date/2012/1/1");
|
doc.add(new FacetField("Publish Date", "2010", "10", "15"));
|
||||||
add(indexWriter, facetFields, "Author/Susan", "Publish Date/2012/1/7");
|
indexWriter.addDocument(config.build(doc));
|
||||||
add(indexWriter, facetFields, "Author/Frank", "Publish Date/1999/5/5");
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new FacetField("Author", "Lisa"));
|
||||||
|
doc.add(new FacetField("Publish Date", "2010", "10", "20"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new FacetField("Author", "Lisa"));
|
||||||
|
doc.add(new FacetField("Publish Date", "2012", "1", "1"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new FacetField("Author", "Susan"));
|
||||||
|
doc.add(new FacetField("Publish Date", "2012", "1", "7"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new FacetField("Author", "Frank"));
|
||||||
|
doc.add(new FacetField("Publish Date", "1999", "5", "5"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
indexWriter.close();
|
indexWriter.close();
|
||||||
taxoWriter.close();
|
taxoWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** User runs a query and counts facets. */
|
/** User runs a query and counts facets. */
|
||||||
private List<FacetResult> search() throws IOException {
|
private List<SimpleFacetResult> search() throws IOException {
|
||||||
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
||||||
IndexSearcher searcher = new IndexSearcher(indexReader);
|
IndexSearcher searcher = new IndexSearcher(indexReader);
|
||||||
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
||||||
|
FacetsConfig config = getConfig(null);
|
||||||
|
|
||||||
// Count both "Publish Date" and "Author" dimensions
|
SimpleFacetsCollector sfc = new SimpleFacetsCollector();
|
||||||
FacetSearchParams fsp = new FacetSearchParams(
|
|
||||||
new CountFacetRequest(new FacetLabel("Publish Date"), 10),
|
|
||||||
new CountFacetRequest(new FacetLabel("Author"), 10));
|
|
||||||
|
|
||||||
// Aggregates the facet counts
|
|
||||||
FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
|
|
||||||
|
|
||||||
// MatchAllDocsQuery is for "browsing" (counts facets
|
// MatchAllDocsQuery is for "browsing" (counts facets
|
||||||
// for all non-deleted docs in the index); normally
|
// for all non-deleted docs in the index); normally
|
||||||
// you'd use a "normal" query, and use MultiCollector to
|
// you'd use a "normal" query, and use MultiCollector to
|
||||||
// wrap collecting the "normal" hits and also facets:
|
// wrap collecting the "normal" hits and also facets:
|
||||||
searcher.search(new MatchAllDocsQuery(), fc);
|
searcher.search(new MatchAllDocsQuery(), sfc);
|
||||||
|
|
||||||
// Retrieve results
|
// Retrieve results
|
||||||
List<FacetResult> facetResults = fc.getFacetResults();
|
List<SimpleFacetResult> results = new ArrayList<SimpleFacetResult>();
|
||||||
|
|
||||||
|
// Count both "Publish Date" and "Author" dimensions
|
||||||
|
Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, sfc);
|
||||||
|
results.add(facets.getTopChildren(10, "Author"));
|
||||||
|
results.add(facets.getTopChildren(10, "Publish Date"));
|
||||||
|
|
||||||
indexReader.close();
|
indexReader.close();
|
||||||
taxoReader.close();
|
taxoReader.close();
|
||||||
|
|
||||||
return facetResults;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** User drills down on 'Publish Date/2010'. */
|
/** User drills down on 'Publish Date/2010'. */
|
||||||
private List<FacetResult> drillDown() throws IOException {
|
private SimpleFacetResult drillDown() throws IOException {
|
||||||
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
||||||
IndexSearcher searcher = new IndexSearcher(indexReader);
|
IndexSearcher searcher = new IndexSearcher(indexReader);
|
||||||
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
|
||||||
|
FacetsConfig config = getConfig(null);
|
||||||
// Now user drills down on Publish Date/2010:
|
|
||||||
FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new FacetLabel("Author"), 10));
|
|
||||||
|
|
||||||
// Passing no baseQuery means we drill down on all
|
// Passing no baseQuery means we drill down on all
|
||||||
// documents ("browse only"):
|
// documents ("browse only"):
|
||||||
DrillDownQuery q = new DrillDownQuery(fsp.indexingParams);
|
SimpleDrillDownQuery q = new SimpleDrillDownQuery(config);
|
||||||
q.add(new FacetLabel("Publish Date/2010", '/'));
|
|
||||||
FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
|
// Now user drills down on Publish Date/2010:
|
||||||
searcher.search(q, fc);
|
q.add("Publish Date", "2010");
|
||||||
|
SimpleFacetsCollector sfc = new SimpleFacetsCollector();
|
||||||
|
searcher.search(q, sfc);
|
||||||
|
|
||||||
// Retrieve results
|
// Retrieve results
|
||||||
List<FacetResult> facetResults = fc.getFacetResults();
|
Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, sfc);
|
||||||
|
SimpleFacetResult result = facets.getTopChildren(10, "Author");
|
||||||
|
|
||||||
indexReader.close();
|
indexReader.close();
|
||||||
taxoReader.close();
|
taxoReader.close();
|
||||||
|
|
||||||
return facetResults;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Runs the search example. */
|
/** Runs the search example. */
|
||||||
public List<FacetResult> runSearch() throws IOException {
|
public List<SimpleFacetResult> runSearch() throws IOException {
|
||||||
index();
|
index();
|
||||||
return search();
|
return search();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Runs the drill-down example. */
|
/** Runs the drill-down example. */
|
||||||
public List<FacetResult> runDrillDown() throws IOException {
|
public SimpleFacetResult runDrillDown() throws IOException {
|
||||||
index();
|
index();
|
||||||
return drillDown();
|
return drillDown();
|
||||||
}
|
}
|
||||||
|
@ -152,18 +169,15 @@ public class SimpleFacetsExample {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
System.out.println("Facet counting example:");
|
System.out.println("Facet counting example:");
|
||||||
System.out.println("-----------------------");
|
System.out.println("-----------------------");
|
||||||
List<FacetResult> results = new SimpleFacetsExample().runSearch();
|
SimpleFacetsExample example = new SimpleFacetsExample();
|
||||||
for (FacetResult res : results) {
|
List<SimpleFacetResult> results = example.runSearch();
|
||||||
System.out.println(res);
|
System.out.println("Author: " + results.get(0));
|
||||||
}
|
System.out.println("Publish Date: " + results.get(1));
|
||||||
|
|
||||||
System.out.println("\n");
|
System.out.println("\n");
|
||||||
System.out.println("Facet drill-down example (Publish Date/2010):");
|
System.out.println("Facet drill-down example (Publish Date/2010):");
|
||||||
System.out.println("---------------------------------------------");
|
System.out.println("---------------------------------------------");
|
||||||
results = new SimpleFacetsExample().runDrillDown();
|
System.out.println("Author: " + example.runDrillDown());
|
||||||
for (FacetResult res : results) {
|
|
||||||
System.out.println(res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,14 +23,14 @@ import java.util.List;
|
||||||
|
|
||||||
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
|
||||||
import org.apache.lucene.document.Document;
|
import org.apache.lucene.document.Document;
|
||||||
import org.apache.lucene.facet.params.FacetSearchParams;
|
import org.apache.lucene.facet.simple.Facets;
|
||||||
import org.apache.lucene.facet.search.CountFacetRequest;
|
import org.apache.lucene.facet.simple.FacetsConfig;
|
||||||
import org.apache.lucene.facet.search.DrillDownQuery;
|
import org.apache.lucene.facet.simple.SimpleDrillDownQuery;
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
import org.apache.lucene.facet.search.FacetsCollector;
|
import org.apache.lucene.facet.simple.SimpleFacetsCollector;
|
||||||
import org.apache.lucene.facet.sortedset.SortedSetDocValuesAccumulator;
|
import org.apache.lucene.facet.simple.SortedSetDocValuesFacetCounts;
|
||||||
import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetFields;
|
import org.apache.lucene.facet.simple.SortedSetDocValuesFacetField;
|
||||||
import org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState;
|
import org.apache.lucene.facet.simple.SortedSetDocValuesReaderState;
|
||||||
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
||||||
import org.apache.lucene.index.DirectoryReader;
|
import org.apache.lucene.index.DirectoryReader;
|
||||||
import org.apache.lucene.index.IndexWriter;
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
@ -51,91 +51,99 @@ public class SimpleSortedSetFacetsExample {
|
||||||
/** Empty constructor */
|
/** Empty constructor */
|
||||||
public SimpleSortedSetFacetsExample() {}
|
public SimpleSortedSetFacetsExample() {}
|
||||||
|
|
||||||
private void add(IndexWriter indexWriter, SortedSetDocValuesFacetFields facetFields, String ... categoryPaths) throws IOException {
|
private FacetsConfig getConfig() {
|
||||||
Document doc = new Document();
|
return new FacetsConfig();
|
||||||
|
|
||||||
List<FacetLabel> paths = new ArrayList<FacetLabel>();
|
|
||||||
for (String categoryPath : categoryPaths) {
|
|
||||||
paths.add(new FacetLabel(categoryPath, '/'));
|
|
||||||
}
|
|
||||||
facetFields.addFields(doc, paths);
|
|
||||||
indexWriter.addDocument(doc);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Build the example index. */
|
/** Build the example index. */
|
||||||
private void index() throws IOException {
|
private void index() throws IOException {
|
||||||
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
|
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
|
||||||
new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));
|
new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));
|
||||||
|
FacetsConfig config = getConfig();
|
||||||
|
Document doc = new Document();
|
||||||
|
doc.add(new SortedSetDocValuesFacetField("Author", "Bob"));
|
||||||
|
doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
// Reused across documents, to add the necessary facet fields
|
doc = new Document();
|
||||||
SortedSetDocValuesFacetFields facetFields = new SortedSetDocValuesFacetFields();
|
doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
|
||||||
|
doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
add(indexWriter, facetFields, "Author/Bob", "Publish Year/2010");
|
doc = new Document();
|
||||||
add(indexWriter, facetFields, "Author/Lisa", "Publish Year/2010");
|
doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
|
||||||
add(indexWriter, facetFields, "Author/Lisa", "Publish Year/2012");
|
doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
|
||||||
add(indexWriter, facetFields, "Author/Susan", "Publish Year/2012");
|
indexWriter.addDocument(config.build(doc));
|
||||||
add(indexWriter, facetFields, "Author/Frank", "Publish Year/1999");
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new SortedSetDocValuesFacetField("Author", "Susan"));
|
||||||
|
doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
|
doc = new Document();
|
||||||
|
doc.add(new SortedSetDocValuesFacetField("Author", "Frank"));
|
||||||
|
doc.add(new SortedSetDocValuesFacetField("Publish Year", "1999"));
|
||||||
|
indexWriter.addDocument(config.build(doc));
|
||||||
|
|
||||||
indexWriter.close();
|
indexWriter.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** User runs a query and counts facets. */
|
/** User runs a query and counts facets. */
|
||||||
private List<FacetResult> search() throws IOException {
|
private List<SimpleFacetResult> search() throws IOException {
|
||||||
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
||||||
IndexSearcher searcher = new IndexSearcher(indexReader);
|
IndexSearcher searcher = new IndexSearcher(indexReader);
|
||||||
SortedSetDocValuesReaderState state = new SortedSetDocValuesReaderState(indexReader);
|
SortedSetDocValuesReaderState state = new SortedSetDocValuesReaderState(indexReader);
|
||||||
|
FacetsConfig config = getConfig();
|
||||||
// Count both "Publish Year" and "Author" dimensions
|
|
||||||
FacetSearchParams fsp = new FacetSearchParams(
|
|
||||||
new CountFacetRequest(new FacetLabel("Publish Year"), 10),
|
|
||||||
new CountFacetRequest(new FacetLabel("Author"), 10));
|
|
||||||
|
|
||||||
// Aggregatses the facet counts
|
// Aggregatses the facet counts
|
||||||
FacetsCollector fc = FacetsCollector.create(new SortedSetDocValuesAccumulator(state, fsp));
|
SimpleFacetsCollector sfc = new SimpleFacetsCollector();
|
||||||
|
|
||||||
// MatchAllDocsQuery is for "browsing" (counts facets
|
// MatchAllDocsQuery is for "browsing" (counts facets
|
||||||
// for all non-deleted docs in the index); normally
|
// for all non-deleted docs in the index); normally
|
||||||
// you'd use a "normal" query, and use MultiCollector to
|
// you'd use a "normal" query, and use MultiCollector to
|
||||||
// wrap collecting the "normal" hits and also facets:
|
// wrap collecting the "normal" hits and also facets:
|
||||||
searcher.search(new MatchAllDocsQuery(), fc);
|
searcher.search(new MatchAllDocsQuery(), sfc);
|
||||||
|
|
||||||
// Retrieve results
|
// Retrieve results
|
||||||
List<FacetResult> facetResults = fc.getFacetResults();
|
Facets facets = new SortedSetDocValuesFacetCounts(state, sfc);
|
||||||
|
|
||||||
|
List<SimpleFacetResult> results = new ArrayList<SimpleFacetResult>();
|
||||||
|
results.add(facets.getTopChildren(10, "Author"));
|
||||||
|
results.add(facets.getTopChildren(10, "Publish Year"));
|
||||||
indexReader.close();
|
indexReader.close();
|
||||||
|
|
||||||
return facetResults;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** User drills down on 'Publish Year/2010'. */
|
/** User drills down on 'Publish Year/2010'. */
|
||||||
private List<FacetResult> drillDown() throws IOException {
|
private SimpleFacetResult drillDown() throws IOException {
|
||||||
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
DirectoryReader indexReader = DirectoryReader.open(indexDir);
|
||||||
IndexSearcher searcher = new IndexSearcher(indexReader);
|
IndexSearcher searcher = new IndexSearcher(indexReader);
|
||||||
SortedSetDocValuesReaderState state = new SortedSetDocValuesReaderState(indexReader);
|
SortedSetDocValuesReaderState state = new SortedSetDocValuesReaderState(indexReader);
|
||||||
|
FacetsConfig config = getConfig();
|
||||||
|
|
||||||
// Now user drills down on Publish Year/2010:
|
// Now user drills down on Publish Year/2010:
|
||||||
FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new FacetLabel("Author"), 10));
|
SimpleDrillDownQuery q = new SimpleDrillDownQuery(config);
|
||||||
DrillDownQuery q = new DrillDownQuery(fsp.indexingParams, new MatchAllDocsQuery());
|
q.add("Publish Year", "2010");
|
||||||
q.add(new FacetLabel("Publish Year/2010", '/'));
|
SimpleFacetsCollector sfc = new SimpleFacetsCollector();
|
||||||
FacetsCollector fc = FacetsCollector.create(new SortedSetDocValuesAccumulator(state, fsp));
|
searcher.search(q, sfc);
|
||||||
searcher.search(q, fc);
|
|
||||||
|
|
||||||
// Retrieve results
|
// Retrieve results
|
||||||
List<FacetResult> facetResults = fc.getFacetResults();
|
Facets facets = new SortedSetDocValuesFacetCounts(state, sfc);
|
||||||
|
SimpleFacetResult result = facets.getTopChildren(10, "Author");
|
||||||
indexReader.close();
|
indexReader.close();
|
||||||
|
|
||||||
return facetResults;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Runs the search example. */
|
/** Runs the search example. */
|
||||||
public List<FacetResult> runSearch() throws IOException {
|
public List<SimpleFacetResult> runSearch() throws IOException {
|
||||||
index();
|
index();
|
||||||
return search();
|
return search();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Runs the drill-down example. */
|
/** Runs the drill-down example. */
|
||||||
public List<FacetResult> runDrillDown() throws IOException {
|
public SimpleFacetResult runDrillDown() throws IOException {
|
||||||
index();
|
index();
|
||||||
return drillDown();
|
return drillDown();
|
||||||
}
|
}
|
||||||
|
@ -144,18 +152,14 @@ public class SimpleSortedSetFacetsExample {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
System.out.println("Facet counting example:");
|
System.out.println("Facet counting example:");
|
||||||
System.out.println("-----------------------");
|
System.out.println("-----------------------");
|
||||||
List<FacetResult> results = new SimpleSortedSetFacetsExample().runSearch();
|
SimpleSortedSetFacetsExample example = new SimpleSortedSetFacetsExample();
|
||||||
for (FacetResult res : results) {
|
List<SimpleFacetResult> results = example.runSearch();
|
||||||
System.out.println(res);
|
System.out.println("Author: " + results.get(0));
|
||||||
}
|
System.out.println("Publish Year: " + results.get(0));
|
||||||
|
|
||||||
System.out.println("\n");
|
System.out.println("\n");
|
||||||
System.out.println("Facet drill-down example (Publish Year/2010):");
|
System.out.println("Facet drill-down example (Publish Year/2010):");
|
||||||
System.out.println("---------------------------------------------");
|
System.out.println("---------------------------------------------");
|
||||||
results = new SimpleSortedSetFacetsExample().runDrillDown();
|
System.out.println("Author: " + example.runDrillDown());
|
||||||
for (FacetResult res : results) {
|
|
||||||
System.out.println(res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,5 @@
|
||||||
package org.apache.lucene.demo.facet;
|
package org.apache.lucene.demo.facet;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
|
||||||
import org.apache.lucene.facet.search.FacetResultNode;
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
@ -24,32 +17,19 @@ import org.junit.Test;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
public class TestAssociationsFacetsExample extends LuceneTestCase {
|
public class TestAssociationsFacetsExample extends LuceneTestCase {
|
||||||
|
|
||||||
private static final double[] EXPECTED_INT_SUM_RESULTS = { 4, 2};
|
|
||||||
private static final double[] EXPECTED_FLOAT_SUM_RESULTS = { 1.62, 0.34};
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExamples() throws Exception {
|
public void testExamples() throws Exception {
|
||||||
List<FacetResult> res = new AssociationsFacetsExample().runSumAssociations();
|
List<SimpleFacetResult> res = new AssociationsFacetsExample().runSumAssociations();
|
||||||
|
|
||||||
assertEquals("Wrong number of results", 2, res.size());
|
assertEquals("Wrong number of results", 2, res.size());
|
||||||
|
assertEquals("value=6 childCount=2\n lucene (4)\n solr (2)\n", res.get(0).toString());
|
||||||
for (FacetResult fres : res) {
|
assertEquals("value=1.96 childCount=2\n computing (1.62)\n software (0.34)\n", res.get(1).toString());
|
||||||
assertEquals("Wrong number of facets", 2, fres.getNumValidDescendants());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Iterable<? extends FacetResultNode> it = res.get(0).getFacetResultNode().subResults;
|
|
||||||
int i = 0;
|
|
||||||
for (FacetResultNode fResNode : it) {
|
|
||||||
assertEquals("Wrong result for facet " + fResNode.label, EXPECTED_INT_SUM_RESULTS[i++], fResNode.value, 1E-5);
|
|
||||||
}
|
|
||||||
|
|
||||||
it = res.get(1).getFacetResultNode().subResults;
|
|
||||||
i = 0;
|
|
||||||
for (FacetResultNode fResNode : it) {
|
|
||||||
assertEquals("Wrong result for facet " + fResNode.label, EXPECTED_FLOAT_SUM_RESULTS[i++], fResNode.value, 1E-5);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,5 @@
|
||||||
package org.apache.lucene.demo.facet;
|
package org.apache.lucene.demo.facet;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
|
||||||
import org.apache.lucene.facet.search.FacetResultNode;
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
@ -25,25 +17,18 @@ import org.junit.Test;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class TestExpressionAggregationFacetsExample extends LuceneTestCase {
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
private static String toSimpleString(FacetResult fr) {
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
StringBuilder sb = new StringBuilder();
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
toSimpleString(fr.getFacetRequest().categoryPath.length, 0, sb, fr.getFacetResultNode(), "");
|
import org.junit.Test;
|
||||||
return sb.toString();
|
|
||||||
}
|
public class TestExpressionAggregationFacetsExample extends LuceneTestCase {
|
||||||
|
|
||||||
private static void toSimpleString(int startLength, int depth, StringBuilder sb, FacetResultNode node, String indent) {
|
|
||||||
sb.append(String.format(Locale.ROOT, "%s%s (%.3f)\n", indent, node.label.components[startLength + depth - 1], node.value));
|
|
||||||
for (FacetResultNode childNode : node.subResults) {
|
|
||||||
toSimpleString(startLength, depth + 1, sb, childNode, indent + " ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
List<FacetResult> facetResults = new ExpressionAggregationFacetsExample().runSearch();
|
SimpleFacetResult result = new ExpressionAggregationFacetsExample().runSearch();
|
||||||
assertEquals("A (0.000)\n B (2.236)\n C (1.732)\n", toSimpleString(facetResults.get(0)));
|
assertEquals("value=3.9681187 childCount=2\n B (2.236068)\n C (1.7320508)\n", result.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,5 @@
|
||||||
package org.apache.lucene.demo.facet;
|
package org.apache.lucene.demo.facet;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.lucene.facet.collections.ObjectToIntMap;
|
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
|
||||||
import org.apache.lucene.facet.search.FacetResultNode;
|
|
||||||
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
@ -26,33 +17,19 @@ import org.junit.Test;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class TestMultiCategoryListsFacetsExample extends LuceneTestCase {
|
import java.util.List;
|
||||||
|
|
||||||
private static final ObjectToIntMap<FacetLabel> expectedCounts = new ObjectToIntMap<FacetLabel>();
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
static {
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
expectedCounts.put(new FacetLabel("Publish Date", "2012"), 2);
|
import org.junit.Test;
|
||||||
expectedCounts.put(new FacetLabel("Publish Date", "2010"), 2);
|
|
||||||
expectedCounts.put(new FacetLabel("Publish Date", "1999"), 1);
|
public class TestMultiCategoryListsFacetsExample extends LuceneTestCase {
|
||||||
expectedCounts.put(new FacetLabel("Author", "Lisa"), 2);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Frank"), 1);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Susan"), 1);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Bob"), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertExpectedCounts(List<FacetResult> facetResults, ObjectToIntMap<FacetLabel> expCounts) {
|
|
||||||
for (FacetResult res : facetResults) {
|
|
||||||
FacetResultNode root = res.getFacetResultNode();
|
|
||||||
for (FacetResultNode node : root.subResults) {
|
|
||||||
assertEquals("incorrect count for " + node.label, expCounts.get(node.label), (int) node.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExample() throws Exception {
|
public void testExample() throws Exception {
|
||||||
List<FacetResult> facetResults = new MultiCategoryListsFacetsExample().runSearch();
|
List<SimpleFacetResult> results = new MultiCategoryListsFacetsExample().runSearch();
|
||||||
assertEquals(2, facetResults.size());
|
assertEquals(2, results.size());
|
||||||
assertExpectedCounts(facetResults, expectedCounts);
|
assertEquals("value=5 childCount=4\n Lisa (2)\n Bob (1)\n Susan (1)\n Frank (1)\n", results.get(0).toString());
|
||||||
|
assertEquals("value=5 childCount=3\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", results.get(1).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,12 +19,7 @@ package org.apache.lucene.demo.facet;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.lucene.facet.collections.ObjectToIntMap;
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
import org.apache.lucene.facet.range.LongRange;
|
|
||||||
import org.apache.lucene.facet.range.RangeFacetRequest;
|
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
|
||||||
import org.apache.lucene.facet.search.FacetResultNode;
|
|
||||||
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
|
||||||
import org.apache.lucene.search.TopDocs;
|
import org.apache.lucene.search.TopDocs;
|
||||||
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
|
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
@ -33,27 +28,12 @@ import org.junit.Test;
|
||||||
@SuppressCodecs("Lucene3x")
|
@SuppressCodecs("Lucene3x")
|
||||||
public class TestRangeFacetsExample extends LuceneTestCase {
|
public class TestRangeFacetsExample extends LuceneTestCase {
|
||||||
|
|
||||||
private static final ObjectToIntMap<FacetLabel> expectedCounts = new ObjectToIntMap<FacetLabel>();
|
|
||||||
static {
|
|
||||||
expectedCounts.put(new FacetLabel("timestamp", "Past hour"), 4);
|
|
||||||
expectedCounts.put(new FacetLabel("timestamp", "Past six hours"), 22);
|
|
||||||
expectedCounts.put(new FacetLabel("timestamp", "Past day"), 87);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertExpectedCounts(FacetResult res, ObjectToIntMap<FacetLabel> expCounts) {
|
|
||||||
FacetResultNode root = res.getFacetResultNode();
|
|
||||||
for (FacetResultNode node : root.subResults) {
|
|
||||||
assertEquals("incorrect count for " + node.label, expCounts.get(node.label), (int) node.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
RangeFacetsExample example = new RangeFacetsExample();
|
RangeFacetsExample example = new RangeFacetsExample();
|
||||||
example.index();
|
example.index();
|
||||||
List<FacetResult> facetResults = example.search();
|
SimpleFacetResult result = example.search();
|
||||||
assertEquals(1, facetResults.size());
|
assertEquals("value=100 childCount=3\n Past hour (4)\n Past six hours (22)\n Past day (87)\n", result.toString());
|
||||||
assertExpectedCounts(facetResults.get(0), expectedCounts);
|
|
||||||
example.close();
|
example.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,8 +42,7 @@ public class TestRangeFacetsExample extends LuceneTestCase {
|
||||||
public void testDrillDown() throws Exception {
|
public void testDrillDown() throws Exception {
|
||||||
RangeFacetsExample example = new RangeFacetsExample();
|
RangeFacetsExample example = new RangeFacetsExample();
|
||||||
example.index();
|
example.index();
|
||||||
List<FacetResult> facetResults = example.search();
|
TopDocs hits = example.drillDown(example.PAST_SIX_HOURS);
|
||||||
TopDocs hits = example.drillDown((LongRange) ((RangeFacetRequest<LongRange>) facetResults.get(0).getFacetRequest()).ranges[1]);
|
|
||||||
assertEquals(22, hits.totalHits);
|
assertEquals(22, hits.totalHits);
|
||||||
example.close();
|
example.close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,5 @@
|
||||||
package org.apache.lucene.demo.facet;
|
package org.apache.lucene.demo.facet;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.lucene.facet.collections.ObjectToIntMap;
|
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
|
||||||
import org.apache.lucene.facet.search.FacetResultNode;
|
|
||||||
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
@ -26,46 +17,26 @@ import org.junit.Test;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
|
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
||||||
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
public class TestSimpleFacetsExample extends LuceneTestCase {
|
public class TestSimpleFacetsExample extends LuceneTestCase {
|
||||||
|
|
||||||
private static final ObjectToIntMap<FacetLabel> expectedCounts = new ObjectToIntMap<FacetLabel>();
|
|
||||||
static {
|
|
||||||
expectedCounts.put(new FacetLabel("Publish Date", "2012"), 2);
|
|
||||||
expectedCounts.put(new FacetLabel("Publish Date", "2010"), 2);
|
|
||||||
expectedCounts.put(new FacetLabel("Publish Date", "1999"), 1);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Lisa"), 2);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Frank"), 1);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Susan"), 1);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Bob"), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final ObjectToIntMap<FacetLabel> expectedCountsDrillDown = new ObjectToIntMap<FacetLabel>();
|
|
||||||
static {
|
|
||||||
expectedCountsDrillDown.put(new FacetLabel("Author", "Lisa"), 1);
|
|
||||||
expectedCountsDrillDown.put(new FacetLabel("Author", "Bob"), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertExpectedCounts(List<FacetResult> facetResults, ObjectToIntMap<FacetLabel> expCounts) {
|
|
||||||
for (FacetResult res : facetResults) {
|
|
||||||
FacetResultNode root = res.getFacetResultNode();
|
|
||||||
for (FacetResultNode node : root.subResults) {
|
|
||||||
assertEquals("incorrect count for " + node.label, expCounts.get(node.label), (int) node.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
List<FacetResult> facetResults = new SimpleFacetsExample().runSearch();
|
List<SimpleFacetResult> results = new SimpleFacetsExample().runSearch();
|
||||||
assertEquals(2, facetResults.size());
|
assertEquals(2, results.size());
|
||||||
assertExpectedCounts(facetResults, expectedCounts);
|
assertEquals("value=5 childCount=4\n Lisa (2)\n Bob (1)\n Susan (1)\n Frank (1)\n", results.get(0).toString());
|
||||||
|
assertEquals("value=5 childCount=3\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", results.get(1).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDrillDown() throws Exception {
|
public void testDrillDown() throws Exception {
|
||||||
List<FacetResult> facetResults = new SimpleFacetsExample().runDrillDown();
|
SimpleFacetResult result = new SimpleFacetsExample().runDrillDown();
|
||||||
assertEquals(1, facetResults.size());
|
assertEquals("value=2 childCount=2\n Bob (1)\n Lisa (1)\n", result.toString());
|
||||||
assertExpectedCounts(facetResults, expectedCountsDrillDown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,5 @@
|
||||||
package org.apache.lucene.demo.facet;
|
package org.apache.lucene.demo.facet;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.apache.lucene.facet.collections.ObjectToIntMap;
|
|
||||||
import org.apache.lucene.facet.search.FacetResult;
|
|
||||||
import org.apache.lucene.facet.search.FacetResultNode;
|
|
||||||
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
|
||||||
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||||
* contributor license agreements. See the NOTICE file distributed with
|
* contributor license agreements. See the NOTICE file distributed with
|
||||||
|
@ -27,48 +17,30 @@ import org.junit.Test;
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.lucene.facet.simple.SimpleFacetResult;
|
||||||
|
import org.apache.lucene.facet.taxonomy.FacetLabel;
|
||||||
|
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
|
||||||
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
||||||
// We require sorted set DVs:
|
// We require sorted set DVs:
|
||||||
@SuppressCodecs({"Lucene40", "Lucene41"})
|
@SuppressCodecs({"Lucene40", "Lucene41"})
|
||||||
public class TestSimpleSortedSetFacetsExample extends LuceneTestCase {
|
public class TestSimpleSortedSetFacetsExample extends LuceneTestCase {
|
||||||
|
|
||||||
private static final ObjectToIntMap<FacetLabel> expectedCounts = new ObjectToIntMap<FacetLabel>();
|
|
||||||
static {
|
|
||||||
expectedCounts.put(new FacetLabel("Publish Year", "2012"), 2);
|
|
||||||
expectedCounts.put(new FacetLabel("Publish Year", "2010"), 2);
|
|
||||||
expectedCounts.put(new FacetLabel("Publish Year", "1999"), 1);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Lisa"), 2);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Frank"), 1);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Susan"), 1);
|
|
||||||
expectedCounts.put(new FacetLabel("Author", "Bob"), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final ObjectToIntMap<FacetLabel> expectedCountsDrillDown = new ObjectToIntMap<FacetLabel>();
|
|
||||||
static {
|
|
||||||
expectedCountsDrillDown.put(new FacetLabel("Author", "Lisa"), 1);
|
|
||||||
expectedCountsDrillDown.put(new FacetLabel("Author", "Bob"), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void assertExpectedCounts(List<FacetResult> facetResults, ObjectToIntMap<FacetLabel> expCounts) {
|
|
||||||
for (FacetResult res : facetResults) {
|
|
||||||
FacetResultNode root = res.getFacetResultNode();
|
|
||||||
for (FacetResultNode node : root.subResults) {
|
|
||||||
assertEquals("incorrect count for " + node.label, expCounts.get(node.label), (int) node.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSimple() throws Exception {
|
public void testSimple() throws Exception {
|
||||||
List<FacetResult> facetResults = new SimpleSortedSetFacetsExample().runSearch();
|
List<SimpleFacetResult> results = new SimpleSortedSetFacetsExample().runSearch();
|
||||||
assertEquals(2, facetResults.size());
|
assertEquals(2, results.size());
|
||||||
assertExpectedCounts(facetResults, expectedCounts);
|
assertEquals("value=5 childCount=4\n Lisa (2)\n Bob (1)\n Frank (1)\n Susan (1)\n", results.get(0).toString());
|
||||||
|
assertEquals("value=5 childCount=3\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", results.get(1).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDrillDown() throws Exception {
|
public void testDrillDown() throws Exception {
|
||||||
List<FacetResult> facetResults = new SimpleSortedSetFacetsExample().runDrillDown();
|
SimpleFacetResult result = new SimpleSortedSetFacetsExample().runDrillDown();
|
||||||
assertEquals(1, facetResults.size());
|
assertEquals("value=2 childCount=2\n Bob (1)\n Lisa (1)\n", result.toString());
|
||||||
assertExpectedCounts(facetResults, expectedCountsDrillDown);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -141,7 +141,7 @@ public class TaxonomyFacetSumFloatAssociations extends TaxonomyFacets {
|
||||||
for(int i=labelValues.length-1;i>=0;i--) {
|
for(int i=labelValues.length-1;i>=0;i--) {
|
||||||
TopOrdAndFloatQueue.OrdAndValue ordAndValue = q.pop();
|
TopOrdAndFloatQueue.OrdAndValue ordAndValue = q.pop();
|
||||||
FacetLabel child = taxoReader.getPath(ordAndValue.ord);
|
FacetLabel child = taxoReader.getPath(ordAndValue.ord);
|
||||||
labelValues[i] = new LabelAndValue(child.components[path.length], ordAndValue.value);
|
labelValues[i] = new LabelAndValue(child.components[cp.length], ordAndValue.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SimpleFacetResult(sumValue, labelValues, childCount);
|
return new SimpleFacetResult(sumValue, labelValues, childCount);
|
||||||
|
|
|
@ -142,7 +142,7 @@ public class TaxonomyFacetSumIntAssociations extends TaxonomyFacets {
|
||||||
for(int i=labelValues.length-1;i>=0;i--) {
|
for(int i=labelValues.length-1;i>=0;i--) {
|
||||||
TopOrdAndIntQueue.OrdAndValue ordAndValue = q.pop();
|
TopOrdAndIntQueue.OrdAndValue ordAndValue = q.pop();
|
||||||
FacetLabel child = taxoReader.getPath(ordAndValue.ord);
|
FacetLabel child = taxoReader.getPath(ordAndValue.ord);
|
||||||
labelValues[i] = new LabelAndValue(child.components[path.length], ordAndValue.value);
|
labelValues[i] = new LabelAndValue(child.components[cp.length], ordAndValue.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SimpleFacetResult(sumValue, labelValues, childCount);
|
return new SimpleFacetResult(sumValue, labelValues, childCount);
|
||||||
|
|
|
@ -107,7 +107,7 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase {
|
||||||
searcher.search(new MatchAllDocsQuery(), fc);
|
searcher.search(new MatchAllDocsQuery(), fc);
|
||||||
|
|
||||||
Facets facets = new TaxonomyFacetSumIntAssociations("$facets.int", taxoReader, config, fc);
|
Facets facets = new TaxonomyFacetSumIntAssociations("$facets.int", taxoReader, config, fc);
|
||||||
|
assertEquals("value=350 childCount=2\n a (200)\n b (150)\n", facets.getTopChildren(10, "int").toString());
|
||||||
assertEquals("Wrong count for category 'a'!", 200, facets.getSpecificValue("int", "a").intValue());
|
assertEquals("Wrong count for category 'a'!", 200, facets.getSpecificValue("int", "a").intValue());
|
||||||
assertEquals("Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue());
|
assertEquals("Wrong count for category 'b'!", 150, facets.getSpecificValue("int", "b").intValue());
|
||||||
}
|
}
|
||||||
|
@ -119,6 +119,7 @@ public class TestTaxonomyFacetAssociations extends FacetTestCase {
|
||||||
searcher.search(new MatchAllDocsQuery(), fc);
|
searcher.search(new MatchAllDocsQuery(), fc);
|
||||||
|
|
||||||
Facets facets = new TaxonomyFacetSumFloatAssociations("$facets.float", taxoReader, config, fc);
|
Facets facets = new TaxonomyFacetSumFloatAssociations("$facets.float", taxoReader, config, fc);
|
||||||
|
assertEquals("value=59.999996 childCount=2\n a (50.0)\n b (9.999995)\n", facets.getTopChildren(10, "float").toString());
|
||||||
assertEquals("Wrong count for category 'a'!", 50f, facets.getSpecificValue("float", "a").floatValue(), 0.00001);
|
assertEquals("Wrong count for category 'a'!", 50f, facets.getSpecificValue("float", "a").floatValue(), 0.00001);
|
||||||
assertEquals("Wrong count for category 'b'!", 10f, facets.getSpecificValue("float", "b").floatValue(), 0.00001);
|
assertEquals("Wrong count for category 'b'!", 10f, facets.getSpecificValue("float", "b").floatValue(), 0.00001);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue