Search in sources :

Example 21 with Facets

use of org.apache.lucene.facet.Facets in project lucene-solr by apache.

the class TestTaxonomyFacetCounts method testRequireDimCount.

public void testRequireDimCount() 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.setRequireDimCount("dim", true);
    config.setMultiValued("dim2", true);
    config.setRequireDimCount("dim2", true);
    config.setMultiValued("dim3", true);
    config.setHierarchical("dim3", true);
    config.setRequireDimCount("dim3", true);
    Document doc = new Document();
    doc.add(newTextField("field", "text", Field.Store.NO));
    doc.add(new FacetField("dim", "a"));
    doc.add(new FacetField("dim2", "a"));
    doc.add(new FacetField("dim2", "b"));
    doc.add(new FacetField("dim3", "a", "b"));
    doc.add(new FacetField("dim3", "a", "c"));
    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.getTopChildren(10, "dim").value);
    assertEquals(1, facets.getTopChildren(10, "dim2").value);
    assertEquals(1, facets.getTopChildren(10, "dim3").value);
    expectThrows(IllegalArgumentException.class, () -> {
        facets.getSpecificValue("dim");
    });
    assertEquals(1, facets.getSpecificValue("dim2"));
    assertEquals(1, facets.getSpecificValue("dim3"));
    writer.close();
    IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, dir, taxoDir);
}
Also used : DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) IndexSearcher(org.apache.lucene.search.IndexSearcher) FacetsConfig(org.apache.lucene.facet.FacetsConfig) Facets(org.apache.lucene.facet.Facets) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Example 22 with Facets

use of org.apache.lucene.facet.Facets in project lucene-solr by apache.

the class TestTaxonomyFacetSumValueSource method testRandom.

public void testRandom() throws Exception {
    String[] tokens = getRandomTokens(10);
    Directory indexDir = newDirectory();
    Directory taxoDir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), indexDir);
    DirectoryTaxonomyWriter tw = new DirectoryTaxonomyWriter(taxoDir);
    FacetsConfig config = new FacetsConfig();
    int numDocs = atLeast(1000);
    int numDims = TestUtil.nextInt(random(), 1, 7);
    List<TestDoc> testDocs = getRandomDocs(tokens, numDocs, numDims);
    for (TestDoc testDoc : testDocs) {
        Document doc = new Document();
        doc.add(newStringField("content", testDoc.content, Field.Store.NO));
        testDoc.value = random().nextFloat();
        doc.add(new FloatDocValuesField("value", testDoc.value));
        for (int j = 0; j < numDims; j++) {
            if (testDoc.dims[j] != null) {
                doc.add(new FacetField("dim" + j, testDoc.dims[j]));
            }
        }
        w.addDocument(config.build(tw, doc));
    }
    // NRT open
    IndexSearcher searcher = newSearcher(w.getReader());
    // NRT open
    TaxonomyReader tr = new DirectoryTaxonomyReader(tw);
    int iters = atLeast(100);
    for (int iter = 0; iter < iters; iter++) {
        String searchToken = tokens[random().nextInt(tokens.length)];
        if (VERBOSE) {
            System.out.println("\nTEST: iter content=" + searchToken);
        }
        FacetsCollector fc = new FacetsCollector();
        FacetsCollector.search(searcher, new TermQuery(new Term("content", searchToken)), 10, fc);
        Facets facets = new TaxonomyFacetSumValueSource(tr, config, fc, DoubleValuesSource.fromFloatField("value"));
        // Slow, yet hopefully bug-free, faceting:
        @SuppressWarnings({ "rawtypes", "unchecked" }) Map<String, Float>[] expectedValues = new HashMap[numDims];
        for (int i = 0; i < numDims; i++) {
            expectedValues[i] = new HashMap<>();
        }
        for (TestDoc doc : testDocs) {
            if (doc.content.equals(searchToken)) {
                for (int j = 0; j < numDims; j++) {
                    if (doc.dims[j] != null) {
                        Float v = expectedValues[j].get(doc.dims[j]);
                        if (v == null) {
                            expectedValues[j].put(doc.dims[j], doc.value);
                        } else {
                            expectedValues[j].put(doc.dims[j], v + doc.value);
                        }
                    }
                }
            }
        }
        List<FacetResult> expected = new ArrayList<>();
        for (int i = 0; i < numDims; i++) {
            List<LabelAndValue> labelValues = new ArrayList<>();
            double totValue = 0;
            for (Map.Entry<String, Float> ent : expectedValues[i].entrySet()) {
                labelValues.add(new LabelAndValue(ent.getKey(), ent.getValue()));
                totValue += ent.getValue();
            }
            sortLabelValues(labelValues);
            if (totValue > 0) {
                expected.add(new FacetResult("dim" + i, new String[0], totValue, labelValues.toArray(new LabelAndValue[labelValues.size()]), labelValues.size()));
            }
        }
        // Sort by highest value, tie break by value:
        sortFacetResults(expected);
        List<FacetResult> actual = facets.getAllDims(10);
        // Messy: fixup ties
        sortTies(actual);
        if (VERBOSE) {
            System.out.println("expected=\n" + expected.toString());
            System.out.println("actual=\n" + actual.toString());
        }
        assertFloatValuesEquals(expected, actual);
    }
    w.close();
    IOUtils.close(tw, searcher.getIndexReader(), tr, indexDir, taxoDir);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FloatDocValuesField(org.apache.lucene.document.FloatDocValuesField) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) LabelAndValue(org.apache.lucene.facet.LabelAndValue) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) TermQuery(org.apache.lucene.search.TermQuery) FacetsConfig(org.apache.lucene.facet.FacetsConfig) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) Term(org.apache.lucene.index.Term) FacetsCollector(org.apache.lucene.facet.FacetsCollector) FacetResult(org.apache.lucene.facet.FacetResult) HashMap(java.util.HashMap) Map(java.util.Map) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 23 with Facets

use of org.apache.lucene.facet.Facets in project lucene-solr by apache.

the class TestTaxonomyFacetCounts2 method testAllCounts.

@Test
public void testAllCounts() throws Exception {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    IndexSearcher searcher = newSearcher(indexReader);
    FacetsCollector sfc = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), sfc);
    Facets facets = getTaxonomyFacetCounts(taxoReader, getConfig(), sfc);
    FacetResult result = facets.getTopChildren(NUM_CHILDREN_CP_A, CP_A);
    assertEquals(-1, result.value.intValue());
    int prevValue = Integer.MAX_VALUE;
    for (LabelAndValue labelValue : result.labelValues) {
        assertEquals(allExpectedCounts.get(CP_A + "/" + labelValue.label), labelValue.value);
        assertTrue("wrong sort order of sub results: labelValue.value=" + labelValue.value + " prevValue=" + prevValue, labelValue.value.intValue() <= prevValue);
        prevValue = labelValue.value.intValue();
    }
    result = facets.getTopChildren(NUM_CHILDREN_CP_B, CP_B);
    assertEquals(allExpectedCounts.get(CP_B), result.value);
    prevValue = Integer.MAX_VALUE;
    for (LabelAndValue labelValue : result.labelValues) {
        assertEquals(allExpectedCounts.get(CP_B + "/" + labelValue.label), labelValue.value);
        assertTrue("wrong sort order of sub results: labelValue.value=" + labelValue.value + " prevValue=" + prevValue, labelValue.value.intValue() <= prevValue);
        prevValue = labelValue.value.intValue();
    }
    IOUtils.close(indexReader, taxoReader);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetResult(org.apache.lucene.facet.FacetResult) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LabelAndValue(org.apache.lucene.facet.LabelAndValue) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetsCollector(org.apache.lucene.facet.FacetsCollector) Test(org.junit.Test)

Example 24 with Facets

use of org.apache.lucene.facet.Facets in project lucene-solr by apache.

the class TestTaxonomyFacetSumValueSource method testNoScore.

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);
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FacetsCollector(org.apache.lucene.facet.FacetsCollector) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Example 25 with Facets

use of org.apache.lucene.facet.Facets in project lucene-solr by apache.

the class TestTaxonomyFacetCounts2 method testBigNumResults.

@Test
public void testBigNumResults() throws Exception {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    IndexSearcher searcher = newSearcher(indexReader);
    FacetsCollector sfc = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), sfc);
    Facets facets = getTaxonomyFacetCounts(taxoReader, getConfig(), sfc);
    FacetResult result = facets.getTopChildren(Integer.MAX_VALUE, CP_A);
    assertEquals(-1, result.value.intValue());
    for (LabelAndValue labelValue : result.labelValues) {
        assertEquals(allExpectedCounts.get(CP_A + "/" + labelValue.label), labelValue.value);
    }
    result = facets.getTopChildren(Integer.MAX_VALUE, CP_B);
    assertEquals(allExpectedCounts.get(CP_B), result.value);
    for (LabelAndValue labelValue : result.labelValues) {
        assertEquals(allExpectedCounts.get(CP_B + "/" + labelValue.label), labelValue.value);
    }
    IOUtils.close(indexReader, taxoReader);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetResult(org.apache.lucene.facet.FacetResult) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LabelAndValue(org.apache.lucene.facet.LabelAndValue) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetsCollector(org.apache.lucene.facet.FacetsCollector) Test(org.junit.Test)

Aggregations

Facets (org.apache.lucene.facet.Facets)72 FacetsCollector (org.apache.lucene.facet.FacetsCollector)60 FacetResult (org.apache.lucene.facet.FacetResult)47 IndexSearcher (org.apache.lucene.search.IndexSearcher)46 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)37 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)35 Directory (org.apache.lucene.store.Directory)34 Document (org.apache.lucene.document.Document)32 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)32 FacetsConfig (org.apache.lucene.facet.FacetsConfig)29 DirectoryReader (org.apache.lucene.index.DirectoryReader)27 LabelAndValue (org.apache.lucene.facet.LabelAndValue)23 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)22 FacetField (org.apache.lucene.facet.FacetField)19 ArrayList (java.util.ArrayList)18 IOException (java.io.IOException)14 DefaultSortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState)14 SortedSetDocValuesFacetCounts (org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts)14 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)13 DrillDownQuery (org.apache.lucene.facet.DrillDownQuery)13