use of com.yelp.nrtsearch.server.grpc.NumericRangeType in project nrtsearch by Yelp.
the class NumberFieldFacetsTest method assertNumericRangeFacet.
private void assertNumericRangeFacet(String fieldName) {
List<NumericRangeType> numericRangeTypes = new ArrayList<>();
numericRangeTypes.add(NumericRangeType.newBuilder().setLabel("1-10").setMin(1L).setMinInclusive(true).setMax(10L).setMaxInclusive(true).build());
numericRangeTypes.add(NumericRangeType.newBuilder().setLabel("11-20").setMin(11L).setMinInclusive(true).setMax(20L).setMaxInclusive(true).build());
SearchResponse response = getSearchResponse(fieldName, false, numericRangeTypes);
assertEquals(1, response.getFacetResultCount());
List<FacetResult> facetResults = response.getFacetResultList();
List<LabelAndValue> expectedLabelAndValues = new ArrayList<>();
expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("1-10").setValue(2.0).build());
expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("11-20").setValue(1.0).build());
assertFacetResult(facetResults.get(0), fieldName, 3, 2L, expectedLabelAndValues);
}
use of com.yelp.nrtsearch.server.grpc.NumericRangeType in project nrtsearch by Yelp.
the class VirtualFieldFacetsTest method testVirtualField.
@Test
public void testVirtualField() {
assertEquals(0, 0);
List<NumericRangeType> numericRangeTypes = new ArrayList<>();
numericRangeTypes.add(NumericRangeType.newBuilder().setLabel("1-50").setMin(1L).setMinInclusive(true).setMax(50L).setMaxInclusive(true).build());
numericRangeTypes.add(NumericRangeType.newBuilder().setLabel("51-100").setMin(51L).setMinInclusive(true).setMax(100L).setMaxInclusive(true).build());
String dim = "virtual_field_js_script";
SearchResponse response = getSearchResponse(dim, false, numericRangeTypes, "int_number_facet_field*5.0", Collections.emptyMap());
assertEquals(1, response.getFacetResultCount());
List<FacetResult> facetResults = response.getFacetResultList();
List<LabelAndValue> expectedLabelAndValues = new ArrayList<>();
expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("1-50").setValue(1.0).build());
expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("51-100").setValue(1.0).build());
assertFacetResult(facetResults.get(0), dim, 2, 2L, expectedLabelAndValues);
}
use of com.yelp.nrtsearch.server.grpc.NumericRangeType in project nrtsearch by Yelp.
the class FacetTopHitsTest method testWithNonTopDocsFacet.
@Test
public void testWithNonTopDocsFacet() {
List<Facet> facetList = new ArrayList<>();
Facet facet = Facet.newBuilder().setName("top_hits_facet_100").setDim("long_field").setSampleTopDocs(100).setTopN(20).build();
facetList.add(facet);
List<NumericRangeType> numericRangeTypes = new ArrayList<>();
numericRangeTypes.add(NumericRangeType.newBuilder().setLabel("0-7").setMin(0L).setMinInclusive(true).setMax(7L).setMaxInclusive(true).build());
numericRangeTypes.add(NumericRangeType.newBuilder().setLabel("8-9").setMin(8L).setMinInclusive(true).setMax(9L).setMaxInclusive(true).build());
facet = Facet.newBuilder().setName("numeric_range").setDim("long_field").setUseOrdsCache(false).addAllNumericRange(numericRangeTypes).setTopN(20).build();
facetList.add(facet);
SearchResponse response = doQueryMulti(facetList, 10);
assertEquals(facetList.size(), response.getFacetResultCount());
FacetResult facetResult = response.getFacetResult(0);
assertEquals("numeric_range", facetResult.getName());
assertEquals(100, facetResult.getValue(), 0);
assertEquals(2, facetResult.getChildCount());
assertEquals(2, facetResult.getLabelValuesCount());
assertEquals("0-7", facetResult.getLabelValues(0).getLabel());
assertEquals(80.0, facetResult.getLabelValues(0).getValue(), 0);
assertEquals("8-9", facetResult.getLabelValues(1).getLabel());
assertEquals(20.0, facetResult.getLabelValues(1).getValue(), 0);
assertFacetResult(response.getFacetResult(1), "top_hits_facet_100", 100, 10, 10, new ExpectedValues(new HashSet<>(Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7", "8", "9")), 10));
}
use of com.yelp.nrtsearch.server.grpc.NumericRangeType in project nrtsearch by Yelp.
the class DrillSidewaysImpl method getFieldFacetResult.
private static com.yelp.nrtsearch.server.grpc.FacetResult getFieldFacetResult(FacetsCollector drillDowns, Map<String, FacetsCollector> dsDimMap, ShardState shardState, Facet facet, Map<String, FieldDef> dynamicFields, SearcherTaxonomyManager.SearcherAndTaxonomy searcherAndTaxonomyManager, Map<String, Facets> indexFieldNameToFacets) throws IOException {
IndexState indexState = shardState.indexState;
String fieldName = facet.getDim();
FieldDef fieldDef = dynamicFields.get(fieldName);
if (fieldDef == null) {
throw new IllegalArgumentException(String.format("field %s was not registered and was not specified as a dynamic field ", fieldName));
}
FacetResult facetResult;
if (!(fieldDef instanceof IndexableFieldDef) && !(fieldDef instanceof VirtualFieldDef)) {
throw new IllegalArgumentException(String.format("field %s is neither a virtual field nor registered as an indexable field. Facets are supported only for these types", fieldName));
}
if (!facet.getNumericRangeList().isEmpty()) {
if (fieldDef.getFacetValueType() != IndexableFieldDef.FacetValueType.NUMERIC_RANGE) {
throw new IllegalArgumentException(String.format("field %s was not registered with facet=numericRange", fieldDef.getName()));
}
if (fieldDef instanceof IntFieldDef || fieldDef instanceof LongFieldDef) {
List<NumericRangeType> rangeList = facet.getNumericRangeList();
LongRange[] ranges = new LongRange[rangeList.size()];
for (int i = 0; i < ranges.length; i++) {
NumericRangeType numericRangeType = rangeList.get(i);
ranges[i] = new LongRange(numericRangeType.getLabel(), numericRangeType.getMin(), numericRangeType.getMinInclusive(), numericRangeType.getMax(), numericRangeType.getMaxInclusive());
}
FacetsCollector c = dsDimMap.get(fieldDef.getName());
if (c == null) {
c = drillDowns;
}
LongRangeFacetCounts longRangeFacetCounts = new LongRangeFacetCounts(fieldDef.getName(), c, ranges);
facetResult = longRangeFacetCounts.getTopChildren(0, fieldDef.getName(), facet.getPathsList().toArray(new String[facet.getPathsCount()]));
} else if (fieldDef instanceof FloatFieldDef) {
throw new IllegalArgumentException(String.format("field %s is of type float with FloatFieldDocValues which do not support numeric_range faceting", fieldDef.getName()));
} else if (fieldDef instanceof DoubleFieldDef || fieldDef instanceof VirtualFieldDef) {
List<NumericRangeType> rangeList = facet.getNumericRangeList();
DoubleRange[] ranges = new DoubleRange[rangeList.size()];
for (int i = 0; i < ranges.length; i++) {
NumericRangeType numericRangeType = rangeList.get(i);
ranges[i] = new DoubleRange(numericRangeType.getLabel(), numericRangeType.getMin(), numericRangeType.getMinInclusive(), numericRangeType.getMax(), numericRangeType.getMaxInclusive());
}
FacetsCollector c = dsDimMap.get(fieldDef.getName());
if (c == null) {
c = drillDowns;
}
DoubleRangeFacetCounts doubleRangeFacetCounts;
if (fieldDef instanceof VirtualFieldDef) {
VirtualFieldDef virtualFieldDef = (VirtualFieldDef) fieldDef;
doubleRangeFacetCounts = new DoubleRangeFacetCounts(virtualFieldDef.getName(), virtualFieldDef.getValuesSource(), c, ranges);
} else {
doubleRangeFacetCounts = new DoubleRangeFacetCounts(fieldDef.getName(), c, ranges);
}
facetResult = doubleRangeFacetCounts.getTopChildren(0, fieldDef.getName(), facet.getPathsList().toArray(new String[facet.getPathsCount()]));
} else {
throw new IllegalArgumentException(String.format("numericRanges must be provided only on field type numeric e.g. int, double, flat"));
}
} else if (fieldDef.getFacetValueType() == IndexableFieldDef.FacetValueType.SORTED_SET_DOC_VALUES) {
FacetsCollector c = dsDimMap.get(fieldDef.getName());
if (c == null) {
c = drillDowns;
}
if (facet.getLabelsCount() > 0) {
// filter facet if a label list is provided
FilteredSSDVFacetCounts filteredSSDVFacetCounts = new FilteredSSDVFacetCounts(facet.getLabelsList(), fieldDef.getName(), shardState.getSSDVState(searcherAndTaxonomyManager, fieldDef), c);
facetResult = filteredSSDVFacetCounts.getTopChildren(facet.getTopN(), fieldDef.getName());
} else {
SortedSetDocValuesFacetCounts sortedSetDocValuesFacetCounts = new SortedSetDocValuesFacetCounts(shardState.getSSDVState(searcherAndTaxonomyManager, fieldDef), c);
facetResult = sortedSetDocValuesFacetCounts.getTopChildren(facet.getTopN(), fieldDef.getName());
}
} else if (fieldDef.getFacetValueType() != IndexableFieldDef.FacetValueType.NO_FACETS) {
// Taxonomy facets
if (fieldDef.getFacetValueType() == IndexableFieldDef.FacetValueType.NUMERIC_RANGE) {
throw new IllegalArgumentException(String.format("%s was registered with facet = numericRange; must pass numericRanges in the request", fieldDef.getName()));
}
String[] path;
if (!facet.getPathsList().isEmpty()) {
ProtocolStringList pathList = facet.getPathsList();
path = new String[facet.getPathsList().size()];
for (int idx = 0; idx < path.length; idx++) {
path[idx] = pathList.get(idx);
}
} else {
path = new String[0];
}
FacetsCollector c = dsDimMap.get(fieldDef.getName());
boolean useCachedOrds = facet.getUseOrdsCache();
Facets luceneFacets;
if (c != null) {
// This dimension was used in
// drill-down; compute its facet counts from the
// drill-sideways collector:
String indexFieldName = indexState.facetsConfig.getDimConfig(fieldDef.getName()).indexFieldName;
if (useCachedOrds) {
luceneFacets = new TaxonomyFacetCounts(shardState.getOrdsCache(indexFieldName), searcherAndTaxonomyManager.taxonomyReader, indexState.facetsConfig, c);
} else {
luceneFacets = new FastTaxonomyFacetCounts(indexFieldName, searcherAndTaxonomyManager.taxonomyReader, indexState.facetsConfig, c);
}
} else {
// nocommit test both normal & ssdv facets in same index
// See if we already computed facet
// counts for this indexFieldName:
String indexFieldName = indexState.facetsConfig.getDimConfig(fieldDef.getName()).indexFieldName;
Map<String, Facets> facetsMap = indexFieldNameToFacets;
luceneFacets = facetsMap.get(indexFieldName);
if (luceneFacets == null) {
if (useCachedOrds) {
luceneFacets = new TaxonomyFacetCounts(shardState.getOrdsCache(indexFieldName), searcherAndTaxonomyManager.taxonomyReader, indexState.facetsConfig, drillDowns);
} else {
luceneFacets = new FastTaxonomyFacetCounts(indexFieldName, searcherAndTaxonomyManager.taxonomyReader, indexState.facetsConfig, drillDowns);
}
facetsMap.put(indexFieldName, luceneFacets);
}
}
if (facet.getTopN() != 0) {
facetResult = luceneFacets.getTopChildren(facet.getTopN(), fieldDef.getName(), path);
} else if (!facet.getLabelsList().isEmpty()) {
List<LabelAndValue> results = new ArrayList<LabelAndValue>();
for (String label : facet.getLabelsList()) {
results.add(new LabelAndValue(label, luceneFacets.getSpecificValue(fieldDef.getName(), label)));
}
facetResult = new FacetResult(fieldDef.getName(), path, -1, results.toArray(new LabelAndValue[results.size()]), -1);
} else {
throw new IllegalArgumentException(String.format("each facet request must have either topN or labels"));
}
} else {
// if no facet type is enabled on the field, try using the field doc values
if (!(fieldDef instanceof IndexableFieldDef)) {
throw new IllegalArgumentException("Doc values facet requires an indexable field : " + fieldName);
}
IndexableFieldDef indexableFieldDef = (IndexableFieldDef) fieldDef;
if (!indexableFieldDef.hasDocValues()) {
throw new IllegalArgumentException("Doc values facet requires doc values enabled : " + fieldName);
}
return getDocValuesFacetResult(facet, drillDowns, indexableFieldDef);
}
return buildFacetResultGrpc(facetResult, facet.getName());
}
Aggregations