Search in sources :

Example 11 with LabelAndValue

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

the class TestTaxonomyFacetCounts2 method testDifferentNumResults.

@Test
public void testDifferentNumResults() throws Exception {
    // test the collector w/ FacetRequests and different numResults
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);
    IndexSearcher searcher = newSearcher(indexReader);
    FacetsCollector sfc = new FacetsCollector();
    TermQuery q = new TermQuery(A);
    searcher.search(q, sfc);
    Facets facets = getTaxonomyFacetCounts(taxoReader, getConfig(), sfc);
    FacetResult result = facets.getTopChildren(NUM_CHILDREN_CP_A, CP_A);
    assertEquals(-1, result.value.intValue());
    for (LabelAndValue labelValue : result.labelValues) {
        assertEquals(termExpectedCounts.get(CP_A + "/" + labelValue.label), labelValue.value);
    }
    result = facets.getTopChildren(NUM_CHILDREN_CP_B, CP_B);
    assertEquals(termExpectedCounts.get(CP_B), result.value);
    for (LabelAndValue labelValue : result.labelValues) {
        assertEquals(termExpectedCounts.get(CP_B + "/" + labelValue.label), labelValue.value);
    }
    IOUtils.close(indexReader, taxoReader);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) 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) 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 12 with LabelAndValue

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

the class TimeCodeSearcher method getYearMonthDayFacetResults.

/**
     * Returns the matching yearmonthday facets for a given query
     */
private List<CodeFacetYearMonthDay> getYearMonthDayFacetResults(IndexSearcher searcher, IndexReader reader, Query query) {
    List<CodeFacetYearMonthDay> codeFacetYearMonthDay = new ArrayList<>();
    try {
        SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(reader, Values.DATEYEARMONTHDAY);
        FacetsCollector fc = new FacetsCollector();
        FacetsCollector.search(searcher, query, 10, fc);
        Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
        FacetResult result = facets.getTopChildren(200, Values.DATEYEARMONTHDAY);
        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) {
                    codeFacetYearMonthDay.add(new CodeFacetYearMonthDay(lv.label, lv.value.intValue()));
                }
            }
        }
    } catch (IOException ex) {
        LOGGER.warning(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    } catch (Exception ex) {
        LOGGER.warning(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    }
    return codeFacetYearMonthDay;
}
Also used : SortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState) DefaultSortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState) Facets(org.apache.lucene.facet.Facets) ArrayList(java.util.ArrayList) 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)

Example 13 with LabelAndValue

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

the class CodeSearcher method getRepoFacetResults.

/**
     * Returns the matching repository facets for a given query
     */
private List<CodeFacetRepo> getRepoFacetResults(IndexSearcher searcher, IndexReader reader, Query query) {
    List<CodeFacetRepo> codeFacetRepo = new ArrayList<>();
    try {
        SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(reader, Values.REPONAME);
        FacetsCollector fc = new FacetsCollector();
        FacetsCollector.search(searcher, query, 10, fc);
        Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
        FacetResult result = facets.getTopChildren(200, Values.REPONAME);
        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) {
                    codeFacetRepo.add(new CodeFacetRepo(lv.label, lv.value.intValue()));
                }
            }
        }
    } catch (IOException ex) {
    } catch (Exception ex) {
    }
    return codeFacetRepo;
}
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)

Example 14 with LabelAndValue

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

the class TimeCodeSearcher method getRepoFacetResults.

/**
     * Returns the matching repository facets for a given query
     */
private List<CodeFacetRepo> getRepoFacetResults(IndexSearcher searcher, IndexReader reader, Query query) {
    List<CodeFacetRepo> codeFacetRepo = new ArrayList<>();
    try {
        SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(reader, Values.REPONAME);
        FacetsCollector fc = new FacetsCollector();
        FacetsCollector.search(searcher, query, 10, fc);
        Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
        FacetResult result = facets.getTopChildren(200, Values.REPONAME);
        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) {
                    codeFacetRepo.add(new CodeFacetRepo(lv.label, lv.value.intValue()));
                }
            }
        }
    } catch (IOException ex) {
        LOGGER.warning(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    } catch (Exception ex) {
        LOGGER.warning(" caught a " + ex.getClass() + "\n with message: " + ex.getMessage());
    }
    return codeFacetRepo;
}
Also used : SortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState) DefaultSortedSetDocValuesReaderState(org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState) Facets(org.apache.lucene.facet.Facets) ArrayList(java.util.ArrayList) 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)

Example 15 with LabelAndValue

use of org.apache.lucene.facet.LabelAndValue in project jackrabbit-oak by apache.

the class FilteredSortedSetDocValuesFacetCounts method filterFacet.

private LabelAndValue[] filterFacet(int docId, String dimension, LabelAndValue[] labelAndValues) throws IOException {
    boolean filterd = false;
    Map<String, Long> newValues = new HashMap<String, Long>();
    Document document = reader.document(docId);
    SortedSetDocValues docValues = state.getDocValues();
    docValues.setDocument(docId);
    // filter using doc values (avoiding requiring stored values)
    if (!filter.isAccessible(document.getField(FieldNames.PATH).stringValue() + "/" + dimension)) {
        filterd = true;
        for (LabelAndValue lv : labelAndValues) {
            long existingCount = lv.value.longValue();
            BytesRef key = new BytesRef(FacetsConfig.pathToString(dimension, new String[] { lv.label }));
            long l = docValues.lookupTerm(key);
            if (l >= 0) {
                if (existingCount > 0) {
                    newValues.put(lv.label, existingCount - 1);
                } else {
                    if (newValues.containsKey(lv.label)) {
                        newValues.remove(lv.label);
                    }
                }
            }
        }
    }
    LabelAndValue[] filteredLVs;
    if (filterd) {
        filteredLVs = new LabelAndValue[newValues.size()];
        int i = 0;
        for (Map.Entry<String, Long> entry : newValues.entrySet()) {
            filteredLVs[i] = new LabelAndValue(entry.getKey(), entry.getValue());
            i++;
        }
    } else {
        filteredLVs = labelAndValues;
    }
    return filteredLVs;
}
Also used : HashMap(java.util.HashMap) Document(org.apache.lucene.document.Document) LabelAndValue(org.apache.lucene.facet.LabelAndValue) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) HashMap(java.util.HashMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

LabelAndValue (org.apache.lucene.facet.LabelAndValue)29 FacetResult (org.apache.lucene.facet.FacetResult)28 Facets (org.apache.lucene.facet.Facets)23 FacetsCollector (org.apache.lucene.facet.FacetsCollector)22 IndexSearcher (org.apache.lucene.search.IndexSearcher)12 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 DefaultSortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.DefaultSortedSetDocValuesReaderState)11 SortedSetDocValuesFacetCounts (org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts)11 SortedSetDocValuesReaderState (org.apache.lucene.facet.sortedset.SortedSetDocValuesReaderState)11 DirectoryTaxonomyReader (org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)9 Document (org.apache.lucene.document.Document)7 FacetsConfig (org.apache.lucene.facet.FacetsConfig)7 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)7 Directory (org.apache.lucene.store.Directory)7 DirectoryReader (org.apache.lucene.index.DirectoryReader)6 TermQuery (org.apache.lucene.search.TermQuery)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4