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;
}
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;
}
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;
}
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]));
}
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);
}
}
}
Aggregations