Search in sources :

Example 1 with FieldDef

use of com.yelp.nrtsearch.server.luceneserver.field.FieldDef in project nrtsearch by Yelp.

the class SearchRequestProcessor method getRetrieveFields.

/**
 * Get map of fields that need to be retrieved for the given request.
 *
 * @param request search requests
 * @param queryFields all valid fields for this query
 * @return map of all fields to retrieve
 * @throws IllegalArgumentException if a field does not exist, or is not retrievable
 */
private static Map<String, FieldDef> getRetrieveFields(SearchRequest request, Map<String, FieldDef> queryFields) {
    Map<String, FieldDef> retrieveFields = new HashMap<>();
    for (String field : request.getRetrieveFieldsList()) {
        FieldDef fieldDef = queryFields.get(field);
        if (fieldDef == null) {
            throw new IllegalArgumentException("RetrieveFields: " + field + " does not exist");
        }
        if (!isRetrievable(fieldDef)) {
            throw new IllegalArgumentException("RetrieveFields: " + field + " is not retrievable, must be stored" + " or have doc values enabled");
        }
        retrieveFields.put(field, fieldDef);
    }
    return retrieveFields;
}
Also used : VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) HashMap(java.util.HashMap)

Example 2 with FieldDef

use of com.yelp.nrtsearch.server.luceneserver.field.FieldDef 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 3 with FieldDef

use of com.yelp.nrtsearch.server.luceneserver.field.FieldDef in project nrtsearch by Yelp.

the class SearchRequestProcessor method getVirtualFields.

/**
 * Parses any virtualFields, which define dynamic (expression) fields for this one request.
 *
 * @throws IllegalArgumentException if there are multiple virtual fields with the same name
 */
private static Map<String, FieldDef> getVirtualFields(ShardState shardState, SearchRequest searchRequest) {
    if (searchRequest.getVirtualFieldsList().isEmpty()) {
        return new HashMap<>();
    }
    IndexState indexState = shardState.indexState;
    Map<String, FieldDef> virtualFields = new HashMap<>();
    for (VirtualField vf : searchRequest.getVirtualFieldsList()) {
        if (virtualFields.containsKey(vf.getName())) {
            throw new IllegalArgumentException("Multiple definitions of Virtual field: " + vf.getName());
        }
        ScoreScript.Factory factory = ScriptService.getInstance().compile(vf.getScript(), ScoreScript.CONTEXT);
        Map<String, Object> params = ScriptParamsUtils.decodeParams(vf.getScript().getParamsMap());
        FieldDef virtualField = new VirtualFieldDef(vf.getName(), factory.newFactory(params, indexState.docLookup));
        virtualFields.put(vf.getName(), virtualField);
    }
    return virtualFields;
}
Also used : VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) VirtualField(com.yelp.nrtsearch.server.grpc.VirtualField) ScoreScript(com.yelp.nrtsearch.server.luceneserver.script.ScoreScript) HashMap(java.util.HashMap) IndexState(com.yelp.nrtsearch.server.luceneserver.IndexState) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)

Example 4 with FieldDef

use of com.yelp.nrtsearch.server.luceneserver.field.FieldDef in project nrtsearch by Yelp.

the class SortParser method parseSort.

/**
 * Decodes a list of request {@link SortType} into the corresponding {@link Sort}.
 *
 * @param fields list of {@link SortType} from grpc request
 * @param sortFieldNames mutable list which will have all sort field names added in sort order,
 *     may be null
 * @param queryFields collection of all possible fields which may be used to sort
 */
public static Sort parseSort(List<SortType> fields, List<String> sortFieldNames, Map<String, FieldDef> queryFields) throws SearchHandler.SearchHandlerException {
    List<SortField> sortFields = new ArrayList<>();
    for (SortType sub : fields) {
        String fieldName = sub.getFieldName();
        SortField sf;
        if (sortFieldNames != null) {
            sortFieldNames.add(fieldName);
        }
        if (fieldName.equals(DOCID)) {
            if (!sub.getReverse()) {
                sf = SortField.FIELD_DOC;
            } else {
                sf = new SortField(null, SortField.Type.DOC, true);
            }
        } else if (fieldName.equals(SCORE)) {
            if (!sub.getReverse()) {
                sf = SortField.FIELD_SCORE;
            } else {
                sf = new SortField(null, SortField.Type.SCORE, true);
            }
        } else {
            FieldDef fd = queryFields.get(fieldName);
            if (fd == null) {
                throw new SearchHandler.SearchHandlerException(String.format("field: %s was not registered and was not specified as a virtualField", fieldName));
            }
            if (!(fd instanceof Sortable)) {
                throw new IllegalArgumentException(String.format("field: %s does not support sorting", fieldName));
            }
            sf = ((Sortable) fd).getSortField(sub);
        }
        sortFields.add(sf);
    }
    return new Sort(sortFields.toArray(new SortField[0]));
}
Also used : SortType(com.yelp.nrtsearch.server.grpc.SortType) SearchHandler(com.yelp.nrtsearch.server.luceneserver.SearchHandler) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) Sortable(com.yelp.nrtsearch.server.luceneserver.field.properties.Sortable) ArrayList(java.util.ArrayList) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField)

Example 5 with FieldDef

use of com.yelp.nrtsearch.server.luceneserver.field.FieldDef in project nrtsearch by Yelp.

the class EagerGlobalOrdinalsTest method testEagerOrdinals.

@Test
public void testEagerOrdinals() throws Exception {
    SearcherTaxonomyManager.SearcherAndTaxonomy s = null;
    ShardState shardState = getGlobalState().getIndex(DEFAULT_TEST_INDEX).getShard(0);
    try {
        FieldDef fieldDef = getGlobalState().getIndex(DEFAULT_TEST_INDEX).getField(EAGER_FIELD);
        addDocAndRefresh();
        s = shardState.acquire();
        assertGlobalOrdinals(s.searcher.getIndexReader(), fieldDef);
        doQuery(EAGER_FIELD);
        assertGlobalOrdinals(s.searcher.getIndexReader(), fieldDef);
        // create new reader version
        addDocAndRefresh();
        shardState.release(s);
        s = null;
        s = shardState.acquire();
        assertGlobalOrdinals(s.searcher.getIndexReader(), fieldDef);
    } finally {
        if (s != null) {
            shardState.release(s);
        }
    }
}
Also used : FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) ShardState(com.yelp.nrtsearch.server.luceneserver.ShardState) SearcherTaxonomyManager(org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager) Test(org.junit.Test)

Aggregations

FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)26 IndexableFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef)16 VirtualFieldDef (com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)13 HashMap (java.util.HashMap)12 Test (org.junit.Test)8 FieldDefBindings (com.yelp.nrtsearch.server.luceneserver.field.FieldDefBindings)7 DoubleValuesSource (org.apache.lucene.search.DoubleValuesSource)7 IdFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IdFieldDef)4 ArrayList (java.util.ArrayList)3 JsonObject (com.google.gson.JsonObject)2 SortType (com.yelp.nrtsearch.server.grpc.SortType)2 IndexState (com.yelp.nrtsearch.server.luceneserver.IndexState)2 ShardState (com.yelp.nrtsearch.server.luceneserver.ShardState)2 GeoQueryable (com.yelp.nrtsearch.server.luceneserver.field.properties.GeoQueryable)2 Sortable (com.yelp.nrtsearch.server.luceneserver.field.properties.Sortable)2 TermQueryable (com.yelp.nrtsearch.server.luceneserver.field.properties.TermQueryable)2 ScoreScript (com.yelp.nrtsearch.server.luceneserver.script.ScoreScript)2 SearcherTaxonomyManager (org.apache.lucene.facet.taxonomy.SearcherTaxonomyManager)2 Sort (org.apache.lucene.search.Sort)2 SortField (org.apache.lucene.search.SortField)2