Search in sources :

Example 6 with FacetResult

use of com.yelp.nrtsearch.server.grpc.FacetResult in project nrtsearch by Yelp.

the class TextFieldFacetsTest method testFlatMultivalued.

@Test
public void testFlatMultivalued() {
    SearchResponse response = getSearchResponse("flat_facet_field");
    assertEquals(1, response.getFacetResultCount());
    List<FacetResult> facetResults = response.getFacetResultList();
    List<LabelAndValue> expectedLabelAndValues = new ArrayList<>();
    expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("John").setValue(2.0).build());
    expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("Doe").setValue(1.0).build());
    expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("Smith").setValue(1.0).build());
    // NOTE: total number of buckets/value returned by FastTaxonomyFacetCounts is -1 on multivalued
    // fields.
    assertFacetResult(facetResults.get(0), "flat_facet_field", -1, 3L, expectedLabelAndValues);
}
Also used : ArrayList(java.util.ArrayList) FacetResult(com.yelp.nrtsearch.server.grpc.FacetResult) LabelAndValue(com.yelp.nrtsearch.server.grpc.LabelAndValue) SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse) Test(org.junit.Test)

Example 7 with FacetResult

use of com.yelp.nrtsearch.server.grpc.FacetResult in project nrtsearch by Yelp.

the class TextFieldFacetsTest method testHierarchySingleValued.

@Test
public void testHierarchySingleValued() {
    SearchResponse response = getSearchResponse("hierarchy_facet_field_single_valued");
    assertEquals(1, response.getFacetResultCount());
    List<FacetResult> facetResults = response.getFacetResultList();
    List<LabelAndValue> expectedLabelAndValues = new ArrayList<>();
    expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("John").setValue(3.0).build());
    assertFacetResult(facetResults.get(0), "hierarchy_facet_field_single_valued", 3, 1L, expectedLabelAndValues);
}
Also used : ArrayList(java.util.ArrayList) FacetResult(com.yelp.nrtsearch.server.grpc.FacetResult) LabelAndValue(com.yelp.nrtsearch.server.grpc.LabelAndValue) SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse) Test(org.junit.Test)

Example 8 with FacetResult

use of com.yelp.nrtsearch.server.grpc.FacetResult in project nrtsearch by Yelp.

the class TextFieldFacetsTest method testSortedDocValuesFacet.

@Test
public void testSortedDocValuesFacet() {
    SearchResponse response = getSearchResponse("sorted_doc_values_facet_field");
    assertEquals(1, response.getFacetResultCount());
    List<FacetResult> facetResults = response.getFacetResultList();
    List<LabelAndValue> expectedLabelAndValues = new ArrayList<>();
    expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("John").setValue(2.0).build());
    expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("Doe").setValue(1.0).build());
    expectedLabelAndValues.add(LabelAndValue.newBuilder().setLabel("Smith").setValue(1.0).build());
    assertFacetResult(facetResults.get(0), "sorted_doc_values_facet_field", 4, 3L, expectedLabelAndValues);
}
Also used : ArrayList(java.util.ArrayList) FacetResult(com.yelp.nrtsearch.server.grpc.FacetResult) LabelAndValue(com.yelp.nrtsearch.server.grpc.LabelAndValue) SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse) Test(org.junit.Test)

Example 9 with FacetResult

use of com.yelp.nrtsearch.server.grpc.FacetResult 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);
}
Also used : NumericRangeType(com.yelp.nrtsearch.server.grpc.NumericRangeType) ArrayList(java.util.ArrayList) FacetResult(com.yelp.nrtsearch.server.grpc.FacetResult) NumberFieldFacetsTest.assertFacetResult(com.yelp.nrtsearch.server.luceneserver.facet.NumberFieldFacetsTest.assertFacetResult) LabelAndValue(com.yelp.nrtsearch.server.grpc.LabelAndValue) SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse) Test(org.junit.Test)

Example 10 with FacetResult

use of com.yelp.nrtsearch.server.grpc.FacetResult in project nrtsearch by Yelp.

the class SearchHandler method handle.

@Override
public SearchResponse handle(IndexState indexState, SearchRequest searchRequest) throws SearchHandlerException {
    // this request may have been waiting in the grpc queue too long
    DeadlineUtils.checkDeadline("SearchHandler: start", "SEARCH");
    ShardState shardState = indexState.getShard(0);
    // Index won't be started if we are currently warming
    if (!warming) {
        indexState.verifyStarted();
    }
    var diagnostics = SearchResponse.Diagnostics.newBuilder();
    SearcherTaxonomyManager.SearcherAndTaxonomy s = null;
    SearchContext searchContext;
    try {
        s = getSearcherAndTaxonomy(searchRequest, shardState, diagnostics, threadPoolExecutor);
        ProfileResult.Builder profileResultBuilder = null;
        if (searchRequest.getProfile()) {
            profileResultBuilder = ProfileResult.newBuilder();
        }
        searchContext = SearchRequestProcessor.buildContextForRequest(searchRequest, indexState, shardState, s, profileResultBuilder);
        long searchStartTime = System.nanoTime();
        SearcherResult searcherResult;
        TopDocs hits;
        if (!searchRequest.getFacetsList().isEmpty()) {
            if (!(searchContext.getQuery() instanceof DrillDownQuery)) {
                throw new IllegalArgumentException("Can only use DrillSideways on DrillDownQuery");
            }
            DrillDownQuery ddq = (DrillDownQuery) searchContext.getQuery();
            List<FacetResult> grpcFacetResults = new ArrayList<>();
            DrillSideways drillS = new DrillSidewaysImpl(s.searcher, indexState.facetsConfig, s.taxonomyReader, searchRequest.getFacetsList(), s, shardState, searchContext.getQueryFields(), grpcFacetResults, threadPoolExecutor, diagnostics);
            DrillSideways.ConcurrentDrillSidewaysResult<SearcherResult> concurrentDrillSidewaysResult;
            try {
                concurrentDrillSidewaysResult = drillS.search(ddq, searchContext.getCollector().getWrappedManager());
            } catch (RuntimeException e) {
                // Searching with DrillSideways wraps exceptions in a few layers.
                // Try to find if this was caused by a timeout, if so, re-wrap
                // so that the top level exception is the same as when not using facets.
                CollectionTimeoutException timeoutException = findTimeoutException(e);
                if (timeoutException != null) {
                    throw new CollectionTimeoutException(timeoutException.getMessage(), e);
                }
                throw e;
            }
            searcherResult = concurrentDrillSidewaysResult.collectorResult;
            hits = searcherResult.getTopDocs();
            searchContext.getResponseBuilder().addAllFacetResult(grpcFacetResults);
            searchContext.getResponseBuilder().addAllFacetResult(FacetTopDocs.facetTopDocsSample(hits, searchRequest.getFacetsList(), indexState, s.searcher, diagnostics));
        } else {
            searcherResult = s.searcher.search(searchContext.getQuery(), searchContext.getCollector().getWrappedManager());
            hits = searcherResult.getTopDocs();
        }
        // add results from any extra collectors
        searchContext.getResponseBuilder().putAllCollectorResults(searcherResult.getCollectorResults());
        searchContext.getResponseBuilder().setHitTimeout(searchContext.getCollector().hadTimeout());
        searchContext.getResponseBuilder().setTerminatedEarly(searchContext.getCollector().terminatedEarly());
        diagnostics.setFirstPassSearchTimeMs(((System.nanoTime() - searchStartTime) / 1000000.0));
        DeadlineUtils.checkDeadline("SearchHandler: post recall", "SEARCH");
        // add detailed timing metrics for query execution
        if (profileResultBuilder != null) {
            searchContext.getCollector().maybeAddProfiling(profileResultBuilder);
        }
        long rescoreStartTime = System.nanoTime();
        if (!searchContext.getRescorers().isEmpty()) {
            for (RescoreTask rescorer : searchContext.getRescorers()) {
                long startNS = System.nanoTime();
                hits = rescorer.rescore(hits, searchContext);
                long endNS = System.nanoTime();
                diagnostics.putRescorersTimeMs(rescorer.getName(), (endNS - startNS) / 1000000.0);
                DeadlineUtils.checkDeadline("SearchHandler: post " + rescorer.getName(), "SEARCH");
            }
            diagnostics.setRescoreTimeMs(((System.nanoTime() - rescoreStartTime) / 1000000.0));
        }
        long t0 = System.nanoTime();
        hits = getHitsFromOffset(hits, searchContext.getStartHit(), searchContext.getTopHits());
        // create Hit.Builder for each hit, and populate with lucene doc id and ranking info
        setResponseHits(searchContext, hits);
        // fill Hit.Builder with requested fields
        fetchFields(searchContext);
        SearchState.Builder searchState = SearchState.newBuilder();
        searchContext.getResponseBuilder().setSearchState(searchState);
        searchState.setTimestamp(searchContext.getTimestampSec());
        // Record searcher version that handled this request:
        searchState.setSearcherVersion(((DirectoryReader) s.searcher.getIndexReader()).getVersion());
        // Fill in lastDoc for searchAfter:
        if (hits.scoreDocs.length != 0) {
            ScoreDoc lastHit = hits.scoreDocs[hits.scoreDocs.length - 1];
            searchState.setLastDocId(lastHit.doc);
            searchContext.getCollector().fillLastHit(searchState, lastHit);
        }
        diagnostics.setGetFieldsTimeMs(((System.nanoTime() - t0) / 1000000.0));
        searchContext.getResponseBuilder().setDiagnostics(diagnostics);
        if (profileResultBuilder != null) {
            searchContext.getResponseBuilder().setProfileResult(profileResultBuilder);
        }
    } catch (IOException | InterruptedException | ExecutionException e) {
        logger.warn(e.getMessage(), e);
        throw new SearchHandlerException(e);
    } finally {
        // does:
        try {
            if (s != null) {
                shardState.release(s);
            }
        } catch (IOException e) {
            logger.warn("Failed to release searcher reference previously acquired by acquire()", e);
            throw new SearchHandlerException(e);
        }
    }
    // Add searchRequest to warmer if needed
    try {
        if (!warming && indexState.getWarmer() != null) {
            indexState.getWarmer().addSearchRequest(searchRequest);
        }
    } catch (Exception e) {
        logger.error("Unable to add warming query", e);
    }
    // if we are out of time, don't bother with serialization
    DeadlineUtils.checkDeadline("SearchHandler: end", "SEARCH");
    return searchContext.getResponseBuilder().build();
}
Also used : DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) ArrayList(java.util.ArrayList) SearchContext(com.yelp.nrtsearch.server.luceneserver.search.SearchContext) DrillSidewaysImpl(com.yelp.nrtsearch.server.luceneserver.facet.DrillSidewaysImpl) SearcherTaxonomyManager(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager) ScoreDoc(org.apache.lucene.search.ScoreDoc) FacetTopDocs(com.yelp.nrtsearch.server.luceneserver.facet.FacetTopDocs) TopDocs(org.apache.lucene.search.TopDocs) ProfileResult(com.yelp.nrtsearch.server.grpc.ProfileResult) CollectionTimeoutException(com.yelp.nrtsearch.server.luceneserver.search.SearchCutoffWrapper.CollectionTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) RescoreTask(com.yelp.nrtsearch.server.luceneserver.rescore.RescoreTask) IOException(java.io.IOException) CollectionTimeoutException(com.yelp.nrtsearch.server.luceneserver.search.SearchCutoffWrapper.CollectionTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SearchState(com.yelp.nrtsearch.server.grpc.SearchResponse.SearchState) SearcherResult(com.yelp.nrtsearch.server.luceneserver.search.SearcherResult) DrillSideways(org.apache.lucene.facet.DrillSideways) FacetResult(com.yelp.nrtsearch.server.grpc.FacetResult)

Aggregations

FacetResult (com.yelp.nrtsearch.server.grpc.FacetResult)17 SearchResponse (com.yelp.nrtsearch.server.grpc.SearchResponse)11 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 LabelAndValue (com.yelp.nrtsearch.server.grpc.LabelAndValue)8 Facet (com.yelp.nrtsearch.server.grpc.Facet)4 NumericRangeType (com.yelp.nrtsearch.server.grpc.NumericRangeType)3 HashSet (java.util.HashSet)3 IOException (java.io.IOException)2 TopDocs (org.apache.lucene.search.TopDocs)2 ProfileResult (com.yelp.nrtsearch.server.grpc.ProfileResult)1 Diagnostics (com.yelp.nrtsearch.server.grpc.SearchResponse.Diagnostics)1 SearchState (com.yelp.nrtsearch.server.grpc.SearchResponse.SearchState)1 IndexState (com.yelp.nrtsearch.server.luceneserver.IndexState)1 LoadedDocValues (com.yelp.nrtsearch.server.luceneserver.doc.LoadedDocValues)1 DrillSidewaysImpl (com.yelp.nrtsearch.server.luceneserver.facet.DrillSidewaysImpl)1 FacetTopDocs (com.yelp.nrtsearch.server.luceneserver.facet.FacetTopDocs)1 NumberFieldFacetsTest.assertFacetResult (com.yelp.nrtsearch.server.luceneserver.facet.NumberFieldFacetsTest.assertFacetResult)1 FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)1 IndexableFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef)1