@Override public FacetResult getTopChildren(int topN, String dim, String... path) throws IOException { Facets facets = dimToFacets.get(dim); if (facets == null) { if (defaultFacets == null) { throw new IllegalArgumentException("invalid dim \"" + dim + "\""); } facets = defaultFacets; } return facets.getTopChildren(topN, dim, path); }
@Override public List<FacetResult> getAllDims(int topN) throws IOException { List<FacetResult> results = new ArrayList<FacetResult>(); // First add the specific dim's facets: for(Map.Entry<String,Facets> ent : dimToFacets.entrySet()) { results.add(ent.getValue().getTopChildren(topN, ent.getKey())); } if (defaultFacets != null) { // Then add all default facets as long as we didn't // already add that dim: for(FacetResult result : defaultFacets.getAllDims(topN)) { if (dimToFacets.containsKey(result.dim) == false) { results.add(result); } } } return results; } }
@Override public Number getSpecificValue(String dim, String... path) throws IOException { Facets facets = dimToFacets.get(dim); if (facets == null) { if (defaultFacets == null) { throw new IllegalArgumentException("invalid dim \"" + dim + "\""); } facets = defaultFacets; } return facets.getSpecificValue(dim, path); }
private void buildFacetState(final String resolvedDimension, final Integer top, final Set<String[]> specificValues, final FacetBuilder facetBuilder) throws IOException { final Facets facets = getFacets(resolvedDimension); if (facets == null) return; if (top != null && top > 0) { final FacetResult facetResult = facets.getTopChildren(top, resolvedDimension); if (facetResult != null && facetResult.labelValues != null) for (LabelAndValue lv : facetResult.labelValues) facetBuilder.put(lv); } if (specificValues != null) { for (String[] path : specificValues) { final Number count = facets.getSpecificValue(resolvedDimension, path); facetBuilder.put(new LabelAndValue(StringUtils.join(path, '/'), count == null || count.longValue() <= 0 ? 0 : count)); } } }
/** User drills down on 'Publish Date/2010', and we * return facets for both 'Publish Date' and 'Author', * using DrillSideways. */ 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; }
@Override public FacetResult getTopChildren(int topN, String dim, String... path) throws IOException { Facets facets = dimToFacets.get(dim); if (facets == null) { if (defaultFacets == null) { throw new IllegalArgumentException("invalid dim \"" + dim + "\""); } facets = defaultFacets; } return facets.getTopChildren(topN, dim, path); }
@Override public List<FacetResult> getAllDims(int topN) throws IOException { List<FacetResult> results = new ArrayList<FacetResult>(); // First add the specific dim's facets: for(Map.Entry<String,Facets> ent : dimToFacets.entrySet()) { results.add(ent.getValue().getTopChildren(topN, ent.getKey())); } if (defaultFacets != null) { // Then add all default facets as long as we didn't // already add that dim: for(FacetResult result : defaultFacets.getAllDims(topN)) { if (dimToFacets.containsKey(result.dim) == false) { results.add(result); } } } return results; } }
@Override public Number getSpecificValue(String dim, String... path) throws IOException { Facets facets = dimToFacets.get(dim); if (facets == null) { if (defaultFacets == null) { throw new IllegalArgumentException("invalid dim \"" + dim + "\""); } facets = defaultFacets; } return facets.getSpecificValue(dim, path); }
public static String getFacetResultChildDisplayLabel(Facets facets, FacetResult facetResult, LabelAndValue labelAndValue) throws IOException { String path[] = Arrays.copyOf(facetResult.path, facetResult.path.length + 1); path[path.length - 1] = labelAndValue.label; FacetResult subResult = facets.getTopChildren(1 /* topN */, facetResult.dim, path); if (subResult != null && subResult.childCount > 0) { return StringUtils.capitalize(subResult.labelValues[0].label); } else { return StringUtils.capitalize(labelAndValue.label); } }
@Override public Number getSpecificValue(String dim, String... path) throws IOException { final Facets facets = getFacets(dim); return facets == null ? -1 : facets.getSpecificValue(dim, path); }
@Override public FacetResult getTopChildren(int topN, String dim, String... path) throws IOException { final Facets facets = getFacets(dim); return facets == null ? null : facets.getTopChildren(topN, dim, path); }
public static String getFacetPathDisplayLabel(Facets facets, RcIrFacetPath facetPath) throws IOException { FacetResult facetResult = facets.getTopChildren(1 /* topN */, facetPath.getDimension(), facetPath.getPath()); if (facetResult != null && facetResult.childCount > 0) { return StringUtils.capitalize(facetResult.labelValues[0].label); } else { return StringUtils.capitalize(facetPath.components[facetPath.components.length - 1]); } }
@Override public List<Facet> getFacets(int numberOfFacets, String columnName) throws IOException { String facetFieldName = FulltextIndex.parseFacetField(columnName); if (facets != null) { ImmutableList.Builder res = new ImmutableList.Builder<Facet>(); FacetResult topChildren = facets.getTopChildren(numberOfFacets, facetFieldName); if (topChildren != null) { for (LabelAndValue lav : topChildren.labelValues) { res.add(new Facet( lav.label, lav.value.intValue() )); } return res.build(); } } return null; } }
@Override public List<Facet> getFacets(int numberOfFacets, String columnName) throws IOException { String facetFieldName = FulltextIndex.parseFacetField(columnName); if (facets != null) { ImmutableList.Builder res = new ImmutableList.Builder<Facet>(); FacetResult topChildren = facets.getTopChildren(numberOfFacets, facetFieldName); if (topChildren != null) { for (LabelAndValue lav : topChildren.labelValues) { res.add(new Facet( lav.label, lav.value.intValue() )); } return res.build(); } } return null; } }
/** Runs the search and drill-down examples and prints the results. */ public static void main(String[] args) throws Exception { RangeFacetsExample example = new RangeFacetsExample(); example.index(); System.out.println("Facet counting example:"); System.out.println("-----------------------"); System.out.println(example.search()); System.out.println("\n"); System.out.println("Facet drill-down example (timestamp/Past six hours):"); System.out.println("---------------------------------------------"); TopDocs hits = example.drillDown(example.PAST_SIX_HOURS); System.out.println(hits.totalHits + " totalHits"); System.out.println("\n"); System.out.println("Facet drill-sideways example (timestamp/Past six hours):"); System.out.println("---------------------------------------------"); DrillSideways.DrillSidewaysResult sideways = example.drillSideways(example.PAST_SIX_HOURS); System.out.println(sideways.hits.totalHits + " totalHits"); System.out.println(sideways.facets.getTopChildren(10, "timestamp")); example.close(); } }
/** 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(); taxoReader.close(); return results; }
private CategoryDrillDownResponse getCategoryDrillDownResponse( CategoryDrillDownRequest drillDownRequest, List<CategorySearchResultEntry> searchResults, String[] path, Facets facets) throws IOException { FacetResult facetResult = facets.getTopChildren(Integer.MAX_VALUE, drillDownRequest.getFieldName(), path); CategoryDrillDownResponse response; if (facetResult != null) { LabelAndValue[] categories = facetResult.labelValues; for (LabelAndValue category : categories) { searchResults.add(new CategorySearchResultEntry(category.label, category.value.doubleValue())); } response = new CategoryDrillDownResponse(searchResults); } else { response = new CategoryDrillDownResponse(new ArrayList<CategorySearchResultEntry>(0)); } return response; }
/** User runs a query and counts facets. */ 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; }
/** 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(); taxoReader.close(); return results; }
String dim = RcIrStaticField.TITLE.getFieldName(); FacetResult facetResult = rcIrObjQueryResults.getFacets().getTopChildren(topN, dim);