Example usage for org.apache.lucene.facet DrillSideways search

List of usage examples for org.apache.lucene.facet DrillSideways search

Introduction

In this page you can find the example usage for org.apache.lucene.facet DrillSideways search.

Prototype

public <R> ConcurrentDrillSidewaysResult<R> search(final DrillDownQuery query,
        final CollectorManager<?, R> hitCollectorManager) throws IOException 

Source Link

Document

Runs a search, using a CollectorManager to gather and merge search results

Usage

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

License:Apache License

/** User drills down on the specified range. */
public TopDocs drillDown(DoubleRange range) throws IOException {

    // Passing no baseQuery means we drill down on all
    // documents ("browse only"):
    DrillDownQuery q = new DrillDownQuery(null);
    final DoubleValuesSource vs = getDistanceValueSource();
    q.add("field", range.getQuery(getBoundingBoxQuery(ORIGIN_LATITUDE, ORIGIN_LONGITUDE, range.max), vs));
    DrillSideways ds = new DrillSideways(searcher, config, (TaxonomyReader) null) {
        @Override/*w  ww.java2s .c  o  m*/
        protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways,
                String[] drillSidewaysDims) throws IOException {
            assert drillSideways.length == 1;
            return new DoubleRangeFacetCounts("field", vs, drillSideways[0], ONE_KM, TWO_KM, FIVE_KM, TEN_KM);
        }
    };
    return ds.search(q, 10).hits;
}

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

License:Apache License

/**
 * User drills down on 'Publish Date/2012', and we
 * return facets for both 'Publish Date' and 'Author',
 * using DrillSideways./*from  w w w  .  j a v  a  2s  .c  o m*/
 */
private List<FacetResult> drillSideways() 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");

    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
    DrillSidewaysResult result = ds.search(q, 10);

    // Retrieve results
    List<FacetResult> facets = result.facets.getAllDims(10);
    System.out.println("totalHits:" + result.hits.totalHits);
    ScoreDoc[] docs = result.hits.scoreDocs;
    System.out.println(searcher.doc(docs[0].doc));

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

    return facets;
}

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

License:Apache License

/** User drills down on 'Publish Date/2010', and we
 *  return facets for both 'Publish Date' and 'Author',
 *  using DrillSideways. *///from w  ww .  j  ava 2  s  .  c  o  m
private List<FacetResult> drillSideways() 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");

    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
    DrillSidewaysResult result = ds.search(q, 10);

    // Retrieve results
    List<FacetResult> facets = result.facets.getAllDims(10);

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

    return facets;
}

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

License:Apache License

/** User drills down on 'Publish Date/2010', and we
 *  return facets for both 'Publish Date' and 'Author',
 *  using DrillSideways. *///  w  w  w  . j  a va  2 s.c  o m
private List<FacetResult> drillSideways() 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");

    DrillSideways ds = new DrillSideways(searcher, config, taxoReader);
    DrillSidewaysResult result = ds.search(q, 10);

    // Retrieve results

    List<FacetResult> facets = result.facets.getAllDims(10);

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

    return facets;
}

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

License:Apache License

/** User drills down on the specified range. */
public TopDocs drillDown(DoubleRange range) throws IOException {

    // Passing no baseQuery means we drill down on all
    // documents ("browse only"):
    DrillDownQuery q = new DrillDownQuery(null);
    final ValueSource vs = getDistanceValueSource();
    //    q.add("field", range.getFilter(getBoundingBoxFilter(ORIGIN_LATITUDE, ORIGIN_LONGITUDE, range.max), vs));
    DrillSideways ds = new DrillSideways(searcher, config, (TaxonomyReader) null) {
        @Override/*from  w w  w  .  j  a  v  a  2s.  co  m*/
        protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways,
                String[] drillSidewaysDims) throws IOException {
            assert drillSideways.length == 1;
            return new DoubleRangeFacetCounts("field", vs, drillSideways[0], ONE_KM, TWO_KM, FIVE_KM, TEN_KM);
        }
    };
    return ds.search(q, 10).hits;
}

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 {/*from w  w w .ja v a  2s .  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);
    }
}

From source file:com.yida.framework.lucene5.facet.DistanceFacetsExample.java

License:Creative Commons License

/**
 * DrillDownQuery/* w w w  .  j a va 2 s. co  m*/
 * @param range
 * @return
 * @throws IOException
 */
public TopDocs drillDown(DoubleRange range) throws IOException {
    DrillDownQuery q = new DrillDownQuery(config);
    final ValueSource vs = getDistanceValueSource();
    q.add("field", range.getFilter(getBoundingBoxFilter(ORIGIN_LATITUDE, ORIGIN_LONGITUDE, range.max), vs));
    DrillSideways ds = new DrillSideways(searcher, config, (TaxonomyReader) null) {
        @Override
        protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways,
                String[] drillSidewaysDims) throws IOException {
            assert drillSideways.length == 1;
            return new DoubleRangeFacetCounts("field", vs, drillSideways[0], ONE_KM, TWO_KM, FIVE_KM, TEN_KM);
        }
    };
    return ds.search(q, 10).hits;
}

From source file:com.zghw.lucene.demo.DistanceFacetsExample.java

License:Apache License

/** User drills down on the specified range. */
public TopDocs drillDown(DoubleRange range) throws IOException {

    // Passing no baseQuery means we drill down on all
    // documents ("browse only"):
    DrillDownQuery q = new DrillDownQuery(null);
    final ValueSource vs = getDistanceValueSource();
    q.add("field", range.getFilter(getBoundingBoxFilter(ORIGIN_LATITUDE, ORIGIN_LONGITUDE, range.max), vs));
    DrillSideways ds = new DrillSideways(searcher, config, (TaxonomyReader) null) {
        @Override/*from  w ww.ja  v  a 2  s. co m*/
        protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways,
                String[] drillSidewaysDims) throws IOException {
            assert drillSideways.length == 1;
            return new DoubleRangeFacetCounts("field", vs, drillSideways[0], ONE_KM, TWO_KM, FIVE_KM, TEN_KM);
        }
    };
    return ds.search(q, 10).hits;
}

From source file:org.wso2.carbon.analytics.dataservice.core.indexing.AnalyticsDataIndexer.java

License:Open Source License

private List<SearchResultEntry> drillDownRecords(int tenantId, AnalyticsDrillDownRequest drillDownRequest,
        IndexReader indexReader, TaxonomyReader taxonomyReader, String rangeField,
        AnalyticsDrillDownRange range) throws AnalyticsIndexException {
    List<SearchResultEntry> searchResults = new ArrayList<>();
    try {/*w  ww  . ja  v  a  2s  .  c  o m*/
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        FacetsCollector facetsCollector = new FacetsCollector(true);
        Map<String, ColumnDefinition> indices = this.lookupIndices(tenantId, drillDownRequest.getTableName());
        FacetsConfig config = this.getFacetsConfigurations(indices);
        DrillSideways drillSideways = new DrillSideways(indexSearcher, config, taxonomyReader);
        DrillDownQuery drillDownQuery = this.createDrillDownQuery(drillDownRequest, indices, config, rangeField,
                range);
        drillSideways.search(drillDownQuery, facetsCollector);
        int topResultCount = drillDownRequest.getRecordStartIndex() + drillDownRequest.getRecordCount();
        TopDocs topDocs;
        if (drillDownRequest.getSortByFields() == null || drillDownRequest.getSortByFields().isEmpty()) {
            topDocs = FacetsCollector.search(indexSearcher, drillDownQuery, topResultCount, facetsCollector);
        } else {
            SortField[] sortFields = createSortFields(drillDownRequest.getSortByFields(), indices);
            topDocs = FacetsCollector.search(indexSearcher, drillDownQuery, null, topResultCount,
                    new Sort(sortFields), true, false, facetsCollector);
        }
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            Document document = indexSearcher.doc(scoreDoc.doc);
            searchResults.add(new SearchResultEntry(document.get(INDEX_ID_INTERNAL_FIELD), scoreDoc.score));
        }
        return searchResults;
    } catch (IndexNotFoundException ignore) {
        return new ArrayList<>();
    } catch (IOException e) {
        throw new AnalyticsIndexException("Error while performing drilldownRecords: " + e.getMessage(), e);
    } finally {
        this.closeTaxonomyIndexReaders(indexReader, taxonomyReader);
    }
}

From source file:org.wso2.carbon.analytics.dataservice.core.indexing.AnalyticsDataIndexer.java

License:Open Source License

private CategoryDrillDownResponse drilldowncategories(int tenantId, IndexReader indexReader,
        TaxonomyReader taxonomyReader, CategoryDrillDownRequest drillDownRequest)
        throws AnalyticsIndexException {
    List<CategorySearchResultEntry> searchResults = new ArrayList<>();
    try {/* ww w  . j  av  a 2  s  .  c o m*/
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        FacetsCollector facetsCollector = new FacetsCollector(true);
        Map<String, ColumnDefinition> indices = this.lookupIndices(tenantId, drillDownRequest.getTableName());
        FacetsConfig config = this.getFacetsConfigurations(indices);
        DrillSideways drillSideways = new DrillSideways(indexSearcher, config, taxonomyReader);
        Query queryObj = new MatchAllDocsQuery();
        if (drillDownRequest.getQuery() != null && !drillDownRequest.getQuery().isEmpty()) {
            Analyzer analyzer = getPerFieldAnalyzerWrapper(indices);
            queryObj = (new AnalyticsQueryParser(analyzer, indices)).parse(drillDownRequest.getQuery());
        }
        DrillDownQuery drillDownQuery = new DrillDownQuery(config, queryObj);
        String[] path = drillDownRequest.getPath();
        if (path == null) {
            path = new String[] {};
        }
        drillDownQuery.add(drillDownRequest.getFieldName(), path);
        drillSideways.search(drillDownQuery, facetsCollector);
        ValueSource valueSource = this.getCompiledScoreFunction(drillDownRequest.getScoreFunction(), indices);
        Facets facets = new TaxonomyFacetSumValueSource(taxonomyReader, config, facetsCollector, valueSource);
        return getCategoryDrillDownResponse(drillDownRequest, searchResults, path, facets);
    } catch (IndexNotFoundException ignore) {
        return new CategoryDrillDownResponse(new ArrayList<CategorySearchResultEntry>(0));
    } catch (IOException e) {
        throw new AnalyticsIndexException("Error while performing drilldownCategories: " + e.getMessage(), e);
    } catch (org.apache.lucene.queryparser.classic.ParseException e) {
        throw new AnalyticsIndexException("Error while parsing query " + e.getMessage(), e);
    } finally {
        this.closeTaxonomyIndexReaders(indexReader, taxonomyReader);
    }
}