Example usage for org.apache.lucene.facet.sortedset SortedSetDocValuesFacetCounts SortedSetDocValuesFacetCounts

List of usage examples for org.apache.lucene.facet.sortedset SortedSetDocValuesFacetCounts SortedSetDocValuesFacetCounts

Introduction

In this page you can find the example usage for org.apache.lucene.facet.sortedset SortedSetDocValuesFacetCounts SortedSetDocValuesFacetCounts.

Prototype

public SortedSetDocValuesFacetCounts(SortedSetDocValuesReaderState state, FacetsCollector hits)
        throws IOException 

Source Link

Document

Counts all facet dimensions across the provided hits.

Usage

From source file:com.czw.search.lucene.example.facet.SimpleSortedSetFacetsExample.java

License:Apache License

/**
 * User runs a query and counts facets./*w w w . ja v  a2 s.  c  om*/
 */
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;
}

From source file:com.czw.search.lucene.example.facet.SimpleSortedSetFacetsExample.java

License:Apache License

/**
 * User drills down on 'Publish Year/2010'.
 *//*from  ww w.jav a  2s. c  o  m*/
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;
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

public int getTotalVariationsCountFacet(List<? extends FeatureFile> files, Query query) throws IOException {
    if (CollectionUtils.isEmpty(files)) {
        return 0;
    }/*from w  w  w.  ja va 2  s  .co  m*/

    SimpleFSDirectory[] indexes = fileManager.getIndexesForFiles(files);

    try (MultiReader reader = openMultiReader(indexes)) {
        if (reader.numDocs() == 0) {
            return 0;
        }

        FacetsCollector facetsCollector = new FacetsCollector();
        IndexSearcher searcher = new IndexSearcher(reader);
        searcher.search(query, facetsCollector);

        Facets facets = new SortedSetDocValuesFacetCounts(
                new DefaultSortedSetDocValuesReaderState(reader, FeatureIndexFields.FACET_UID.fieldName),
                facetsCollector);
        FacetResult res = facets.getTopChildren(reader.numDocs(), FeatureIndexFields.F_UID.getFieldName());
        if (res == null) {
            return 0;
        }

        return res.childCount;
    } finally {
        for (SimpleFSDirectory index : indexes) {
            IOUtils.closeQuietly(index);
        }
    }
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

/**
 * Returns a {@code List} of chromosome IDs for a project, specified by ID, where variations exist and satisfy a
 * specified query//from   ww  w .j ava 2  s .c o m
 *
 * @param projectId an ID of a project, which index to query
 * @param query     a query to filter variations
 * @return a {@code List} of chromosome IDs
 * @throws IOException
 */
public List<Long> getChromosomeIdsWhereVariationsPresentFacet(long projectId, Query query) throws IOException {
    List<Long> chromosomeIds = new ArrayList<>();

    try (Directory index = fileManager.getIndexForProject(projectId);
            IndexReader reader = DirectoryReader.open(index)) {
        if (reader.numDocs() == 0) {
            return Collections.emptyList();
        }

        FacetsCollector facetsCollector = new FacetsCollector();
        IndexSearcher searcher = new IndexSearcher(reader);
        searcher.search(query, facetsCollector);

        Facets facets = new SortedSetDocValuesFacetCounts(new DefaultSortedSetDocValuesReaderState(reader,
                FeatureIndexFields.FACET_CHR_ID.getFieldName()), facetsCollector);
        FacetResult res = facets.getTopChildren(FACET_LIMIT, FeatureIndexFields.CHR_ID.getFieldName());
        if (res == null) {
            return Collections.emptyList();
        }

        for (LabelAndValue labelAndValue : res.labelValues) {
            chromosomeIds.add(Long.parseLong(labelAndValue.label));
        }
    }

    return chromosomeIds;
}

From source file:com.epam.catgenome.dao.index.FeatureIndexDao.java

License:Open Source License

/**
 * Returns a {@code List} of chromosome IDs from specified files, where variations exist and satisfy a
 * specified query/* www.  ja va2  s  .c  o  m*/
 *
 * @param files a list of {@link FeatureFile}s to search chromosomes
 * @param query     a query to filter variations
 * @return a {@code List} of chromosome IDs
 * @throws IOException
 */
public List<Long> getChromosomeIdsWhereVariationsPresentFacet(List<? extends FeatureFile> files, Query query)
        throws IOException {
    if (CollectionUtils.isEmpty(files)) {
        return Collections.emptyList();
    }

    List<Long> chromosomeIds = new ArrayList<>();

    SimpleFSDirectory[] indexes = fileManager.getIndexesForFiles(files);

    try (MultiReader reader = openMultiReader(indexes)) {
        if (reader.numDocs() == 0) {
            return Collections.emptyList();
        }

        FacetsCollector facetsCollector = new FacetsCollector();
        IndexSearcher searcher = new IndexSearcher(reader);
        searcher.search(query, facetsCollector);

        Facets facets = new SortedSetDocValuesFacetCounts(new DefaultSortedSetDocValuesReaderState(reader,
                FeatureIndexFields.FACET_CHR_ID.getFieldName()), facetsCollector);
        FacetResult res = facets.getTopChildren(FACET_LIMIT, FeatureIndexFields.CHR_ID.getFieldName());
        if (res == null) {
            return Collections.emptyList();
        }

        for (LabelAndValue labelAndValue : res.labelValues) {
            chromosomeIds.add(Long.parseLong(labelAndValue.label));
        }
    } finally {
        closeIndexes(indexes);
    }

    return chromosomeIds;
}

From source file:com.qwazr.search.index.FacetsBuilder.java

License:Apache License

FacetsBuilder(QueryContext queryContext, LinkedHashMap<String, FacetDefinition> facetsDef, Query searchQuery,
        FacetsCollector facetsCollector, TimeTracker timeTracker)
        throws IOException, ParseException, ReflectiveOperationException, QueryNodeException {

    this.queryContext = queryContext;
    this.searchQuery = searchQuery;
    this.facetsDef = facetsDef;
    this.results = new LinkedHashMap();
    this.counts = queryContext.state == null ? null
            : new SortedSetDocValuesFacetCounts(queryContext.state, facetsCollector);

    for (Map.Entry<String, FacetDefinition> entry : facetsDef.entrySet()) {
        final String dim = entry.getKey();
        final FacetDefinition facet = entry.getValue();
        final Map<String, Number> result;
        if (facet.queries == null || facet.queries.isEmpty())
            result = buildFacetState(dim, facet);
        else//w  ww. j ava2  s  . c o  m
            result = buildFacetQueries(facet);
        if (result != null)
            results.put(dim, result);
    }
    if (timeTracker != null)
        timeTracker.next("facet_count");
}

From source file:com.searchcode.app.service.CodeSearcher.java

License:Open Source License

/**
 * Returns the matching language facets for a given query
 *//*from   www. j a  v  a 2s  .  c o  m*/
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;
}

From source file:com.searchcode.app.service.CodeSearcher.java

License:Open Source License

/**
 * Returns the matching repository facets for a given query
 *//*from   w ww  .j  ava  2 s  .  c o m*/
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;
}

From source file:com.searchcode.app.service.CodeSearcher.java

License:Open Source License

/**
 * Returns the matching owner facets for a given query
 *///from w  ww  .  j a v  a2  s  .  co  m
private List<CodeFacetOwner> getOwnerFacetResults(IndexSearcher searcher, IndexReader reader, Query query) {
    List<CodeFacetOwner> codeFacetRepo = new ArrayList<>();

    try {
        SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(reader,
                Values.CODEOWNER);
        FacetsCollector fc = new FacetsCollector();
        FacetsCollector.search(searcher, query, 10, fc);
        Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
        FacetResult result = facets.getTopChildren(200, Values.CODEOWNER);

        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 CodeFacetOwner(lv.label, lv.value.intValue()));
                }
            }
        }
    } catch (IOException ex) {
    } catch (Exception ex) {
    }

    return codeFacetRepo;
}

From source file:com.searchcode.app.service.TimeCodeSearcher.java

/**
 * Returns the matching revision facets for a given query
 *//*from  w  ww .  j  a va2  s.  c om*/
private List<CodeFacetDeleted> getDeletedFacetResults(IndexSearcher searcher, IndexReader reader, Query query) {
    List<CodeFacetDeleted> deletedFacets = new ArrayList<>();

    try {
        SortedSetDocValuesReaderState state = new DefaultSortedSetDocValuesReaderState(reader, Values.DELETED);
        FacetsCollector fc = new FacetsCollector();
        FacetsCollector.search(searcher, query, 10, fc);
        Facets facets = new SortedSetDocValuesFacetCounts(state, fc);
        FacetResult result = facets.getTopChildren(200, Values.DELETED);

        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) {
                    deletedFacets.add(new CodeFacetDeleted(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 deletedFacets;
}