Search in sources :

Example 56 with FacetResult

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

the class TestSimpleFacetsExample method testDrillDown.

@Test
public void testDrillDown() throws Exception {
    FacetResult result = new SimpleFacetsExample().runDrillDown();
    assertEquals("dim=Author path=[] value=2 childCount=2\n  Bob (1)\n  Lisa (1)\n", result.toString());
}
Also used : FacetResult(org.apache.lucene.facet.FacetResult) Test(org.junit.Test)

Example 57 with FacetResult

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

the class AssociationsFacetsExample method sumAssociations.

/** 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;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) ArrayList(java.util.ArrayList) TaxonomyFacetSumIntAssociations(org.apache.lucene.facet.taxonomy.TaxonomyFacetSumIntAssociations) FacetResult(org.apache.lucene.facet.FacetResult) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TaxonomyFacetSumFloatAssociations(org.apache.lucene.facet.taxonomy.TaxonomyFacetSumFloatAssociations) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Example 58 with FacetResult

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

the class AssociationsFacetsExample method drillDown.

/** 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;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) FacetResult(org.apache.lucene.facet.FacetResult) TaxonomyFacetSumFloatAssociations(org.apache.lucene.facet.taxonomy.TaxonomyFacetSumFloatAssociations) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Example 59 with FacetResult

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

the class ExpressionAggregationFacetsExample method search.

/** 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();
    // the score of the document
    bindings.add(new SortField("_score", SortField.Type.SCORE));
    // the value of the 'popularity' field
    bindings.add(new SortField("popularity", SortField.Type.LONG));
    // 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;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) TaxonomyReader(org.apache.lucene.facet.taxonomy.TaxonomyReader) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) Expression(org.apache.lucene.expressions.Expression) SimpleBindings(org.apache.lucene.expressions.SimpleBindings) SortField(org.apache.lucene.search.SortField) TaxonomyFacetSumValueSource(org.apache.lucene.facet.taxonomy.TaxonomyFacetSumValueSource) FacetResult(org.apache.lucene.facet.FacetResult) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader) FacetsCollector(org.apache.lucene.facet.FacetsCollector)

Example 60 with FacetResult

use of org.apache.lucene.facet.FacetResult in project searchcode-server by boyter.

the class CodeSearcher method getLanguageFacetResults.

/**
     * Returns the matching language facets for a given query
     */
private List<CodeFacetLanguage> getLanguageFacetResults(IndexSearcher searcher, IndexReader reader, Query query) {
    List<CodeFacetLanguage> codeFacetLanguages = new ArrayList<>();
    try {
        SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(reader, Values.LANGUAGENAME);
        FacetsCollector fc = new FacetsCollector();
        FacetsCollector.search(searcher, query, 10, fc);
        Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
        FacetResult result = facets.getTopChildren(200, Values.LANGUAGENAME);
        if (result != null) {
            int stepThru = result.childCount > 200 ? 200 : result.childCount;
            for (int i = 0; i < stepThru; i++) {
                LabelAndValue lv = result.labelValues[i];
                if (lv != null && lv.value != null) {
                    codeFacetLanguages.add(new CodeFacetLanguage(lv.label, lv.value.intValue()));
                }
            }
        }
    } catch (IOException ex) {
    } catch (Exception ex) {
    }
    return codeFacetLanguages;
}
Also used : SortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState) DefaultSortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState) Facets(org.apache.lucene.facet.Facets) IOException(java.io.IOException) LabelAndValue(org.apache.lucene.facet.LabelAndValue) IOException(java.io.IOException) FacetsCollector(org.apache.lucene.facet.FacetsCollector) DefaultSortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState) SortedSetDocValuesFacetCounts(org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts) FacetResult(org.apache.lucene.facet.FacetResult)

Aggregations

FacetResult (org.apache.lucene.facet.FacetResult)68 Facets (org.apache.lucene.facet.Facets)47 FacetsCollector (org.apache.lucene.facet.FacetsCollector)42 IndexSearcher (org.apache.lucene.search.IndexSearcher)36 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)29 LabelAndValue (org.apache.lucene.facet.LabelAndValue)28 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)23 DirectoryReader (org.apache.lucene.index.DirectoryReader)22 ArrayList (java.util.ArrayList)21 Directory (org.apache.lucene.store.Directory)21 FacetsConfig (org.apache.lucene.facet.FacetsConfig)19 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)19 Document (org.apache.lucene.document.Document)18 DirectoryTaxonomyWriter (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter)14 DefaultSortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState)13 SortedSetDocValuesFacetCounts (org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts)13 SortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState)13 IOException (java.io.IOException)12 TaxonomyReader (org.apache.lucene.facet.taxonomy.TaxonomyReader)12 FacetField (org.apache.lucene.facet.FacetField)11