org.apache.lucene.facet.Facets Java Examples
The following examples show how to use
org.apache.lucene.facet.Facets.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FacetStorageTest.java From lumongo with Apache License 2.0 | 6 votes |
/** User drills down on 'Publish Year/2010'. */ private FacetResult drillDown() throws IOException { DirectoryReader indexReader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(indexReader); SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader); // Now user drills down on Publish Year/2010: DrillDownQuery q = new DrillDownQuery(config); q.add("Publish Year", "2010"); FacetsCollector fc = new FacetsCollector(); FacetsCollector.search(searcher, q, 10, fc); // Retrieve results Facets facets = new SortedSetDocValuesFacetCounts(state, fc); FacetResult result = facets.getTopChildren(10, "Author"); indexReader.close(); return result; }
Example #2
Source File: TestTaxonomyFacetCounts.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testGetFacetResultsTwice() throws Exception { // LUCENE-4893: counts were multiplied as many times as getFacetResults was called. Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); Document doc = new Document(); doc.add(new FacetField("a", "1")); doc.add(new FacetField("b", "1")); iw.addDocument(config.build(taxoWriter, doc)); DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, newSearcher(r), taxoReader, config); List<FacetResult> res1 = facets.getAllDims(10); List<FacetResult> res2 = facets.getAllDims(10); assertEquals("calling getFacetResults twice should return the .equals()=true result", res1, res2); iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #3
Source File: TestTaxonomyFacetCounts.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testChildCount() throws Exception { // LUCENE-4885: FacetResult.numValidDescendants was not set properly by FacetsAccumulator Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); for (int i = 0; i < 10; i++) { Document doc = new Document(); doc.add(new FacetField("a", Integer.toString(i))); iw.addDocument(config.build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, newSearcher(r), taxoReader, config); assertEquals(10, facets.getTopChildren(2, "a").childCount); iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #4
Source File: TestTaxonomyFacetCounts.java From lucene-solr with Apache License 2.0 | 6 votes |
private static Facets getAllFacets(String indexFieldName, IndexSearcher searcher, TaxonomyReader taxoReader, FacetsConfig config) throws IOException { if (random().nextBoolean()) { // Aggregate the facet counts: FacetsCollector c = 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(), c); return new FastTaxonomyFacetCounts(taxoReader, config, c); } else { return new FastTaxonomyFacetCounts(indexFieldName, searcher.getIndexReader(), taxoReader, config); } }
Example #5
Source File: TestTaxonomyFacetSumValueSource.java From lucene-solr with Apache License 2.0 | 6 votes |
public void testNoScore() throws Exception { Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); for (int i = 0; i < 4; i++) { Document doc = new Document(); doc.add(new NumericDocValuesField("price", (i+1))); doc.add(new FacetField("a", Integer.toString(i % 2))); iw.addDocument(config.build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector sfc = new FacetsCollector(); newSearcher(r).search(new MatchAllDocsQuery(), sfc); Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, sfc, DoubleValuesSource.fromLongField("price")); assertEquals("dim=a path=[] value=10.0 childCount=2\n 1 (6.0)\n 0 (4.0)\n", facets.getTopChildren(10, "a").toString()); iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #6
Source File: TestSortedSetDocValuesFacets.java From lucene-solr with Apache License 2.0 | 6 votes |
private static Facets getAllFacets(IndexSearcher searcher, SortedSetDocValuesReaderState state, ExecutorService exec) throws IOException, InterruptedException { if (random().nextBoolean()) { FacetsCollector c = new FacetsCollector(); searcher.search(new MatchAllDocsQuery(), c); if (exec != null) { return new ConcurrentSortedSetDocValuesFacetCounts(state, c, exec); } else { return new SortedSetDocValuesFacetCounts(state, c); } } else if (exec != null) { return new ConcurrentSortedSetDocValuesFacetCounts(state, exec); } else { return new SortedSetDocValuesFacetCounts(state); } }
Example #7
Source File: AssociationsFacetsExample.java From lucene-solr with Apache License 2.0 | 6 votes |
/** User drills down on 'tags/solr'. */ private FacetResult drillDown() throws IOException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); // Passing no baseQuery means we drill down on all // documents ("browse only"): DrillDownQuery q = new DrillDownQuery(config); // Now user drills down on Publish Date/2010: q.add("tags", "solr"); FacetsCollector fc = new FacetsCollector(); FacetsCollector.search(searcher, q, 10, fc); // Retrieve results Facets facets = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc); FacetResult result = facets.getTopChildren(10, "genre"); indexReader.close(); taxoReader.close(); return result; }
Example #8
Source File: AssociationsFacetsExample.java From lucene-solr with Apache License 2.0 | 6 votes |
/** User runs a query and aggregates facets by summing their association values. */ private List<FacetResult> sumAssociations() throws IOException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); 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: FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); Facets tags = new TaxonomyFacetSumIntAssociations("$tags", taxoReader, config, fc); Facets genre = new TaxonomyFacetSumFloatAssociations("$genre", taxoReader, config, fc); // Retrieve results List<FacetResult> results = new ArrayList<>(); results.add(tags.getTopChildren(10, "tags")); results.add(genre.getTopChildren(10, "genre")); indexReader.close(); taxoReader.close(); return results; }
Example #9
Source File: DistanceFacetsExample.java From lucene-solr with Apache License 2.0 | 6 votes |
/** User runs a query and counts facets. */ public FacetResult search() throws IOException { FacetsCollector fc = new FacetsCollector(); searcher.search(new MatchAllDocsQuery(), fc); Facets facets = new DoubleRangeFacetCounts("field", getDistanceValueSource(), fc, getBoundingBoxQuery(ORIGIN_LATITUDE, ORIGIN_LONGITUDE, 10.0), ONE_KM, TWO_KM, FIVE_KM, TEN_KM); return facets.getTopChildren(10, "field"); }
Example #10
Source File: SimpleSortedSetFacetsExample.java From lucene-solr with Apache License 2.0 | 6 votes |
/** User runs a query and counts facets. */ private List<FacetResult> search() throws IOException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader); // Aggregatses the facet counts 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: FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); // Retrieve results Facets facets = new SortedSetDocValuesFacetCounts(state, fc); List<FacetResult> results = new ArrayList<>(); results.add(facets.getTopChildren(10, "Author")); results.add(facets.getTopChildren(10, "Publish Year")); indexReader.close(); return results; }
Example #11
Source File: SimpleFacetsExample.java From lucene-solr with Apache License 2.0 | 6 votes |
/** User drills down on 'Publish Date/2010', and we * return facets for 'Author' */ private FacetResult drillDown() throws IOException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); // Passing no baseQuery means we drill down on all // documents ("browse only"): 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 Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc); FacetResult result = facets.getTopChildren(10, "Author"); indexReader.close(); taxoReader.close(); return result; }
Example #12
Source File: SimpleSortedSetFacetsExample.java From lucene-solr with Apache License 2.0 | 6 votes |
/** User drills down on 'Publish Year/2010'. */ private FacetResult drillDown() throws IOException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader); // Now user drills down on Publish Year/2010: DrillDownQuery q = new DrillDownQuery(config); q.add("Publish Year", "2010"); FacetsCollector fc = new FacetsCollector(); FacetsCollector.search(searcher, q, 10, fc); // Retrieve results Facets facets = new SortedSetDocValuesFacetCounts(state, fc); FacetResult result = facets.getTopChildren(10, "Author"); indexReader.close(); return result; }
Example #13
Source File: SimpleFacetsExample.java From lucene-solr with Apache License 2.0 | 6 votes |
/** User runs a query and counts facets. */ private List<FacetResult> facetsWithSearch() throws IOException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); 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: FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); // Retrieve results List<FacetResult> results = new ArrayList<>(); // 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 results; }
Example #14
Source File: RangeFacetsExample.java From lucene-solr with Apache License 2.0 | 6 votes |
/** User runs a query and counts facets. */ public FacetResult search() throws IOException { // Aggregates the facet counts 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: FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); Facets facets = new LongRangeFacetCounts("timestamp", fc, PAST_HOUR, PAST_SIX_HOURS, PAST_DAY); return facets.getTopChildren(10, "timestamp"); }
Example #15
Source File: TestTaxonomyFacetCounts.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testLabelWithDelimiter() throws Exception { Directory dir = newDirectory(); Directory taxoDir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE); FacetsConfig config = new FacetsConfig(); config.setMultiValued("dim", true); Document doc = new Document(); doc.add(newTextField("field", "text", Field.Store.NO)); doc.add(new FacetField("dim", "test\u001Fone")); doc.add(new FacetField("dim", "test\u001Etwo")); writer.addDocument(config.build(taxoWriter, doc)); // NRT open IndexSearcher searcher = newSearcher(writer.getReader()); // NRT open TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, searcher, taxoReader, config); assertEquals(1, facets.getSpecificValue("dim", "test\u001Fone")); assertEquals(1, facets.getSpecificValue("dim", "test\u001Etwo")); // no hierarchy assertFalse(((TaxonomyFacets) facets).siblingsLoaded()); assertFalse(((TaxonomyFacets) facets).childrenLoaded()); FacetResult result = facets.getTopChildren(10, "dim"); assertEquals("dim=dim path=[] value=-1 childCount=2\n test\u001Fone (1)\n test\u001Etwo (1)\n", result.toString()); writer.close(); IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, dir, taxoDir); }
Example #16
Source File: TestTaxonomyFacetCounts.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testManyFacetsInOneDocument() throws Exception { assumeTrue("default Codec doesn't support huge BinaryDocValues", TestUtil.fieldSupportsHugeBinaryDocValues(FacetsConfig.DEFAULT_INDEX_FIELD_NAME)); Directory dir = newDirectory(); Directory taxoDir = newDirectory(); IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random())); RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE); FacetsConfig config = new FacetsConfig(); config.setMultiValued("dim", true); int numLabels = TEST_NIGHTLY ? TestUtil.nextInt(random(), 40000, 100000) : TestUtil.nextInt(random(), 4000, 10000); Document doc = new Document(); doc.add(newTextField("field", "text", Field.Store.NO)); for (int i = 0; i < numLabels; i++) { doc.add(new FacetField("dim", "" + i)); } writer.addDocument(config.build(taxoWriter, doc)); // NRT open IndexSearcher searcher = newSearcher(writer.getReader()); // NRT open TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, searcher, taxoReader, config); FacetResult result = facets.getTopChildren(Integer.MAX_VALUE, "dim"); assertEquals(numLabels, result.labelValues.length); Set<String> allLabels = new HashSet<>(); for (LabelAndValue labelValue : result.labelValues) { allLabels.add(labelValue.label); assertEquals(1, labelValue.value.intValue()); } assertEquals(numLabels, allLabels.size()); writer.close(); IOUtils.close(searcher.getIndexReader(), taxoWriter, taxoReader, dir, taxoDir); }
Example #17
Source File: TestTaxonomyFacetCounts.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testSeparateIndexedFields() throws Exception { Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); config.setIndexFieldName("b", "$b"); for(int i = atLeast(30); i > 0; --i) { Document doc = new Document(); doc.add(new StringField("f", "v", Field.Store.NO)); doc.add(new FacetField("a", "1")); doc.add(new FacetField("b", "1")); iw.addDocument(config.build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector sfc = new FacetsCollector(); newSearcher(r).search(new MatchAllDocsQuery(), sfc); Facets facets1 = getTaxonomyFacetCounts(taxoReader, config, sfc); Facets facets2 = getTaxonomyFacetCounts(taxoReader, config, sfc, "$b"); assertEquals(r.maxDoc(), facets1.getTopChildren(10, "a").value.intValue()); assertEquals(r.maxDoc(), facets2.getTopChildren(10, "b").value.intValue()); iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #18
Source File: TestTaxonomyFacetCounts.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testCountRoot() throws Exception { // LUCENE-4882: FacetsAccumulator threw NPE if a FacetRequest was defined on CP.EMPTY Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); for(int i = atLeast(30); i > 0; --i) { Document doc = new Document(); doc.add(new FacetField("a", "1")); doc.add(new FacetField("b", "1")); iw.addDocument(config.build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); Facets facets = getAllFacets(FacetsConfig.DEFAULT_INDEX_FIELD_NAME, newSearcher(r), taxoReader, config); for (FacetResult result : facets.getAllDims(10)) { assertEquals(r.numDocs(), result.value.intValue()); } iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #19
Source File: ExpressionAggregationFacetsExample.java From lucene-solr with Apache License 2.0 | 5 votes |
/** User runs a query and aggregates facets. */ private FacetResult search() throws IOException, ParseException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); // Aggregate categories by an expression that combines the document's score // and its popularity field Expression expr = JavascriptCompiler.compile("_score * sqrt(popularity)"); SimpleBindings bindings = new SimpleBindings(); bindings.add("_score", DoubleValuesSource.SCORES); // the score of the document bindings.add("popularity", DoubleValuesSource.fromLongField("popularity")); // the value of the 'popularity' field // Aggregates the facet values FacetsCollector fc = new FacetsCollector(true); // MatchAllDocsQuery is for "browsing" (counts facets // for all non-deleted docs in the index); normally // you'd use a "normal" query: FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); // Retrieve results Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, expr.getDoubleValuesSource(bindings)); FacetResult result = facets.getTopChildren(10, "A"); indexReader.close(); taxoReader.close(); return result; }
Example #20
Source File: TestTaxonomyFacetCounts.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testSegmentsWithoutCategoriesOrResults() throws Exception { // tests the accumulator when there are segments with no results Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); IndexWriterConfig iwc = newIndexWriterConfig(new MockAnalyzer(random())); iwc.setMergePolicy(NoMergePolicy.INSTANCE); // prevent merges IndexWriter indexWriter = new IndexWriter(indexDir, iwc); TaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); FacetsConfig config = new FacetsConfig(); indexTwoDocs(taxoWriter, indexWriter, config, false); // 1st segment, no content, with categories indexTwoDocs(taxoWriter, indexWriter, null, true); // 2nd segment, with content, no categories indexTwoDocs(taxoWriter, indexWriter, config, true); // 3rd segment ok indexTwoDocs(taxoWriter, indexWriter, null, false); // 4th segment, no content, or categories indexTwoDocs(taxoWriter, indexWriter, null, true); // 5th segment, with content, no categories indexTwoDocs(taxoWriter, indexWriter, config, true); // 6th segment, with content, with categories indexTwoDocs(taxoWriter, indexWriter, null, true); // 7th segment, with content, no categories indexWriter.close(); IOUtils.close(taxoWriter); DirectoryReader indexReader = DirectoryReader.open(indexDir); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); IndexSearcher indexSearcher = newSearcher(indexReader); // search for "f:a", only segments 1 and 3 should match results Query q = new TermQuery(new Term("f", "a")); FacetsCollector sfc = new FacetsCollector(); indexSearcher.search(q, sfc); Facets facets = getTaxonomyFacetCounts(taxoReader, config, sfc); FacetResult result = facets.getTopChildren(10, "A"); assertEquals("wrong number of children", 2, result.labelValues.length); for (LabelAndValue labelValue : result.labelValues) { assertEquals("wrong weight for child " + labelValue.label, 2, labelValue.value.intValue()); } IOUtils.close(indexReader, taxoReader, indexDir, taxoDir); }
Example #21
Source File: FacetStorageTest.java From lumongo with Apache License 2.0 | 5 votes |
/** User runs a query and counts facets. */ private List<FacetResult> search() throws IOException { DirectoryReader indexReader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(indexReader); SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(indexReader); // Aggregates the facet counts 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: //FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc); TotalHitCountCollector collector = new TotalHitCountCollector(); searcher.search(new MatchAllDocsQuery(), MultiCollector.wrap(collector, fc)); // Retrieve results Facets facets = new SortedSetDocValuesFacetCounts(state, fc); List<FacetResult> results = new ArrayList<>(); results.add(facets.getTopChildren(10, "Author")); results.add(facets.getTopChildren(10, "Publish Year")); indexReader.close(); return results; }
Example #22
Source File: TestOrdinalMappingLeafReader.java From lucene-solr with Apache License 2.0 | 5 votes |
private void verifyResults(Directory indexDir, Directory taxoDir) throws IOException { DirectoryReader indexReader = DirectoryReader.open(indexDir); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); IndexSearcher searcher = newSearcher(indexReader); FacetsCollector collector = new FacetsCollector(); FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, collector); // tag facets Facets tagFacets = new FastTaxonomyFacetCounts("$tags", taxoReader, facetConfig, collector); FacetResult result = tagFacets.getTopChildren(10, "tag"); for (LabelAndValue lv: result.labelValues) { if (VERBOSE) { System.out.println(lv); } assertEquals(NUM_DOCS, lv.value.intValue()); } // id facets Facets idFacets = new FastTaxonomyFacetCounts(taxoReader, facetConfig, collector); FacetResult idResult = idFacets.getTopChildren(10, "id"); assertEquals(NUM_DOCS, idResult.childCount); assertEquals(NUM_DOCS * 2, idResult.value); // each "id" appears twice BinaryDocValues bdv = MultiDocValues.getBinaryValues(indexReader, "bdv"); BinaryDocValues cbdv = MultiDocValues.getBinaryValues(indexReader, "cbdv"); for (int i = 0; i < indexReader.maxDoc(); i++) { assertEquals(i, bdv.nextDoc()); assertEquals(i, cbdv.nextDoc()); assertEquals(Integer.parseInt(cbdv.binaryValue().utf8ToString()), Integer.parseInt(bdv.binaryValue().utf8ToString())*2); } IOUtils.close(indexReader, taxoReader); }
Example #23
Source File: TestTaxonomyFacetSumValueSource.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testSumScoreAggregator() throws Exception { Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); for(int i = atLeast(30); i > 0; --i) { Document doc = new Document(); if (random().nextBoolean()) { // don't match all documents doc.add(new StringField("f", "v", Field.Store.NO)); } doc.add(new FacetField("dim", "a")); iw.addDocument(config.build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector fc = new FacetsCollector(true); BoostQuery csq = new BoostQuery(new ConstantScoreQuery(new MatchAllDocsQuery()), 2f); TopDocs td = FacetsCollector.search(newSearcher(r), csq, 10, fc); Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES); int expected = (int) (csq.getBoost() * td.totalHits.value); assertEquals(expected, facets.getSpecificValue("dim", "a").intValue()); iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #24
Source File: TestTaxonomyFacetSumValueSource.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testWithScore() throws Exception { Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); for (int i = 0; i < 4; i++) { Document doc = new Document(); doc.add(new NumericDocValuesField("price", (i+1))); doc.add(new FacetField("a", Integer.toString(i % 2))); iw.addDocument(config.build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector fc = new FacetsCollector(true); // score documents by their 'price' field - makes asserting the correct counts for the categories easier Query q = new FunctionQuery(new LongFieldSource("price")); FacetsCollector.search(newSearcher(r), q, 10, fc); Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES); assertEquals("dim=a path=[] value=10.0 childCount=2\n 1 (6.0)\n 0 (4.0)\n", facets.getTopChildren(10, "a").toString()); iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #25
Source File: TestTaxonomyFacetSumValueSource.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testCountAndSumScore() throws Exception { Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); config.setIndexFieldName("b", "$b"); for(int i = atLeast(30); i > 0; --i) { Document doc = new Document(); doc.add(new StringField("f", "v", Field.Store.NO)); doc.add(new FacetField("a", "1")); doc.add(new FacetField("b", "1")); iw.addDocument(config.build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector fc = new FacetsCollector(true); FacetsCollector.search(newSearcher(r), new MatchAllDocsQuery(), 10, fc); Facets facets1 = getTaxonomyFacetCounts(taxoReader, config, fc); Facets facets2 = new TaxonomyFacetSumValueSource(new DocValuesOrdinalsReader("$b"), taxoReader, config, fc, DoubleValuesSource.SCORES); assertEquals(r.maxDoc(), facets1.getTopChildren(10, "a").value.intValue()); assertEquals(r.maxDoc(), facets2.getTopChildren(10, "b").value.doubleValue(), 1E-10); iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #26
Source File: TestTaxonomyFacetSumValueSource.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testRollupValues() throws Exception { Directory indexDir = newDirectory(); Directory taxoDir = newDirectory(); DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir); IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random()))); FacetsConfig config = new FacetsConfig(); config.setHierarchical("a", true); //config.setRequireDimCount("a", true); for (int i = 0; i < 4; i++) { Document doc = new Document(); doc.add(new NumericDocValuesField("price", (i+1))); doc.add(new FacetField("a", Integer.toString(i % 2), "1")); iw.addDocument(config.build(taxoWriter, doc)); } DirectoryReader r = DirectoryReader.open(iw); DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter); FacetsCollector sfc = new FacetsCollector(); newSearcher(r).search(new MatchAllDocsQuery(), sfc); Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, sfc, DoubleValuesSource.fromLongField("price")); assertEquals("dim=a path=[] value=10.0 childCount=2\n 1 (6.0)\n 0 (4.0)\n", facets.getTopChildren(10, "a").toString()); iw.close(); IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir); }
Example #27
Source File: TestRangeFacetCounts.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testBasicDouble() throws Exception { Directory d = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), d); Document doc = new Document(); DoubleDocValuesField field = new DoubleDocValuesField("field", 0.0); doc.add(field); for(long l=0;l<100;l++) { field.setDoubleValue(l); w.addDocument(doc); } IndexReader r = w.getReader(); FacetsCollector fc = new FacetsCollector(); IndexSearcher s = newSearcher(r); s.search(new MatchAllDocsQuery(), fc); Facets facets = new DoubleRangeFacetCounts("field", fc, new DoubleRange("less than 10", 0.0, true, 10.0, false), new DoubleRange("less than or equal to 10", 0.0, true, 10.0, true), new DoubleRange("over 90", 90.0, false, 100.0, false), new DoubleRange("90 or above", 90.0, true, 100.0, false), new DoubleRange("over 1000", 1000.0, false, Double.POSITIVE_INFINITY, false)); assertEquals("dim=field path=[] value=21 childCount=5\n less than 10 (10)\n less than or equal to 10 (11)\n over 90 (9)\n 90 or above (10)\n over 1000 (0)\n", facets.getTopChildren(10, "field").toString()); w.close(); IOUtils.close(r, d); }
Example #28
Source File: SimpleFacetsExample.java From lucene-solr with Apache License 2.0 | 5 votes |
/** User runs a query and counts facets only without collecting the matching documents.*/ private List<FacetResult> facetsOnly() throws IOException { DirectoryReader indexReader = DirectoryReader.open(indexDir); IndexSearcher searcher = new IndexSearcher(indexReader); TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir); 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: searcher.search(new MatchAllDocsQuery(), fc); // Retrieve results List<FacetResult> results = new ArrayList<>(); // 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 results; }
Example #29
Source File: IndexAndTaxonomyReplicationClientTest.java From lucene-solr with Apache License 2.0 | 5 votes |
@Override public Boolean call() throws Exception { if (indexReader == null) { indexReader = DirectoryReader.open(indexDir); lastIndexGeneration = indexReader.getIndexCommit().getGeneration(); taxoReader = new DirectoryTaxonomyReader(taxoDir); } else { // verify search index DirectoryReader newReader = DirectoryReader.openIfChanged(indexReader); assertNotNull("should not have reached here if no changes were made to the index", newReader); long newGeneration = newReader.getIndexCommit().getGeneration(); assertTrue("expected newer generation; current=" + lastIndexGeneration + " new=" + newGeneration, newGeneration > lastIndexGeneration); indexReader.close(); indexReader = newReader; lastIndexGeneration = newGeneration; TestUtil.checkIndex(indexDir); // verify taxonomy index DirectoryTaxonomyReader newTaxoReader = TaxonomyReader.openIfChanged(taxoReader); if (newTaxoReader != null) { taxoReader.close(); taxoReader = newTaxoReader; } TestUtil.checkIndex(taxoDir); // verify faceted search int id = Integer.parseInt(indexReader.getIndexCommit().getUserData().get(VERSION_ID), 16); IndexSearcher searcher = new IndexSearcher(indexReader); FacetsCollector fc = new FacetsCollector(); searcher.search(new MatchAllDocsQuery(), fc); Facets facets = new FastTaxonomyFacetCounts(taxoReader, config, fc); assertEquals(1, facets.getSpecificValue("A", Integer.toString(id, 16)).intValue()); DrillDownQuery drillDown = new DrillDownQuery(config); drillDown.add("A", Integer.toString(id, 16)); TopDocs docs = searcher.search(drillDown, 10); assertEquals(1, docs.totalHits.value); } return null; }
Example #30
Source File: TestRangeFacetCounts.java From lucene-solr with Apache License 2.0 | 5 votes |
public void testBasicLong() throws Exception { Directory d = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), d); Document doc = new Document(); NumericDocValuesField field = new NumericDocValuesField("field", 0L); doc.add(field); for(long l=0;l<100;l++) { field.setLongValue(l); w.addDocument(doc); } // Also add Long.MAX_VALUE field.setLongValue(Long.MAX_VALUE); w.addDocument(doc); IndexReader r = w.getReader(); w.close(); FacetsCollector fc = new FacetsCollector(); IndexSearcher s = newSearcher(r); s.search(new MatchAllDocsQuery(), fc); Facets facets = new LongRangeFacetCounts("field", fc, new LongRange("less than 10", 0L, true, 10L, false), new LongRange("less than or equal to 10", 0L, true, 10L, true), new LongRange("over 90", 90L, false, 100L, false), new LongRange("90 or above", 90L, true, 100L, false), new LongRange("over 1000", 1000L, false, Long.MAX_VALUE, true)); FacetResult result = facets.getTopChildren(10, "field"); assertEquals("dim=field path=[] value=22 childCount=5\n less than 10 (10)\n less than or equal to 10 (11)\n over 90 (9)\n 90 or above (10)\n over 1000 (1)\n", result.toString()); r.close(); d.close(); }