You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by mi...@apache.org on 2014/01/05 22:05:03 UTC
svn commit: r1555629 [2/7] - in /lucene/dev/branches/lucene5376: ./
dev-tools/ dev-tools/maven/solr/contrib/map-reduce/ lucene/
lucene/analysis/ lucene/analysis/common/
lucene/analysis/common/src/java/org/apache/lucene/analysis/ckb/
lucene/analysis/com...
Modified: lucene/dev/branches/lucene5376/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleFacetsExample.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleFacetsExample.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleFacetsExample.java (original)
+++ lucene/dev/branches/lucene5376/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleFacetsExample.java Sun Jan 5 21:04:58 2014
@@ -1,29 +1,5 @@
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.CategoryPath;
-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
* contributor license agreements. See the NOTICE file distributed with
@@ -41,26 +17,42 @@ import org.apache.lucene.store.RAMDirect
* 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.DrillDownQuery;
+import org.apache.lucene.facet.FacetField;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.FacetsConfig;
+import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
+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;
+
/** Shows simple usage of faceted indexing and search. */
public class SimpleFacetsExample {
private final Directory indexDir = new RAMDirectory();
private final Directory taxoDir = new RAMDirectory();
+ private final FacetsConfig config = new FacetsConfig();
/** Empty constructor */
- public SimpleFacetsExample() {}
-
- private void add(IndexWriter indexWriter, FacetFields facetFields, String ... categoryPaths) throws IOException {
- Document doc = new Document();
-
- List<CategoryPath> paths = new ArrayList<CategoryPath>();
- for (String categoryPath : categoryPaths) {
- paths.add(new CategoryPath(categoryPath, '/'));
- }
- facetFields.addFields(doc, paths);
- indexWriter.addDocument(doc);
+ public SimpleFacetsExample() {
+ config.setHierarchical("Publish Date", true);
}
-
+
/** Build the example index. */
private void index() throws IOException {
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
@@ -69,14 +61,30 @@ public class SimpleFacetsExample {
// Writes facet ords to a separate directory from the main index
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
- // Reused across documents, to add the necessary facet fields
- FacetFields facetFields = new FacetFields(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");
+ Document doc = new Document();
+ doc.add(new FacetField("Author", "Bob"));
+ doc.add(new FacetField("Publish Date", "2010", "10", "15"));
+ indexWriter.addDocument(config.build(taxoWriter, doc));
+
+ doc = new Document();
+ doc.add(new FacetField("Author", "Lisa"));
+ doc.add(new FacetField("Publish Date", "2010", "10", "20"));
+ indexWriter.addDocument(config.build(taxoWriter, doc));
+
+ doc = new Document();
+ doc.add(new FacetField("Author", "Lisa"));
+ doc.add(new FacetField("Publish Date", "2012", "1", "1"));
+ indexWriter.addDocument(config.build(taxoWriter, doc));
+
+ doc = new Document();
+ doc.add(new FacetField("Author", "Susan"));
+ doc.add(new FacetField("Publish Date", "2012", "1", "7"));
+ indexWriter.addDocument(config.build(taxoWriter, doc));
+
+ doc = new Document();
+ doc.add(new FacetField("Author", "Frank"));
+ doc.add(new FacetField("Publish Date", "1999", "5", "5"));
+ indexWriter.addDocument(config.build(taxoWriter, doc));
indexWriter.close();
taxoWriter.close();
@@ -88,52 +96,50 @@ public class SimpleFacetsExample {
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
- // Count both "Publish Date" and "Author" dimensions
- FacetSearchParams fsp = new FacetSearchParams(
- new CountFacetRequest(new CategoryPath("Publish Date"), 10),
- new CountFacetRequest(new CategoryPath("Author"), 10));
-
- // Aggregates the facet counts
- FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
+ FacetsCollector fc = new FacetsCollector();
// MatchAllDocsQuery is for "browsing" (counts facets
// for all non-deleted docs in the index); normally
- // you'd use a "normal" query, and use MultiCollector to
- // wrap collecting the "normal" hits and also facets:
- searcher.search(new MatchAllDocsQuery(), fc);
+ // you'd use a "normal" query:
+ FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
// Retrieve results
- List<FacetResult> facetResults = fc.getFacetResults();
+ List<FacetResult> results = new ArrayList<FacetResult>();
+
+ // Count both "Publish Date" and "Author" dimensions
+ Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
+ results.add(facets.getTopChildren(10, "Author"));
+ results.add(facets.getTopChildren(10, "Publish Date"));
indexReader.close();
taxoReader.close();
- return facetResults;
+ return results;
}
/** User drills down on 'Publish Date/2010'. */
- private List<FacetResult> drillDown() throws IOException {
+ private FacetResult drillDown() throws IOException {
DirectoryReader indexReader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
- // Now user drills down on Publish Date/2010:
- FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Author"), 10));
-
// Passing no baseQuery means we drill down on all
// documents ("browse only"):
- DrillDownQuery q = new DrillDownQuery(fsp.indexingParams);
- q.add(new CategoryPath("Publish Date/2010", '/'));
- FacetsCollector fc = FacetsCollector.create(fsp, searcher.getIndexReader(), taxoReader);
- searcher.search(q, fc);
+ DrillDownQuery q = new DrillDownQuery(config);
+
+ // Now user drills down on Publish Date/2010:
+ q.add("Publish Date", "2010");
+ FacetsCollector fc = new FacetsCollector();
+ FacetsCollector.search(searcher, q, 10, fc);
// Retrieve results
- List<FacetResult> facetResults = fc.getFacetResults();
-
+ Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc);
+ FacetResult result = facets.getTopChildren(10, "Author");
+
indexReader.close();
taxoReader.close();
- return facetResults;
+ return result;
}
/** Runs the search example. */
@@ -143,7 +149,7 @@ public class SimpleFacetsExample {
}
/** Runs the drill-down example. */
- public List<FacetResult> runDrillDown() throws IOException {
+ public FacetResult runDrillDown() throws IOException {
index();
return drillDown();
}
@@ -152,18 +158,15 @@ public class SimpleFacetsExample {
public static void main(String[] args) throws Exception {
System.out.println("Facet counting example:");
System.out.println("-----------------------");
- List<FacetResult> results = new SimpleFacetsExample().runSearch();
- for (FacetResult res : results) {
- System.out.println(res);
- }
+ SimpleFacetsExample example = new SimpleFacetsExample();
+ List<FacetResult> results = example.runSearch();
+ System.out.println("Author: " + results.get(0));
+ System.out.println("Publish Date: " + results.get(1));
System.out.println("\n");
System.out.println("Facet drill-down example (Publish Date/2010):");
System.out.println("---------------------------------------------");
- results = new SimpleFacetsExample().runDrillDown();
- for (FacetResult res : results) {
- System.out.println(res);
- }
+ System.out.println("Author: " + example.runDrillDown());
}
}
Modified: lucene/dev/branches/lucene5376/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleSortedSetFacetsExample.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleSortedSetFacetsExample.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleSortedSetFacetsExample.java (original)
+++ lucene/dev/branches/lucene5376/lucene/demo/src/java/org/apache/lucene/demo/facet/SimpleSortedSetFacetsExample.java Sun Jan 5 21:04:58 2014
@@ -23,15 +23,14 @@ import java.util.List;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
-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.sortedset.SortedSetDocValuesAccumulator;
-import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetFields;
+import org.apache.lucene.facet.DrillDownQuery;
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.Facets;
+import org.apache.lucene.facet.FacetsCollector;
+import org.apache.lucene.facet.FacetsConfig;
+import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts;
+import org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField;
import org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState;
-import org.apache.lucene.facet.taxonomy.CategoryPath;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
@@ -41,40 +40,46 @@ import org.apache.lucene.store.Directory
import org.apache.lucene.store.RAMDirectory;
/** Shows simple usage of faceted indexing and search,
- * using {@link SortedSetDocValuesFacetFields} and {@link
- * SortedSetDocValuesAccumulator}. */
+ * using {@link SortedSetDocValuesFacetField} and {@link
+ * SortedSetDocValuesFacetCounts}. */
public class SimpleSortedSetFacetsExample {
private final Directory indexDir = new RAMDirectory();
+ private final FacetsConfig config = new FacetsConfig();
/** Empty constructor */
- public SimpleSortedSetFacetsExample() {}
-
- private void add(IndexWriter indexWriter, SortedSetDocValuesFacetFields facetFields, String ... categoryPaths) throws IOException {
- Document doc = new Document();
-
- List<CategoryPath> paths = new ArrayList<CategoryPath>();
- for (String categoryPath : categoryPaths) {
- paths.add(new CategoryPath(categoryPath, '/'));
- }
- facetFields.addFields(doc, paths);
- indexWriter.addDocument(doc);
+ public SimpleSortedSetFacetsExample() {
}
/** Build the example index. */
private void index() throws IOException {
IndexWriter indexWriter = new IndexWriter(indexDir, new IndexWriterConfig(FacetExamples.EXAMPLES_VER,
new WhitespaceAnalyzer(FacetExamples.EXAMPLES_VER)));
-
- // Reused across documents, to add the necessary facet fields
- SortedSetDocValuesFacetFields facetFields = new SortedSetDocValuesFacetFields();
-
- add(indexWriter, facetFields, "Author/Bob", "Publish Year/2010");
- add(indexWriter, facetFields, "Author/Lisa", "Publish Year/2010");
- add(indexWriter, facetFields, "Author/Lisa", "Publish Year/2012");
- add(indexWriter, facetFields, "Author/Susan", "Publish Year/2012");
- add(indexWriter, facetFields, "Author/Frank", "Publish Year/1999");
+ Document doc = new Document();
+ doc.add(new SortedSetDocValuesFacetField("Author", "Bob"));
+ doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
+ indexWriter.addDocument(config.build(doc));
+
+ doc = new Document();
+ doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
+ doc.add(new SortedSetDocValuesFacetField("Publish Year", "2010"));
+ indexWriter.addDocument(config.build(doc));
+
+ doc = new Document();
+ doc.add(new SortedSetDocValuesFacetField("Author", "Lisa"));
+ doc.add(new SortedSetDocValuesFacetField("Publish Year", "2012"));
+ indexWriter.addDocument(config.build(doc));
+
+ 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();
}
@@ -85,47 +90,43 @@ public class SimpleSortedSetFacetsExampl
IndexSearcher searcher = new IndexSearcher(indexReader);
SortedSetDocValuesReaderState state = new SortedSetDocValuesReaderState(indexReader);
- // Count both "Publish Year" and "Author" dimensions
- FacetSearchParams fsp = new FacetSearchParams(
- new CountFacetRequest(new CategoryPath("Publish Year"), 10),
- new CountFacetRequest(new CategoryPath("Author"), 10));
-
// Aggregatses the facet counts
- FacetsCollector fc = FacetsCollector.create(new SortedSetDocValuesAccumulator(state, fsp));
+ FacetsCollector fc = new FacetsCollector();
// MatchAllDocsQuery is for "browsing" (counts facets
// for all non-deleted docs in the index); normally
- // you'd use a "normal" query, and use MultiCollector to
- // wrap collecting the "normal" hits and also facets:
- searcher.search(new MatchAllDocsQuery(), fc);
+ // you'd use a "normal" query:
+ FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);
// Retrieve results
- List<FacetResult> facetResults = fc.getFacetResults();
-
+ Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
+
+ List<FacetResult> results = new ArrayList<FacetResult>();
+ results.add(facets.getTopChildren(10, "Author"));
+ results.add(facets.getTopChildren(10, "Publish Year"));
indexReader.close();
- return facetResults;
+ return results;
}
/** User drills down on 'Publish Year/2010'. */
- private List<FacetResult> drillDown() throws IOException {
+ private FacetResult drillDown() throws IOException {
DirectoryReader indexReader = DirectoryReader.open(indexDir);
IndexSearcher searcher = new IndexSearcher(indexReader);
SortedSetDocValuesReaderState state = new SortedSetDocValuesReaderState(indexReader);
// Now user drills down on Publish Year/2010:
- FacetSearchParams fsp = new FacetSearchParams(new CountFacetRequest(new CategoryPath("Author"), 10));
- DrillDownQuery q = new DrillDownQuery(fsp.indexingParams, new MatchAllDocsQuery());
- q.add(new CategoryPath("Publish Year/2010", '/'));
- FacetsCollector fc = FacetsCollector.create(new SortedSetDocValuesAccumulator(state, fsp));
- searcher.search(q, fc);
+ DrillDownQuery q = new DrillDownQuery(config);
+ q.add("Publish Year", "2010");
+ FacetsCollector fc = new FacetsCollector();
+ FacetsCollector.search(searcher, q, 10, fc);
// Retrieve results
- List<FacetResult> facetResults = fc.getFacetResults();
-
+ Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
+ FacetResult result = facets.getTopChildren(10, "Author");
indexReader.close();
- return facetResults;
+ return result;
}
/** Runs the search example. */
@@ -135,7 +136,7 @@ public class SimpleSortedSetFacetsExampl
}
/** Runs the drill-down example. */
- public List<FacetResult> runDrillDown() throws IOException {
+ public FacetResult runDrillDown() throws IOException {
index();
return drillDown();
}
@@ -144,18 +145,14 @@ public class SimpleSortedSetFacetsExampl
public static void main(String[] args) throws Exception {
System.out.println("Facet counting example:");
System.out.println("-----------------------");
- List<FacetResult> results = new SimpleSortedSetFacetsExample().runSearch();
- for (FacetResult res : results) {
- System.out.println(res);
- }
+ SimpleSortedSetFacetsExample example = new SimpleSortedSetFacetsExample();
+ List<FacetResult> results = example.runSearch();
+ System.out.println("Author: " + results.get(0));
+ System.out.println("Publish Year: " + results.get(0));
System.out.println("\n");
System.out.println("Facet drill-down example (Publish Year/2010):");
System.out.println("---------------------------------------------");
- results = new SimpleSortedSetFacetsExample().runDrillDown();
- for (FacetResult res : results) {
- System.out.println(res);
- }
+ System.out.println("Author: " + example.runDrillDown());
}
-
}
Modified: lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestAssociationsFacetsExample.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestAssociationsFacetsExample.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestAssociationsFacetsExample.java (original)
+++ lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestAssociationsFacetsExample.java Sun Jan 5 21:04:58 2014
@@ -1,12 +1,5 @@
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
* contributor license agreements. See the NOTICE file distributed with
@@ -24,32 +17,19 @@ import org.junit.Test;
* limitations under the License.
*/
+import java.util.List;
+
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.util.LuceneTestCase;
+import org.junit.Test;
+
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
public void testExamples() throws Exception {
List<FacetResult> res = new AssociationsFacetsExample().runSumAssociations();
-
assertEquals("Wrong number of results", 2, res.size());
-
- for (FacetResult fres : res) {
- 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);
- }
- }
-
+ assertEquals("dim=tags path=[] value=-1 childCount=2\n lucene (4)\n solr (2)\n", res.get(0).toString());
+ assertEquals("dim=genre path=[] value=-1.0 childCount=2\n computing (1.62)\n software (0.34)\n", res.get(1).toString());
+ }
}
Modified: lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestExpressionAggregationFacetsExample.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestExpressionAggregationFacetsExample.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestExpressionAggregationFacetsExample.java (original)
+++ lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestExpressionAggregationFacetsExample.java Sun Jan 5 21:04:58 2014
@@ -1,13 +1,5 @@
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
* contributor license agreements. See the NOTICE file distributed with
@@ -25,25 +17,18 @@ import org.junit.Test;
* limitations under the License.
*/
-public class TestExpressionAggregationFacetsExample extends LuceneTestCase {
+import java.util.List;
+import java.util.Locale;
- private static String toSimpleString(FacetResult fr) {
- StringBuilder sb = new StringBuilder();
- toSimpleString(fr.getFacetRequest().categoryPath.length, 0, sb, fr.getFacetResultNode(), "");
- return sb.toString();
- }
-
- 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 + " ");
- }
- }
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.util.LuceneTestCase;
+import org.junit.Test;
+
+public class TestExpressionAggregationFacetsExample extends LuceneTestCase {
@Test
public void testSimple() throws Exception {
- List<FacetResult> facetResults = new ExpressionAggregationFacetsExample().runSearch();
- assertEquals("A (0.000)\n B (2.236)\n C (1.732)\n", toSimpleString(facetResults.get(0)));
+ FacetResult result = new ExpressionAggregationFacetsExample().runSearch();
+ assertEquals("dim=A path=[] value=3.9681187 childCount=2\n B (2.236068)\n C (1.7320508)\n", result.toString());
}
-
}
Modified: lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestMultiCategoryListsFacetsExample.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestMultiCategoryListsFacetsExample.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestMultiCategoryListsFacetsExample.java (original)
+++ lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestMultiCategoryListsFacetsExample.java Sun Jan 5 21:04:58 2014
@@ -1,14 +1,5 @@
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.CategoryPath;
-import org.apache.lucene.util.LuceneTestCase;
-import org.junit.Test;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -26,33 +17,19 @@ import org.junit.Test;
* limitations under the License.
*/
-public class TestMultiCategoryListsFacetsExample extends LuceneTestCase {
+import java.util.List;
- private static final ObjectToIntMap<CategoryPath> expectedCounts = new ObjectToIntMap<CategoryPath>();
- static {
- expectedCounts.put(new CategoryPath("Publish Date", "2012"), 2);
- expectedCounts.put(new CategoryPath("Publish Date", "2010"), 2);
- expectedCounts.put(new CategoryPath("Publish Date", "1999"), 1);
- expectedCounts.put(new CategoryPath("Author", "Lisa"), 2);
- expectedCounts.put(new CategoryPath("Author", "Frank"), 1);
- expectedCounts.put(new CategoryPath("Author", "Susan"), 1);
- expectedCounts.put(new CategoryPath("Author", "Bob"), 1);
- }
-
- private void assertExpectedCounts(List<FacetResult> facetResults, ObjectToIntMap<CategoryPath> 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);
- }
- }
- }
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.util.LuceneTestCase;
+import org.junit.Test;
+
+public class TestMultiCategoryListsFacetsExample extends LuceneTestCase {
@Test
public void testExample() throws Exception {
- List<FacetResult> facetResults = new MultiCategoryListsFacetsExample().runSearch();
- assertEquals(2, facetResults.size());
- assertExpectedCounts(facetResults, expectedCounts);
+ List<FacetResult> results = new MultiCategoryListsFacetsExample().runSearch();
+ assertEquals(2, results.size());
+ assertEquals("dim=Author path=[] value=5 childCount=4\n Lisa (2)\n Bob (1)\n Susan (1)\n Frank (1)\n", results.get(0).toString());
+ assertEquals("dim=Publish Date path=[] value=5 childCount=3\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", results.get(1).toString());
}
-
}
Modified: lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestRangeFacetsExample.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestRangeFacetsExample.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestRangeFacetsExample.java (original)
+++ lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestRangeFacetsExample.java Sun Jan 5 21:04:58 2014
@@ -19,12 +19,7 @@ package org.apache.lucene.demo.facet;
import java.util.List;
-import org.apache.lucene.facet.collections.ObjectToIntMap;
-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.CategoryPath;
+import org.apache.lucene.facet.FacetResult;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
import org.apache.lucene.util.LuceneTestCase;
@@ -33,27 +28,12 @@ import org.junit.Test;
@SuppressCodecs("Lucene3x")
public class TestRangeFacetsExample extends LuceneTestCase {
- private static final ObjectToIntMap<CategoryPath> expectedCounts = new ObjectToIntMap<CategoryPath>();
- static {
- expectedCounts.put(new CategoryPath("timestamp", "Past hour"), 4);
- expectedCounts.put(new CategoryPath("timestamp", "Past six hours"), 22);
- expectedCounts.put(new CategoryPath("timestamp", "Past day"), 87);
- }
-
- private void assertExpectedCounts(FacetResult res, ObjectToIntMap<CategoryPath> expCounts) {
- FacetResultNode root = res.getFacetResultNode();
- for (FacetResultNode node : root.subResults) {
- assertEquals("incorrect count for " + node.label, expCounts.get(node.label), (int) node.value);
- }
- }
-
@Test
public void testSimple() throws Exception {
RangeFacetsExample example = new RangeFacetsExample();
example.index();
- List<FacetResult> facetResults = example.search();
- assertEquals(1, facetResults.size());
- assertExpectedCounts(facetResults.get(0), expectedCounts);
+ FacetResult result = example.search();
+ assertEquals("dim=timestamp path=[] value=87 childCount=3\n Past hour (4)\n Past six hours (22)\n Past day (87)\n", result.toString());
example.close();
}
@@ -62,8 +42,7 @@ public class TestRangeFacetsExample exte
public void testDrillDown() throws Exception {
RangeFacetsExample example = new RangeFacetsExample();
example.index();
- List<FacetResult> facetResults = example.search();
- TopDocs hits = example.drillDown((LongRange) ((RangeFacetRequest<LongRange>) facetResults.get(0).getFacetRequest()).ranges[1]);
+ TopDocs hits = example.drillDown(example.PAST_SIX_HOURS);
assertEquals(22, hits.totalHits);
example.close();
}
Modified: lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java (original)
+++ lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleFacetsExample.java Sun Jan 5 21:04:58 2014
@@ -1,14 +1,5 @@
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.CategoryPath;
-import org.apache.lucene.util.LuceneTestCase;
-import org.junit.Test;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -26,46 +17,26 @@ import org.junit.Test;
* limitations under the License.
*/
+import java.util.List;
+
+import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.taxonomy.FacetLabel;
+import org.apache.lucene.util.LuceneTestCase;
+import org.junit.Test;
+
public class TestSimpleFacetsExample extends LuceneTestCase {
- private static final ObjectToIntMap<CategoryPath> expectedCounts = new ObjectToIntMap<CategoryPath>();
- static {
- expectedCounts.put(new CategoryPath("Publish Date", "2012"), 2);
- expectedCounts.put(new CategoryPath("Publish Date", "2010"), 2);
- expectedCounts.put(new CategoryPath("Publish Date", "1999"), 1);
- expectedCounts.put(new CategoryPath("Author", "Lisa"), 2);
- expectedCounts.put(new CategoryPath("Author", "Frank"), 1);
- expectedCounts.put(new CategoryPath("Author", "Susan"), 1);
- expectedCounts.put(new CategoryPath("Author", "Bob"), 1);
- }
-
- private static final ObjectToIntMap<CategoryPath> expectedCountsDrillDown = new ObjectToIntMap<CategoryPath>();
- static {
- expectedCountsDrillDown.put(new CategoryPath("Author", "Lisa"), 1);
- expectedCountsDrillDown.put(new CategoryPath("Author", "Bob"), 1);
- }
-
- private void assertExpectedCounts(List<FacetResult> facetResults, ObjectToIntMap<CategoryPath> 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
public void testSimple() throws Exception {
- List<FacetResult> facetResults = new SimpleFacetsExample().runSearch();
- assertEquals(2, facetResults.size());
- assertExpectedCounts(facetResults, expectedCounts);
+ List<FacetResult> results = new SimpleFacetsExample().runSearch();
+ assertEquals(2, results.size());
+ assertEquals("dim=Author path=[] value=5 childCount=4\n Lisa (2)\n Bob (1)\n Susan (1)\n Frank (1)\n", results.get(0).toString());
+ assertEquals("dim=Publish Date path=[] value=5 childCount=3\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", results.get(1).toString());
}
@Test
public void testDrillDown() throws Exception {
- List<FacetResult> facetResults = new SimpleFacetsExample().runDrillDown();
- assertEquals(1, facetResults.size());
- assertExpectedCounts(facetResults, expectedCountsDrillDown);
+ FacetResult result = new SimpleFacetsExample().runDrillDown();
+ assertEquals("dim=Author path=[] value=2 childCount=2\n Bob (1)\n Lisa (1)\n", result.toString());
}
-
}
Modified: lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleSortedSetFacetsExample.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleSortedSetFacetsExample.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleSortedSetFacetsExample.java (original)
+++ lucene/dev/branches/lucene5376/lucene/demo/src/test/org/apache/lucene/demo/facet/TestSimpleSortedSetFacetsExample.java Sun Jan 5 21:04:58 2014
@@ -1,15 +1,5 @@
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.CategoryPath;
-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
* contributor license agreements. See the NOTICE file distributed with
@@ -27,48 +17,30 @@ import org.junit.Test;
* limitations under the License.
*/
+import java.util.List;
+
+import org.apache.lucene.facet.FacetResult;
+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:
@SuppressCodecs({"Lucene40", "Lucene41"})
public class TestSimpleSortedSetFacetsExample extends LuceneTestCase {
- private static final ObjectToIntMap<CategoryPath> expectedCounts = new ObjectToIntMap<CategoryPath>();
- static {
- expectedCounts.put(new CategoryPath("Publish Year", "2012"), 2);
- expectedCounts.put(new CategoryPath("Publish Year", "2010"), 2);
- expectedCounts.put(new CategoryPath("Publish Year", "1999"), 1);
- expectedCounts.put(new CategoryPath("Author", "Lisa"), 2);
- expectedCounts.put(new CategoryPath("Author", "Frank"), 1);
- expectedCounts.put(new CategoryPath("Author", "Susan"), 1);
- expectedCounts.put(new CategoryPath("Author", "Bob"), 1);
- }
-
- private static final ObjectToIntMap<CategoryPath> expectedCountsDrillDown = new ObjectToIntMap<CategoryPath>();
- static {
- expectedCountsDrillDown.put(new CategoryPath("Author", "Lisa"), 1);
- expectedCountsDrillDown.put(new CategoryPath("Author", "Bob"), 1);
- }
-
- private void assertExpectedCounts(List<FacetResult> facetResults, ObjectToIntMap<CategoryPath> 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
public void testSimple() throws Exception {
- List<FacetResult> facetResults = new SimpleSortedSetFacetsExample().runSearch();
- assertEquals(2, facetResults.size());
- assertExpectedCounts(facetResults, expectedCounts);
+ List<FacetResult> results = new SimpleSortedSetFacetsExample().runSearch();
+ assertEquals(2, results.size());
+ assertEquals("dim=Author path=[] value=5 childCount=4\n Lisa (2)\n Bob (1)\n Frank (1)\n Susan (1)\n", results.get(0).toString());
+ assertEquals("dim=Publish Year path=[] value=5 childCount=3\n 2010 (2)\n 2012 (2)\n 1999 (1)\n", results.get(1).toString());
}
@Test
public void testDrillDown() throws Exception {
- List<FacetResult> facetResults = new SimpleSortedSetFacetsExample().runDrillDown();
- assertEquals(1, facetResults.size());
- assertExpectedCounts(facetResults, expectedCountsDrillDown);
+ FacetResult result = new SimpleSortedSetFacetsExample().runDrillDown();
+ assertEquals("dim=Author path=[] value=2 childCount=2\n Bob (1)\n Lisa (1)\n", result.toString());
}
-
}
Modified: lucene/dev/branches/lucene5376/lucene/facet/build.xml
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/build.xml?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/build.xml (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/build.xml Sun Jan 5 21:04:58 2014
@@ -24,7 +24,7 @@
</description>
<property name="forbidden-sysout-excludes" value="
- org/apache/lucene/facet/util/PrintTaxonomyStats.class
+ org/apache/lucene/facet/taxonomy/PrintTaxonomyStats.class
"/>
<import file="../module-build.xml"/>
Modified: lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/package.html
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/package.html?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/package.html (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/package.html Sun Jan 5 21:04:58 2014
@@ -20,5 +20,46 @@
</head>
<body>
<h1>faceted search</h1>
+ <p>
+ This module provides multiple methods for computing facet counts and
+ value aggregations:
+ <ul>
+ <li> Taxonomy-based methods rely on a separate taxonomy index to
+ map hierarchical facet paths to global int ordinals for fast
+ counting at search time; these methods can compute counts
+ (({@link org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts}, {@link
+ org.apache.lucene.facet.taxonomy.TaxonomyFacetCounts}) aggregate long or double values {@link
+ org.apache.lucene.facet.taxonomy.TaxonomyFacetSumIntAssociations}, {@link
+ org.apache.lucene.facet.taxonomy.TaxonomyFacetSumFloatAssociations}, {@link
+ org.apache.lucene.facet.taxonomy.TaxonomyFacetSumValueSource}. Add {@link org.apache.lucene.facet.FacetField} or
+ {@link org.apache.lucene.facet.taxonomy.AssociationFacetField} to your documents at index time
+ to use taxonomy-based methods.
+
+ <li> Sorted-set doc values method does not require a separate
+ taxonomy index, and computes counts based on sorted set doc
+ values fields ({@link org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts}). Add
+ {@link org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetField} to your documents at
+ index time to use sorted set facet counts.
+
+ <li> Range faceting {@link org.apache.lucene.facet.range.LongRangeFacetCounts}, {@link
+ org.apache.lucene.facet.range.DoubleRangeFacetCounts} compute counts for a dynamic numeric
+ range from a provided {@link org.apache.lucene.queries.function.ValueSource} (previously indexed
+ numeric field, or a dynamic expression such as distance).
+ </ul>
+ </p>
+ <p>
+ At search time you first run your search, but pass a {@link
+ org.apache.lucene.facet.FacetsCollector} to gather all hits (and optionally, scores for each
+ hit). Then, instantiate whichever facet methods you'd like to use
+ to compute aggregates. Finally, all methods implement a common
+ {@link org.apache.lucene.facet.Facets} base API that you use to obtain specific facet
+ counts.
+ </p>
+ <p>
+ The various {@link org.apache.lucene.facet.FacetsCollector#search} utility methods are
+ useful for doing an "ordinary" search (sorting by score, or by a
+ specified Sort) but also collecting into a {@link org.apache.lucene.facet.FacetsCollector} for
+ subsequent faceting.
+ </p>
</body>
-</html>
\ No newline at end of file
+</html>
Modified: lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/ParallelTaxonomyArrays.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/ParallelTaxonomyArrays.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/ParallelTaxonomyArrays.java (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/ParallelTaxonomyArrays.java Sun Jan 5 21:04:58 2014
@@ -42,6 +42,10 @@ package org.apache.lucene.facet.taxonomy
*/
public abstract class ParallelTaxonomyArrays {
+ /** Sole constructor. */
+ public ParallelTaxonomyArrays() {
+ }
+
/**
* Returns the parents array, where {@code parents[i]} denotes the parent of
* category ordinal {@code i}.
Modified: lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyReader.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyReader.java (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyReader.java Sun Jan 5 21:04:58 2014
@@ -89,10 +89,14 @@ public abstract class TaxonomyReader imp
}
}
+
+ /** Sole constructor. */
+ public TaxonomyReader() {
+ }
/**
* The root category (the category with the empty path) always has the ordinal
- * 0, to which we give a name ROOT_ORDINAL. {@link #getOrdinal(CategoryPath)}
+ * 0, to which we give a name ROOT_ORDINAL. {@link #getOrdinal(FacetLabel)}
* of an empty path will always return {@code ROOT_ORDINAL}, and
* {@link #getPath(int)} with {@code ROOT_ORDINAL} will return the empty path.
*/
@@ -142,7 +146,7 @@ public abstract class TaxonomyReader imp
protected abstract TaxonomyReader doOpenIfChanged() throws IOException;
/**
- * @throws AlreadyClosedException if this IndexReader is closed
+ * Throws {@link AlreadyClosedException} if this IndexReader is closed
*/
protected final void ensureOpen() throws AlreadyClosedException {
if (getRefCount() <= 0) {
@@ -215,10 +219,18 @@ public abstract class TaxonomyReader imp
* @return the category's ordinal or {@link #INVALID_ORDINAL} if the category
* wasn't foun.
*/
- public abstract int getOrdinal(CategoryPath categoryPath) throws IOException;
+ public abstract int getOrdinal(FacetLabel categoryPath) throws IOException;
+
+ /** Returns ordinal for the dim + path. */
+ public int getOrdinal(String dim, String[] path) throws IOException {
+ String[] fullPath = new String[path.length+1];
+ fullPath[0] = dim;
+ System.arraycopy(path, 0, fullPath, 1, path.length);
+ return getOrdinal(new FacetLabel(fullPath));
+ }
/** Returns the path name of the category with the given ordinal. */
- public abstract CategoryPath getPath(int ordinal) throws IOException;
+ public abstract FacetLabel getPath(int ordinal) throws IOException;
/** Returns the current refCount for this taxonomy reader. */
public final int getRefCount() {
Modified: lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyWriter.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyWriter.java (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/TaxonomyWriter.java Sun Jan 5 21:04:58 2014
@@ -64,7 +64,7 @@ public interface TaxonomyWriter extends
* ordinal of a category is guaranteed to be smaller then the ordinal of
* any of its descendants.
*/
- public int addCategory(CategoryPath categoryPath) throws IOException;
+ public int addCategory(FacetLabel categoryPath) throws IOException;
/**
* getParent() returns the ordinal of the parent category of the category
Modified: lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/Consts.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/Consts.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/Consts.java (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/Consts.java Sun Jan 5 21:04:58 2014
@@ -23,23 +23,8 @@ import org.apache.lucene.util.BytesRef;
* @lucene.experimental
*/
abstract class Consts {
-
static final String FULL = "$full_path$";
static final String FIELD_PAYLOADS = "$payloads$";
static final String PAYLOAD_PARENT = "p";
static final BytesRef PAYLOAD_PARENT_BYTES_REF = new BytesRef(PAYLOAD_PARENT);
-
- /**
- * Delimiter used for creating the full path of a category from the list of
- * its labels from root. It is forbidden for labels to contain this
- * character.
- * <P>
- * Originally, we used \uFFFE, officially a "unicode noncharacter" (invalid
- * unicode character) for this purpose. Recently, we switched to the
- * "private-use" character \uF749. Even more recently, we
- * switched to \U001F (INFORMATION_SEPARATOR).
- */
- //static final char DEFAULT_DELIMITER = '\uFFFE';
- //static final char DEFAULT_DELIMITER = '\uF749';
- static final char DEFAULT_DELIMITER = '\u001F';
}
Modified: lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyReader.java Sun Jan 5 21:04:58 2014
@@ -5,8 +5,9 @@ import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
-import org.apache.lucene.facet.collections.LRUHashMap;
-import org.apache.lucene.facet.taxonomy.CategoryPath;
+import org.apache.lucene.facet.FacetsConfig;
+import org.apache.lucene.facet.taxonomy.FacetLabel;
+import org.apache.lucene.facet.taxonomy.LRUHashMap;
import org.apache.lucene.facet.taxonomy.ParallelTaxonomyArrays;
import org.apache.lucene.facet.taxonomy.TaxonomyReader;
import org.apache.lucene.index.CorruptIndexException; // javadocs
@@ -60,28 +61,26 @@ public class DirectoryTaxonomyReader ext
private final DirectoryReader indexReader;
// TODO: test DoubleBarrelLRUCache and consider using it instead
- private LRUHashMap<CategoryPath, Integer> ordinalCache;
- private LRUHashMap<Integer, CategoryPath> categoryCache;
+ private LRUHashMap<FacetLabel, Integer> ordinalCache;
+ private LRUHashMap<Integer, FacetLabel> categoryCache;
private volatile TaxonomyIndexArrays taxoArrays;
- private char delimiter = Consts.DEFAULT_DELIMITER;
-
/**
* Called only from {@link #doOpenIfChanged()}. If the taxonomy has been
* recreated, you should pass {@code null} as the caches and parent/children
* arrays.
*/
DirectoryTaxonomyReader(DirectoryReader indexReader, DirectoryTaxonomyWriter taxoWriter,
- LRUHashMap<CategoryPath,Integer> ordinalCache, LRUHashMap<Integer,CategoryPath> categoryCache,
+ LRUHashMap<FacetLabel,Integer> ordinalCache, LRUHashMap<Integer,FacetLabel> categoryCache,
TaxonomyIndexArrays taxoArrays) throws IOException {
this.indexReader = indexReader;
this.taxoWriter = taxoWriter;
this.taxoEpoch = taxoWriter == null ? -1 : taxoWriter.getTaxonomyEpoch();
// use the same instance of the cache, note the protective code in getOrdinal and getPath
- this.ordinalCache = ordinalCache == null ? new LRUHashMap<CategoryPath,Integer>(DEFAULT_CACHE_VALUE) : ordinalCache;
- this.categoryCache = categoryCache == null ? new LRUHashMap<Integer,CategoryPath>(DEFAULT_CACHE_VALUE) : categoryCache;
+ this.ordinalCache = ordinalCache == null ? new LRUHashMap<FacetLabel,Integer>(DEFAULT_CACHE_VALUE) : ordinalCache;
+ this.categoryCache = categoryCache == null ? new LRUHashMap<Integer,FacetLabel>(DEFAULT_CACHE_VALUE) : categoryCache;
this.taxoArrays = taxoArrays != null ? new TaxonomyIndexArrays(indexReader, taxoArrays) : null;
}
@@ -103,8 +102,8 @@ public class DirectoryTaxonomyReader ext
// These are the default cache sizes; they can be configured after
// construction with the cache's setMaxSize() method
- ordinalCache = new LRUHashMap<CategoryPath, Integer>(DEFAULT_CACHE_VALUE);
- categoryCache = new LRUHashMap<Integer, CategoryPath>(DEFAULT_CACHE_VALUE);
+ ordinalCache = new LRUHashMap<FacetLabel, Integer>(DEFAULT_CACHE_VALUE);
+ categoryCache = new LRUHashMap<Integer, FacetLabel>(DEFAULT_CACHE_VALUE);
}
/**
@@ -122,8 +121,8 @@ public class DirectoryTaxonomyReader ext
// These are the default cache sizes; they can be configured after
// construction with the cache's setMaxSize() method
- ordinalCache = new LRUHashMap<CategoryPath, Integer>(DEFAULT_CACHE_VALUE);
- categoryCache = new LRUHashMap<Integer, CategoryPath>(DEFAULT_CACHE_VALUE);
+ ordinalCache = new LRUHashMap<FacetLabel, Integer>(DEFAULT_CACHE_VALUE);
+ categoryCache = new LRUHashMap<Integer, FacetLabel>(DEFAULT_CACHE_VALUE);
}
private synchronized void initTaxoArrays() throws IOException {
@@ -209,10 +208,14 @@ public class DirectoryTaxonomyReader ext
}
}
+ /** Open the {@link DirectoryReader} from this {@link
+ * Directory}. */
protected DirectoryReader openIndexReader(Directory directory) throws IOException {
return DirectoryReader.open(directory);
}
+ /** Open the {@link DirectoryReader} from this {@link
+ * IndexWriter}. */
protected DirectoryReader openIndexReader(IndexWriter writer) throws IOException {
return DirectoryReader.open(writer, false);
}
@@ -242,7 +245,7 @@ public class DirectoryTaxonomyReader ext
}
@Override
- public int getOrdinal(CategoryPath cp) throws IOException {
+ public int getOrdinal(FacetLabel cp) throws IOException {
ensureOpen();
if (cp.length == 0) {
return ROOT_ORDINAL;
@@ -270,7 +273,7 @@ public class DirectoryTaxonomyReader ext
// If we're still here, we have a cache miss. We need to fetch the
// value from disk, and then also put it in the cache:
int ret = TaxonomyReader.INVALID_ORDINAL;
- DocsEnum docs = MultiFields.getTermDocsEnum(indexReader, null, Consts.FULL, new BytesRef(cp.toString(delimiter)), 0);
+ DocsEnum docs = MultiFields.getTermDocsEnum(indexReader, null, Consts.FULL, new BytesRef(FacetsConfig.pathToString(cp.components, cp.length)), 0);
if (docs != null && docs.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
ret = docs.docID();
@@ -288,7 +291,7 @@ public class DirectoryTaxonomyReader ext
}
@Override
- public CategoryPath getPath(int ordinal) throws IOException {
+ public FacetLabel getPath(int ordinal) throws IOException {
ensureOpen();
// Since the cache is shared with DTR instances allocated from
@@ -303,14 +306,14 @@ public class DirectoryTaxonomyReader ext
// wrapped as LRU?
Integer catIDInteger = Integer.valueOf(ordinal);
synchronized (categoryCache) {
- CategoryPath res = categoryCache.get(catIDInteger);
+ FacetLabel res = categoryCache.get(catIDInteger);
if (res != null) {
return res;
}
}
StoredDocument doc = indexReader.document(ordinal);
- CategoryPath ret = new CategoryPath(doc.get(Consts.FULL), delimiter);
+ FacetLabel ret = new FacetLabel(FacetsConfig.stringToPath(doc.get(Consts.FULL)));
synchronized (categoryCache) {
categoryCache.put(catIDInteger, ret);
}
@@ -326,7 +329,7 @@ public class DirectoryTaxonomyReader ext
/**
* setCacheSize controls the maximum allowed size of each of the caches
- * used by {@link #getPath(int)} and {@link #getOrdinal(CategoryPath)}.
+ * used by {@link #getPath(int)} and {@link #getOrdinal(FacetLabel)}.
* <P>
* Currently, if the given size is smaller than the current size of
* a cache, it will not shrink, and rather we be limited to its current
@@ -343,28 +346,16 @@ public class DirectoryTaxonomyReader ext
}
}
- /**
- * setDelimiter changes the character that the taxonomy uses in its
- * internal storage as a delimiter between category components. Do not
- * use this method unless you really know what you are doing.
- * <P>
- * If you do use this method, make sure you call it before any other
- * methods that actually queries the taxonomy. Moreover, make sure you
- * always pass the same delimiter for all LuceneTaxonomyWriter and
- * LuceneTaxonomyReader objects you create.
- */
- public void setDelimiter(char delimiter) {
- ensureOpen();
- this.delimiter = delimiter;
- }
-
+ /** Returns ordinal -> label mapping, up to the provided
+ * max ordinal or number of ordinals, whichever is
+ * smaller. */
public String toString(int max) {
ensureOpen();
StringBuilder sb = new StringBuilder();
int upperl = Math.min(max, indexReader.maxDoc());
for (int i = 0; i < upperl; i++) {
try {
- CategoryPath category = this.getPath(i);
+ FacetLabel category = this.getPath(i);
if (category == null) {
sb.append(i + ": NULL!! \n");
continue;
Modified: lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/directory/DirectoryTaxonomyWriter.java Sun Jan 5 21:04:58 2014
@@ -21,12 +21,13 @@ import org.apache.lucene.document.Field;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
-import org.apache.lucene.facet.taxonomy.CategoryPath;
+import org.apache.lucene.facet.FacetsConfig;
+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.writercache.TaxonomyWriterCache;
-import org.apache.lucene.facet.taxonomy.writercache.cl2o.Cl2oTaxonomyWriterCache;
-import org.apache.lucene.facet.taxonomy.writercache.lru.LruTaxonomyWriterCache;
+import org.apache.lucene.facet.taxonomy.writercache.Cl2oTaxonomyWriterCache;
+import org.apache.lucene.facet.taxonomy.writercache.LruTaxonomyWriterCache;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.CorruptIndexException; // javadocs
@@ -34,8 +35,8 @@ import org.apache.lucene.index.Directory
import org.apache.lucene.index.DocsEnum;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
-import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
+import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogByteSizeMergePolicy;
import org.apache.lucene.index.ReaderManager;
import org.apache.lucene.index.SegmentInfos;
@@ -103,8 +104,7 @@ public class DirectoryTaxonomyWriter imp
// Records the taxonomy index epoch, updated on replaceTaxonomy as well.
private long indexEpoch;
-
- private char delimiter = Consts.DEFAULT_DELIMITER;
+
private SinglePositionTokenStream parentStream = new SinglePositionTokenStream(Consts.PAYLOAD_PARENT);
private Field parentStreamField;
private Field fullPathField;
@@ -140,23 +140,6 @@ public class DirectoryTaxonomyWriter imp
}
/**
- * Changes the character that the taxonomy uses in its internal storage as a
- * delimiter between category components. Do not use this method unless you
- * really know what you are doing. It has nothing to do with whatever
- * character the application may be using to represent categories for its own
- * use.
- * <p>
- * If you do use this method, make sure you call it before any other methods
- * that actually queries the taxonomy. Moreover, make sure you always pass the
- * same delimiter for all taxonomy writer and reader instances you create for
- * the same directory.
- */
- public void setDelimiter(char delimiter) {
- ensureOpen();
- this.delimiter = delimiter;
- }
-
- /**
* Forcibly unlocks the taxonomy in the named directory.
* <P>
* Caution: this should only be used by failure recovery code, when it is
@@ -248,7 +231,7 @@ public class DirectoryTaxonomyWriter imp
cacheIsComplete = true;
// Make sure that the taxonomy always contain the root category
// with category id 0.
- addCategory(CategoryPath.EMPTY);
+ addCategory(new FacetLabel());
} else {
// There are some categories on the disk, which we have not yet
// read into the cache, and therefore the cache is incomplete.
@@ -343,6 +326,7 @@ public class DirectoryTaxonomyWriter imp
return new Cl2oTaxonomyWriterCache(1024, 0.15f, 3);
}
+ /** Create this with {@code OpenMode.CREATE_OR_APPEND}. */
public DirectoryTaxonomyWriter(Directory d) throws IOException {
this(d, OpenMode.CREATE_OR_APPEND);
}
@@ -389,7 +373,7 @@ public class DirectoryTaxonomyWriter imp
* returning the category's ordinal, or a negative number in case the
* category does not yet exist in the taxonomy.
*/
- protected synchronized int findCategory(CategoryPath categoryPath) throws IOException {
+ protected synchronized int findCategory(FacetLabel categoryPath) throws IOException {
// If we can find the category in the cache, or we know the cache is
// complete, we can return the response directly from it
int res = cache.get(categoryPath);
@@ -422,7 +406,7 @@ public class DirectoryTaxonomyWriter imp
int doc = -1;
DirectoryReader reader = readerManager.acquire();
try {
- final BytesRef catTerm = new BytesRef(categoryPath.toString(delimiter));
+ final BytesRef catTerm = new BytesRef(FacetsConfig.pathToString(categoryPath.components, categoryPath.length));
TermsEnum termsEnum = null; // reuse
DocsEnum docs = null; // reuse
for (AtomicReaderContext ctx : reader.leaves()) {
@@ -448,7 +432,7 @@ public class DirectoryTaxonomyWriter imp
}
@Override
- public int addCategory(CategoryPath categoryPath) throws IOException {
+ public int addCategory(FacetLabel categoryPath) throws IOException {
ensureOpen();
// check the cache outside the synchronized block. this results in better
// concurrency when categories are there.
@@ -480,14 +464,14 @@ public class DirectoryTaxonomyWriter imp
* parent is always added to the taxonomy before its child). We do this by
* recursion.
*/
- private int internalAddCategory(CategoryPath cp) throws IOException {
+ private int internalAddCategory(FacetLabel cp) throws IOException {
// Find our parent's ordinal (recursively adding the parent category
// to the taxonomy if it's not already there). Then add the parent
// ordinal as payloads (rather than a stored field; payloads can be
// more efficiently read into memory in bulk by LuceneTaxonomyReader)
int parent;
if (cp.length > 1) {
- CategoryPath parentPath = cp.subpath(cp.length - 1);
+ FacetLabel parentPath = cp.subpath(cp.length - 1);
parent = findCategory(parentPath);
if (parent < 0) {
parent = internalAddCategory(parentPath);
@@ -516,7 +500,7 @@ public class DirectoryTaxonomyWriter imp
* Note that the methods calling addCategoryDocument() are synchornized, so
* this method is effectively synchronized as well.
*/
- private int addCategoryDocument(CategoryPath categoryPath, int parent) throws IOException {
+ private int addCategoryDocument(FacetLabel categoryPath, int parent) throws IOException {
// Before Lucene 2.9, position increments >=0 were supported, so we
// added 1 to parent to allow the parent -1 (the parent of the root).
// Unfortunately, starting with Lucene 2.9, after LUCENE-1542, this is
@@ -530,7 +514,7 @@ public class DirectoryTaxonomyWriter imp
Document d = new Document();
d.add(parentStreamField);
- fullPathField.setStringValue(categoryPath.toString(delimiter));
+ fullPathField.setStringValue(FacetsConfig.pathToString(categoryPath.components, categoryPath.length));
d.add(fullPathField);
// Note that we do no pass an Analyzer here because the fields that are
@@ -597,7 +581,7 @@ public class DirectoryTaxonomyWriter imp
}
}
- private void addToCache(CategoryPath categoryPath, int id) throws IOException {
+ private void addToCache(FacetLabel categoryPath, int id) throws IOException {
if (cache.put(categoryPath, id)) {
// If cache.put() returned true, it means the cache was limited in
// size, became full, and parts of it had to be evicted. It is
@@ -730,7 +714,7 @@ public class DirectoryTaxonomyWriter imp
// hence documents), there are no deletions in the index. Therefore, it
// is sufficient to call next(), and then doc(), exactly once with no
// 'validation' checks.
- CategoryPath cp = new CategoryPath(t.utf8ToString(), delimiter);
+ FacetLabel cp = new FacetLabel(FacetsConfig.stringToPath(t.utf8ToString()));
docsEnum = termsEnum.docs(null, docsEnum, DocsEnum.FLAG_NONE);
boolean res = cache.put(cp, docsEnum.nextDoc() + ctx.docBase);
assert !res : "entries should not have been evicted from the cache";
@@ -819,8 +803,7 @@ public class DirectoryTaxonomyWriter imp
final Terms terms = ar.terms(Consts.FULL);
te = terms.iterator(te);
while (te.next() != null) {
- String value = te.term().utf8ToString();
- CategoryPath cp = new CategoryPath(value, delimiter);
+ FacetLabel cp = new FacetLabel(FacetsConfig.stringToPath(te.term().utf8ToString()));
final int ordinal = addCategory(cp);
docs = te.docs(null, docs, DocsEnum.FLAG_NONE);
ordinalMap.addMapping(docs.nextDoc() + base, ordinal);
@@ -858,12 +841,16 @@ public class DirectoryTaxonomyWriter imp
* and size-1.
*/
public void setSize(int size) throws IOException;
+
+ /** Record a mapping. */
public void addMapping(int origOrdinal, int newOrdinal) throws IOException;
+
/**
* Call addDone() to say that all addMapping() have been done.
* In some implementations this might free some resources.
*/
public void addDone() throws IOException;
+
/**
* Return the map from the taxonomy's original (consecutive) ordinals
* to the new taxonomy's ordinals. If the map has to be read from disk
@@ -880,6 +867,11 @@ public class DirectoryTaxonomyWriter imp
*/
public static final class MemoryOrdinalMap implements OrdinalMap {
int[] map;
+
+ /** Sole constructor. */
+ public MemoryOrdinalMap() {
+ }
+
@Override
public void setSize(int taxonomySize) {
map = new int[taxonomySize];
@@ -903,6 +895,7 @@ public class DirectoryTaxonomyWriter imp
File tmpfile;
DataOutputStream out;
+ /** Sole constructor. */
public DiskOrdinalMap(File tmpfile) throws FileNotFoundException {
this.tmpfile = tmpfile;
out = new DataOutputStream(new BufferedOutputStream(
Modified: lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/writercache/TaxonomyWriterCache.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/writercache/TaxonomyWriterCache.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/writercache/TaxonomyWriterCache.java (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/writercache/TaxonomyWriterCache.java Sun Jan 5 21:04:58 2014
@@ -1,6 +1,6 @@
package org.apache.lucene.facet.taxonomy.writercache;
-import org.apache.lucene.facet.taxonomy.CategoryPath;
+import org.apache.lucene.facet.taxonomy.FacetLabel;
import org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter;
/*
@@ -37,7 +37,7 @@ import org.apache.lucene.facet.taxonomy.
* <p>
* However, if it does so, it should clear out large parts of the cache at once,
* because the user will typically need to work hard to recover from every cache
- * cleanup (see {@link #put(CategoryPath, int)}'s return value).
+ * cleanup (see {@link #put(FacetLabel, int)}'s return value).
* <p>
* <b>NOTE:</b> the cache may be accessed concurrently by multiple threads,
* therefore cache implementations should take this into consideration.
@@ -62,7 +62,7 @@ public interface TaxonomyWriterCache {
* it means the category does not exist. Otherwise, the category might
* still exist, but just be missing from the cache.
*/
- public int get(CategoryPath categoryPath);
+ public int get(FacetLabel categoryPath);
/**
* Add a category to the cache, with the given ordinal as the value.
@@ -82,7 +82,7 @@ public interface TaxonomyWriterCache {
* It doesn't really matter, because normally the next thing we do after
* finding that a category does not exist is to add it.
*/
- public boolean put(CategoryPath categoryPath, int ordinal);
+ public boolean put(FacetLabel categoryPath, int ordinal);
/**
* Returns true if the cache is full, such that the next {@link #put} will
Modified: lucene/dev/branches/lucene5376/lucene/facet/src/test/org/apache/lucene/facet/FacetTestCase.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/lucene5376/lucene/facet/src/test/org/apache/lucene/facet/FacetTestCase.java?rev=1555629&r1=1555628&r2=1555629&view=diff
==============================================================================
--- lucene/dev/branches/lucene5376/lucene/facet/src/test/org/apache/lucene/facet/FacetTestCase.java (original)
+++ lucene/dev/branches/lucene5376/lucene/facet/src/test/org/apache/lucene/facet/FacetTestCase.java Sun Jan 5 21:04:58 2014
@@ -1,19 +1,5 @@
package org.apache.lucene.facet;
-import java.util.Random;
-
-import org.apache.lucene.facet.encoding.DGapIntEncoder;
-import org.apache.lucene.facet.encoding.DGapVInt8IntEncoder;
-import org.apache.lucene.facet.encoding.EightFlagsIntEncoder;
-import org.apache.lucene.facet.encoding.FourFlagsIntEncoder;
-import org.apache.lucene.facet.encoding.IntEncoder;
-import org.apache.lucene.facet.encoding.NOnesIntEncoder;
-import org.apache.lucene.facet.encoding.SortingIntEncoder;
-import org.apache.lucene.facet.encoding.UniqueValuesIntEncoder;
-import org.apache.lucene.facet.encoding.VInt8IntEncoder;
-import org.apache.lucene.facet.params.CategoryListParams;
-import org.apache.lucene.util.LuceneTestCase;
-
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
@@ -31,34 +17,190 @@ import org.apache.lucene.util.LuceneTest
* limitations under the License.
*/
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts;
+import org.apache.lucene.facet.taxonomy.TaxonomyFacetCounts;
+import org.apache.lucene.facet.taxonomy.TaxonomyReader;
+import org.apache.lucene.util.BytesRef;
+import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util._TestUtil;
+
public abstract class FacetTestCase extends LuceneTestCase {
- private static final IntEncoder[] ENCODERS = new IntEncoder[] {
- new SortingIntEncoder(new UniqueValuesIntEncoder(new VInt8IntEncoder())),
- new SortingIntEncoder(new UniqueValuesIntEncoder(new DGapIntEncoder(new VInt8IntEncoder()))),
- new SortingIntEncoder(new UniqueValuesIntEncoder(new DGapVInt8IntEncoder())),
- new SortingIntEncoder(new UniqueValuesIntEncoder(new DGapIntEncoder(new EightFlagsIntEncoder()))),
- new SortingIntEncoder(new UniqueValuesIntEncoder(new DGapIntEncoder(new FourFlagsIntEncoder()))),
- new SortingIntEncoder(new UniqueValuesIntEncoder(new DGapIntEncoder(new NOnesIntEncoder(3)))),
- new SortingIntEncoder(new UniqueValuesIntEncoder(new DGapIntEncoder(new NOnesIntEncoder(4)))),
- };
-
- /** Returns a {@link CategoryListParams} with random {@link IntEncoder} and field. */
- public static CategoryListParams randomCategoryListParams() {
- final String field = CategoryListParams.DEFAULT_FIELD + "$" + random().nextInt();
- return randomCategoryListParams(field);
+ public Facets getTaxonomyFacetCounts(TaxonomyReader taxoReader, FacetsConfig config, FacetsCollector c) throws IOException {
+ return getTaxonomyFacetCounts(taxoReader, config, c, FacetsConfig.DEFAULT_INDEX_FIELD_NAME);
}
-
- /** Returns a {@link CategoryListParams} with random {@link IntEncoder}. */
- public static CategoryListParams randomCategoryListParams(String field) {
- Random random = random();
- final IntEncoder encoder = ENCODERS[random.nextInt(ENCODERS.length)];
- return new CategoryListParams(field) {
- @Override
- public IntEncoder createEncoder() {
- return encoder;
+
+ public Facets getTaxonomyFacetCounts(TaxonomyReader taxoReader, FacetsConfig config, FacetsCollector c, String indexFieldName) throws IOException {
+ Facets facets;
+ if (random().nextBoolean()) {
+ facets = new FastTaxonomyFacetCounts(indexFieldName, taxoReader, config, c);
+ } else {
+ OrdinalsReader ordsReader = new DocValuesOrdinalsReader(indexFieldName);
+ if (random().nextBoolean()) {
+ ordsReader = new CachedOrdinalsReader(ordsReader);
}
- };
+ facets = new TaxonomyFacetCounts(ordsReader, taxoReader, config, c);
+ }
+
+ return facets;
+ }
+
+ protected String[] getRandomTokens(int count) {
+ String[] tokens = new String[count];
+ for(int i=0;i<tokens.length;i++) {
+ tokens[i] = _TestUtil.randomRealisticUnicodeString(random(), 1, 10);
+ //tokens[i] = _TestUtil.randomSimpleString(random(), 1, 10);
+ }
+ return tokens;
+ }
+
+ protected String pickToken(String[] tokens) {
+ for(int i=0;i<tokens.length;i++) {
+ if (random().nextBoolean()) {
+ return tokens[i];
+ }
+ }
+
+ // Move long tail onto first token:
+ return tokens[0];
+ }
+
+ protected static class TestDoc {
+ public String content;
+ public String[] dims;
+ public float value;
+ }
+
+ protected List<TestDoc> getRandomDocs(String[] tokens, int count, int numDims) {
+ List<TestDoc> docs = new ArrayList<>();
+ for(int i=0;i<count;i++) {
+ TestDoc doc = new TestDoc();
+ docs.add(doc);
+ doc.content = pickToken(tokens);
+ doc.dims = new String[numDims];
+ for(int j=0;j<numDims;j++) {
+ doc.dims[j] = pickToken(tokens);
+ if (random().nextInt(10) < 3) {
+ break;
+ }
+ }
+ if (VERBOSE) {
+ System.out.println(" doc " + i + ": content=" + doc.content);
+ for(int j=0;j<numDims;j++) {
+ if (doc.dims[j] != null) {
+ System.out.println(" dim[" + j + "]=" + doc.dims[j]);
+ }
+ }
+ }
+ }
+
+ return docs;
}
+ protected void sortTies(List<FacetResult> results) {
+ for(FacetResult result : results) {
+ sortTies(result.labelValues);
+ }
+ }
+
+ protected void sortTies(LabelAndValue[] labelValues) {
+ double lastValue = -1;
+ int numInRow = 0;
+ int i = 0;
+ while(i <= labelValues.length) {
+ if (i < labelValues.length && labelValues[i].value.doubleValue() == lastValue) {
+ numInRow++;
+ } else {
+ if (numInRow > 1) {
+ Arrays.sort(labelValues, i-numInRow, i,
+ new Comparator<LabelAndValue>() {
+ @Override
+ public int compare(LabelAndValue a, LabelAndValue b) {
+ assert a.value.doubleValue() == b.value.doubleValue();
+ return new BytesRef(a.label).compareTo(new BytesRef(b.label));
+ }
+ });
+ }
+ numInRow = 1;
+ if (i < labelValues.length) {
+ lastValue = labelValues[i].value.doubleValue();
+ }
+ }
+ i++;
+ }
+ }
+
+ protected void sortLabelValues(List<LabelAndValue> labelValues) {
+ Collections.sort(labelValues,
+ new Comparator<LabelAndValue>() {
+ @Override
+ public int compare(LabelAndValue a, LabelAndValue b) {
+ if (a.value.doubleValue() > b.value.doubleValue()) {
+ return -1;
+ } else if (a.value.doubleValue() < b.value.doubleValue()) {
+ return 1;
+ } else {
+ return new BytesRef(a.label).compareTo(new BytesRef(b.label));
+ }
+ }
+ });
+ }
+
+ protected void sortFacetResults(List<FacetResult> results) {
+ Collections.sort(results,
+ new Comparator<FacetResult>() {
+ @Override
+ public int compare(FacetResult a, FacetResult b) {
+ if (a.value.doubleValue() > b.value.doubleValue()) {
+ return -1;
+ } else if (b.value.doubleValue() > a.value.doubleValue()) {
+ return 1;
+ } else {
+ return 0;
+ }
+ }
+ });
+ }
+
+ protected void assertFloatValuesEquals(List<FacetResult> a, List<FacetResult> b) {
+ assertEquals(a.size(), b.size());
+ float lastValue = Float.POSITIVE_INFINITY;
+ Map<String,FacetResult> aByDim = new HashMap<String,FacetResult>();
+ for(int i=0;i<a.size();i++) {
+ assertTrue(a.get(i).value.floatValue() <= lastValue);
+ lastValue = a.get(i).value.floatValue();
+ aByDim.put(a.get(i).dim, a.get(i));
+ }
+ lastValue = Float.POSITIVE_INFINITY;
+ Map<String,FacetResult> bByDim = new HashMap<String,FacetResult>();
+ for(int i=0;i<b.size();i++) {
+ bByDim.put(b.get(i).dim, b.get(i));
+ assertTrue(b.get(i).value.floatValue() <= lastValue);
+ lastValue = b.get(i).value.floatValue();
+ }
+ for(String dim : aByDim.keySet()) {
+ assertFloatValuesEquals(aByDim.get(dim), bByDim.get(dim));
+ }
+ }
+
+ protected void assertFloatValuesEquals(FacetResult a, FacetResult b) {
+ assertEquals(a.dim, b.dim);
+ assertTrue(Arrays.equals(a.path, b.path));
+ assertEquals(a.childCount, b.childCount);
+ assertEquals(a.value.floatValue(), b.value.floatValue(), a.value.floatValue()/1e5);
+ assertEquals(a.labelValues.length, b.labelValues.length);
+ for(int i=0;i<a.labelValues.length;i++) {
+ assertEquals(a.labelValues[i].label, b.labelValues[i].label);
+ assertEquals(a.labelValues[i].value.floatValue(), b.labelValues[i].value.floatValue(), a.labelValues[i].value.floatValue()/1e5);
+ }
+ }
}