Example usage for org.apache.lucene.facet.taxonomy FastTaxonomyFacetCounts FastTaxonomyFacetCounts

List of usage examples for org.apache.lucene.facet.taxonomy FastTaxonomyFacetCounts FastTaxonomyFacetCounts

Introduction

In this page you can find the example usage for org.apache.lucene.facet.taxonomy FastTaxonomyFacetCounts FastTaxonomyFacetCounts.

Prototype

public FastTaxonomyFacetCounts(TaxonomyReader taxoReader, FacetsConfig config, FacetsCollector fc)
        throws IOException 

Source Link

Document

Create FastTaxonomyFacetCounts , which also counts all facet labels.

Usage

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

License:Apache License

/**
 * User runs a query and counts facets.//  w ww .ja v a2s.  com
 */
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"));
    results.add(facets.getTopChildren(10, "Category"));

    indexReader.close();
    taxoReader.close();

    return results;
}

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

License:Apache License

/**
 * User runs a query and counts facets only without collecting the matching documents.
 *//* ww  w  . ja v  a2s .c om*/
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"));
    results.add(facets.getTopChildren(10, "Category"));

    indexReader.close();
    taxoReader.close();

    return results;
}

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

License:Apache License

/**
 * User drills down on 'Publish Date/2011', and we
 * return facets for 'Author'//from  w  w  w  .  j  a  va  2 s .  co  m
 */
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;
}

From source file:com.justinleegrant.myluceneplayground.SimpleFacetsExample.java

License:Apache License

/** 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();//from w  w w  .  j  a va 2 s  . co m
    taxoReader.close();

    return results;
}

From source file:com.justinleegrant.myluceneplayground.SimpleFacetsExample.java

License:Apache License

/** 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(), null /*Filter */, 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();/*from   w  ww.j  av a  2s  .com*/
    taxoReader.close();

    return results;
}

From source file:com.orientechnologies.lucene.collections.LuceneResultSet.java

License:Apache License

private void fetchFacet() {
    if (queryContext.facet) {
        FacetsCollector facetsCollector = new FacetsCollector(true);

        try {//from  w w w.j a  v a  2 s.c  om

            String[] pathFacet = null;
            if (queryContext.isDrillDown()) {
                DrillDownQuery drillDownQuery = new DrillDownQuery(queryContext.getFacetConfig(), query);
                String[] path = queryContext.getDrillDownQuery().split(":");
                pathFacet = path[1].split("/");
                drillDownQuery.add(path[0], pathFacet);
                FacetsCollector.search(queryContext.searcher, drillDownQuery, PAGE_SIZE, facetsCollector);
            } else {
                FacetsCollector.search(queryContext.searcher, query, PAGE_SIZE, facetsCollector);
            }

            Facets facets = new FastTaxonomyFacetCounts(queryContext.reader, queryContext.getFacetConfig(),
                    facetsCollector);

            FacetResult facetResult = null;
            if (pathFacet != null) {
                facetResult = facets.getTopChildren(PAGE_SIZE, queryContext.getFacetField(), pathFacet);
            } else {
                facetResult = facets.getTopChildren(PAGE_SIZE, queryContext.getFacetField());
            }

            if (facetResult != null) {
                List<ODocument> documents = new ArrayList<ODocument>();
                // for (FacetResult facetResult : res) {

                ODocument doc = new ODocument();

                doc.field("childCount", facetResult.childCount);
                doc.field("value", facetResult.value);
                doc.field("dim", facetResult.dim);
                List<ODocument> labelsAndValue = new ArrayList<ODocument>();
                for (LabelAndValue labelValue : facetResult.labelValues) {
                    ODocument doc1 = new ODocument();
                    doc1.field("label", labelValue.label);
                    doc1.field("value", labelValue.value);
                    labelsAndValue.add(doc1);

                }
                doc.field("labelsValue", labelsAndValue);
                documents.add(doc);
                queryContext.context.setVariable("$facet", documents);
            }
            // }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.orientechnologies.lucene.test.LuceneNativeFacet.java

License:Apache License

/** 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<FacetResult>();

    // 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();/*from  w  w  w.  j  a va2  s  . co m*/
    taxoReader.close();

    return results;
}

From source file:com.orientechnologies.lucene.test.LuceneNativeFacet.java

License:Apache License

/** 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(), null /*Filter */, fc);

    // Retrieve results
    List<FacetResult> results = new ArrayList<FacetResult>();

    // 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();/* w  w  w  . j  a v a  2 s .  c o  m*/
    taxoReader.close();

    return results;
}

From source file:com.search.lucene.demo.facet.SimpleFacetsExample.java

License:Apache License

/** 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();/*from  w ww.  ja va 2s .  com*/
    taxoReader.close();

    return results;
}

From source file:com.xiaomi.linden.core.search.LindenCoreImpl.java

License:Apache License

public LindenResult search(LindenSearchRequest request) throws IOException {
    SearcherTaxonomyManager.SearcherAndTaxonomy searcherAndTaxonomy = lindenNRTSearcherManager.acquire();
    try {// w  w w. j a  v  a2  s.  c o m
        IndexSearcher indexSearcher = searcherAndTaxonomy.searcher;
        Filter filter = FilterConstructor.constructFilter(request.getFilter(), config);
        Sort sort = SortConstructor.constructSort(request, indexSearcher, config);
        indexSearcher.setSimilarity(config.getSearchSimilarityInstance());

        Query query = QueryConstructor.constructQuery(request.getQuery(), config);
        if (filter != null) {
            query = new FilteredQuery(query, filter);
        }

        int from = request.getOffset();
        int size = request.getLength();
        LindenResultParser resultParser = new LindenResultParser(config, request, indexSearcher,
                snippetGenerator, query, filter, sort);
        // very common search, no group, no facet, no early termination, no search time limit
        if (!request.isSetGroupParam() && !request.isSetFacet() && !request.isSetEarlyParam()
                && config.getSearchTimeLimit() <= 0) {
            TopDocs docs;
            if (sort != null) {
                docs = indexSearcher.search(query, from + size, sort);
            } else {
                docs = indexSearcher.search(query, from + size);
            }
            return resultParser.parse(docs, null, null, null);
        }

        // group param will suppress facet, group, early termination and search time limit parameters
        if (request.isSetGroupParam()) {
            String groupField = request.getGroupParam().getGroupField();
            GroupingSearch groupingSearch = new GroupingSearch(groupField);
            groupingSearch.setGroupDocsLimit(request.getGroupParam().getGroupInnerLimit());
            if (sort != null) {
                groupingSearch.setGroupSort(sort);
                groupingSearch.setSortWithinGroup(sort);
                groupingSearch.setFillSortFields(true);
            }
            groupingSearch.setCachingInMB(8.0, true);
            groupingSearch.setAllGroups(true);
            TopGroups<TopDocs> topGroupedDocs = groupingSearch.search(indexSearcher, query, 0, from + size);
            return resultParser.parse(null, topGroupedDocs, null, null);
        }

        TopDocsCollector topDocsCollector;
        if (sort != null) {
            topDocsCollector = TopFieldCollector.create(sort, from + size, null, true, false, false, false);
        } else {
            topDocsCollector = TopScoreDocCollector.create(from + size, false);
        }

        LindenDocsCollector lindenDocsCollector;
        if (request.isSetEarlyParam()) {
            MergePolicy mergePolicy = indexWriter.getConfig().getMergePolicy();
            Sort mergePolicySort = null;
            if (mergePolicy instanceof SortingMergePolicyDecorator) {
                mergePolicySort = ((SortingMergePolicyDecorator) mergePolicy).getSort();
            }
            EarlyTerminationCollector earlyTerminationCollector = new EarlyTerminationCollector(
                    topDocsCollector, mergePolicySort, request.getEarlyParam().getMaxNum());
            lindenDocsCollector = new LindenDocsCollector(earlyTerminationCollector);
        } else {
            lindenDocsCollector = new LindenDocsCollector(topDocsCollector);
        }

        Collector collector = lindenDocsCollector;
        if (config.getSearchTimeLimit() > 0) {
            collector = new TimeLimitingCollector(lindenDocsCollector, TimeLimitingCollector.getGlobalCounter(),
                    config.getSearchTimeLimit());
        }

        // no facet param
        if (!request.isSetFacet()) {
            indexSearcher.search(query, collector);
            return resultParser.parse(lindenDocsCollector.topDocs(), null, null, null);
        }

        // facet search
        LindenFacet facetRequest = request.getFacet();
        FacetsCollector facetsCollector = new FacetsCollector();
        lindenDocsCollector.wrap(facetsCollector);

        Facets facets = null;
        if (facetRequest.isSetDrillDownDimAndPaths()) {
            // drillDown or drillSideways
            DrillDownQuery drillDownQuery = new DrillDownQuery(facetsConfig, query);
            List<LindenFacetDimAndPath> drillDownDimAndPaths = facetRequest.getDrillDownDimAndPaths();
            for (int i = 0; i < drillDownDimAndPaths.size(); ++i) {
                String fieldName = drillDownDimAndPaths.get(i).dim;
                if (drillDownDimAndPaths.get(i).path != null) {
                    drillDownQuery.add(fieldName, drillDownDimAndPaths.get(i).path.split("/"));
                } else {
                    drillDownQuery.add(fieldName);
                }
            }

            // drillSideways
            if (facetRequest.getFacetDrillingType() == FacetDrillingType.DRILLSIDEWAYS) {
                DrillSideways dillSideways = new DrillSideways(indexSearcher, facetsConfig,
                        searcherAndTaxonomy.taxonomyReader);
                DrillSideways.DrillSidewaysResult drillSidewaysResult = dillSideways.search(drillDownQuery,
                        collector);
                facets = drillSidewaysResult.facets;
            } else {
                // drillDown
                indexSearcher.search(drillDownQuery, collector);
                facets = new FastTaxonomyFacetCounts(searcherAndTaxonomy.taxonomyReader, facetsConfig,
                        facetsCollector);
            }
        } else {
            indexSearcher.search(query, collector);
            // Simple facet browsing
            if (facetRequest.isSetFacetParams()) {
                facets = new FastTaxonomyFacetCounts(searcherAndTaxonomy.taxonomyReader, facetsConfig,
                        facetsCollector);
            }
        }
        return resultParser.parse(lindenDocsCollector.topDocs(), null, facets, facetsCollector);
    } catch (Exception e) {
        throw new IOException(Throwables.getStackTraceAsString(e));
    } finally {
        lindenNRTSearcherManager.release(searcherAndTaxonomy);
    }
}