Search in sources :

Example 1 with CollectorCreatorContext

use of com.yelp.nrtsearch.server.luceneserver.search.collectors.CollectorCreatorContext in project nrtsearch by Yelp.

the class SearchRequestProcessor method buildContextForRequest.

/**
 * Create a {@link SearchContext} representing the given {@link SearchRequest} and index/searcher
 * state.
 *
 * @param searchRequest grpc request message
 * @param indexState index state
 * @param shardState shard state
 * @param searcherAndTaxonomy index searcher
 * @param profileResult container message for returned debug info
 * @return context info needed to execute the search query
 * @throws IOException if query rewrite fails
 */
public static SearchContext buildContextForRequest(SearchRequest searchRequest, IndexState indexState, ShardState shardState, SearcherTaxonomyManager.SearcherAndTaxonomy searcherAndTaxonomy, ProfileResult.Builder profileResult) throws IOException {
    SearchContext.Builder contextBuilder = SearchContext.newBuilder();
    SearchResponse.Builder responseBuilder = SearchResponse.newBuilder();
    contextBuilder.setIndexState(indexState).setShardState(shardState).setSearcherAndTaxonomy(searcherAndTaxonomy).setResponseBuilder(responseBuilder).setTimestampSec(System.currentTimeMillis() / 1000).setStartHit(searchRequest.getStartHit()).setTopHits(searchRequest.getTopHits());
    Map<String, FieldDef> queryVirtualFields = getVirtualFields(shardState, searchRequest);
    Map<String, FieldDef> queryFields = new HashMap<>(queryVirtualFields);
    addIndexFields(indexState, queryFields);
    contextBuilder.setQueryFields(Collections.unmodifiableMap(queryFields));
    Map<String, FieldDef> retrieveFields = getRetrieveFields(searchRequest, queryFields);
    contextBuilder.setRetrieveFields(Collections.unmodifiableMap(retrieveFields));
    Query query = extractQuery(indexState, searchRequest);
    if (profileResult != null) {
        profileResult.setParsedQuery(query.toString());
    }
    query = searcherAndTaxonomy.searcher.rewrite(query);
    if (profileResult != null) {
        profileResult.setRewrittenQuery(query.toString());
    }
    if (searchRequest.getFacetsCount() > 0) {
        query = addDrillDowns(indexState, query);
        if (profileResult != null) {
            profileResult.setDrillDownQuery(query.toString());
        }
    }
    contextBuilder.setFetchTasks(new FetchTasks(searchRequest.getFetchTasksList()));
    contextBuilder.setQuery(query);
    CollectorCreatorContext collectorCreatorContext = new CollectorCreatorContext(searchRequest, indexState, shardState, queryFields);
    DocCollector docCollector = buildDocCollector(collectorCreatorContext);
    contextBuilder.setCollector(docCollector);
    contextBuilder.setRescorers(getRescorers(indexState, searcherAndTaxonomy.searcher, searchRequest));
    contextBuilder.setSharedDocContext(new DefaultSharedDocContext());
    SearchContext searchContext = contextBuilder.build(true);
    // Give underlying collectors access to the search context
    docCollector.setSearchContext(searchContext);
    return searchContext;
}
Also used : DocCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.DocCollector) Query(org.apache.lucene.search.Query) DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) HashMap(java.util.HashMap) SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse) CollectorCreatorContext(com.yelp.nrtsearch.server.luceneserver.search.collectors.CollectorCreatorContext) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) DefaultSharedDocContext(com.yelp.nrtsearch.server.luceneserver.doc.DefaultSharedDocContext)

Example 2 with CollectorCreatorContext

use of com.yelp.nrtsearch.server.luceneserver.search.collectors.CollectorCreatorContext in project nrtsearch by Yelp.

the class SearchRequestProcessor method buildDocCollector.

/**
 * Build {@link DocCollector} to provide the {@link org.apache.lucene.search.CollectorManager} for
 * collecting hits for this query.
 *
 * @return collector
 */
private static DocCollector buildDocCollector(CollectorCreatorContext collectorCreatorContext) {
    SearchRequest searchRequest = collectorCreatorContext.getRequest();
    List<AdditionalCollectorManager<? extends Collector, ? extends CollectorResult>> additionalCollectors = searchRequest.getCollectorsMap().entrySet().stream().map(e -> CollectorCreator.getInstance().createCollectorManager(collectorCreatorContext, e.getKey(), e.getValue())).collect(Collectors.toList());
    if (searchRequest.getQuerySort().getFields().getSortedFieldsList().isEmpty()) {
        if (hasLargeNumHits(searchRequest)) {
            return new LargeNumHitsCollector(collectorCreatorContext, additionalCollectors);
        } else {
            return new RelevanceCollector(collectorCreatorContext, additionalCollectors);
        }
    } else {
        return new SortFieldCollector(collectorCreatorContext, additionalCollectors);
    }
}
Also used : SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse) ScriptParamsUtils(com.yelp.nrtsearch.server.utils.ScriptParamsUtils) SearcherTaxonomyManager(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager) Query(org.apache.lucene.search.Query) QueryRescorer(com.yelp.nrtsearch.server.grpc.QueryRescorer) PluginRescorer(com.yelp.nrtsearch.server.grpc.PluginRescorer) HashMap(java.util.HashMap) AdditionalCollectorManager(com.yelp.nrtsearch.server.luceneserver.search.collectors.AdditionalCollectorManager) VirtualField(com.yelp.nrtsearch.server.grpc.VirtualField) CollectorCreator(com.yelp.nrtsearch.server.luceneserver.search.collectors.CollectorCreator) ArrayList(java.util.ArrayList) QueryBuilder(org.apache.lucene.util.QueryBuilder) MultiFieldQueryParser(org.apache.lucene.queryparser.classic.MultiFieldQueryParser) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) Map(java.util.Map) DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) ShardState(com.yelp.nrtsearch.server.luceneserver.ShardState) ScoreScript(com.yelp.nrtsearch.server.luceneserver.script.ScoreScript) DefaultSharedDocContext(com.yelp.nrtsearch.server.luceneserver.doc.DefaultSharedDocContext) SimpleQueryParser(org.apache.lucene.queryparser.simple.SimpleQueryParser) QueryParserBase(org.apache.lucene.queryparser.classic.QueryParserBase) CollectorResult(com.yelp.nrtsearch.server.grpc.CollectorResult) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) CollectorCreatorContext(com.yelp.nrtsearch.server.luceneserver.search.collectors.CollectorCreatorContext) RescoreTask(com.yelp.nrtsearch.server.luceneserver.rescore.RescoreTask) QueryNodeMapper(com.yelp.nrtsearch.server.luceneserver.QueryNodeMapper) QueryRescore(com.yelp.nrtsearch.server.luceneserver.rescore.QueryRescore) RescoreOperation(com.yelp.nrtsearch.server.luceneserver.rescore.RescoreOperation) DocCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.DocCollector) IOException(java.io.IOException) IndexState(com.yelp.nrtsearch.server.luceneserver.IndexState) Collector(org.apache.lucene.search.Collector) ProfileResult(com.yelp.nrtsearch.server.grpc.ProfileResult) Collectors(java.util.stream.Collectors) LargeNumHitsCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.LargeNumHitsCollector) ScriptService(com.yelp.nrtsearch.server.luceneserver.script.ScriptService) SearchRequest(com.yelp.nrtsearch.server.grpc.SearchRequest) List(java.util.List) RescorerCreator(com.yelp.nrtsearch.server.luceneserver.rescore.RescorerCreator) SortFieldCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.SortFieldCollector) RelevanceCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.RelevanceCollector) Collections(java.util.Collections) IndexSearcher(org.apache.lucene.search.IndexSearcher) SearchRequest(com.yelp.nrtsearch.server.grpc.SearchRequest) CollectorResult(com.yelp.nrtsearch.server.grpc.CollectorResult) AdditionalCollectorManager(com.yelp.nrtsearch.server.luceneserver.search.collectors.AdditionalCollectorManager) LargeNumHitsCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.LargeNumHitsCollector) DocCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.DocCollector) Collector(org.apache.lucene.search.Collector) LargeNumHitsCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.LargeNumHitsCollector) SortFieldCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.SortFieldCollector) RelevanceCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.RelevanceCollector) RelevanceCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.RelevanceCollector) SortFieldCollector(com.yelp.nrtsearch.server.luceneserver.search.collectors.SortFieldCollector)

Aggregations

SearchResponse (com.yelp.nrtsearch.server.grpc.SearchResponse)2 DefaultSharedDocContext (com.yelp.nrtsearch.server.luceneserver.doc.DefaultSharedDocContext)2 FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)2 IndexableFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef)2 VirtualFieldDef (com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)2 CollectorCreatorContext (com.yelp.nrtsearch.server.luceneserver.search.collectors.CollectorCreatorContext)2 DocCollector (com.yelp.nrtsearch.server.luceneserver.search.collectors.DocCollector)2 HashMap (java.util.HashMap)2 DrillDownQuery (org.apache.lucene.facet.DrillDownQuery)2 CollectorResult (com.yelp.nrtsearch.server.grpc.CollectorResult)1 PluginRescorer (com.yelp.nrtsearch.server.grpc.PluginRescorer)1 ProfileResult (com.yelp.nrtsearch.server.grpc.ProfileResult)1 QueryRescorer (com.yelp.nrtsearch.server.grpc.QueryRescorer)1 SearchRequest (com.yelp.nrtsearch.server.grpc.SearchRequest)1 VirtualField (com.yelp.nrtsearch.server.grpc.VirtualField)1 IndexState (com.yelp.nrtsearch.server.luceneserver.IndexState)1 QueryNodeMapper (com.yelp.nrtsearch.server.luceneserver.QueryNodeMapper)1 ShardState (com.yelp.nrtsearch.server.luceneserver.ShardState)1 QueryRescore (com.yelp.nrtsearch.server.luceneserver.rescore.QueryRescore)1 RescoreOperation (com.yelp.nrtsearch.server.luceneserver.rescore.RescoreOperation)1