List of usage examples for org.apache.lucene.facet FacetsCollector FacetsCollector
public FacetsCollector(boolean keepScores)
From source file:com.czw.search.lucene.example.facet.ExpressionAggregationFacetsExample.java
License:Apache License
/** 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(); bindings.add(new SortField("_score", SortField.Type.SCORE)); // the score of the document bindings.add(new SortField("popularity", SortField.Type.LONG)); // the value of the 'popularity' field // 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();/*from www .j a v a 2 s.c om*/ taxoReader.close(); return result; }
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 2s . 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.search.lucene.demo.facet.ExpressionAggregationFacetsExample.java
License:Apache License
/** 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(); bindings.add(new SortField("_score", SortField.Type.SCORE)); // the score of the document bindings.add(new SortField("popularity", SortField.Type.LONG)); // the value of the 'popularity' field // 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.getValueSource(bindings)); FacetResult result = facets.getTopChildren(10, "A"); indexReader.close();/*w ww . j a v a2 s . c o m*/ taxoReader.close(); return result; }
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 {/*www.ja va 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 {/*from w w w .ja va 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); } }
From source file:org.wso2.carbon.analytics.dataservice.core.indexing.AnalyticsDataIndexer.java
License:Open Source License
private double getDrillDownRecordCount(int tenantId, AnalyticsDrillDownRequest drillDownRequest, IndexReader indexReader, TaxonomyReader taxonomyReader, String rangeField, AnalyticsDrillDownRange range) throws AnalyticsIndexException { try {//from w ww . java 2 s .c om IndexSearcher indexSearcher = new IndexSearcher(indexReader); Map<String, ColumnDefinition> indices = this.lookupIndices(tenantId, drillDownRequest.getTableName()); FacetsConfig config = this.getFacetsConfigurations(indices); DrillDownQuery drillDownQuery = this.createDrillDownQuery(drillDownRequest, indices, config, rangeField, range); ValueSource scoreFunction = this.getCompiledScoreFunction(drillDownRequest.getScoreFunction(), indices); FacetsCollector facetsCollector = new FacetsCollector(true); Map<String, List<String>> categoryPaths = drillDownRequest.getCategoryPaths(); double count = 0; if (!categoryPaths.isEmpty()) { Map.Entry<String, List<String>> aCategory = categoryPaths.entrySet().iterator().next(); String categoryName = aCategory.getKey(); FacetsCollector.search(indexSearcher, drillDownQuery, Integer.MAX_VALUE, facetsCollector); Facets facets = new TaxonomyFacetSumValueSource(taxonomyReader, config, facetsCollector, scoreFunction); FacetResult facetResult = facets.getTopChildren(Integer.MAX_VALUE, categoryName, new String[0]); if (facetResult != null) { LabelAndValue[] subCategories = facetResult.labelValues; for (LabelAndValue category : subCategories) { count += category.value.doubleValue(); } } } else { count = indexSearcher.search(drillDownQuery, Integer.MAX_VALUE).totalHits; } return count; } catch (IndexNotFoundException ignore) { return 0; } catch (IOException e) { throw new AnalyticsIndexException("Error while getting drilldownCount: " + e.getMessage(), e); } finally { this.closeTaxonomyIndexReaders(indexReader, taxonomyReader); } }