use of com.yelp.nrtsearch.server.grpc.VirtualField 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.grpc.VirtualField in project nrtsearch by Yelp.
the class ScoreScriptTest method testScriptUsingScore.
@Test
public void testScriptUsingScore() throws Exception {
GrpcServer.TestServer testAddDocs = new GrpcServer.TestServer(grpcServer, true, Mode.STANDALONE);
// 2 docs addDocuments
testAddDocs.addDocuments();
// manual refresh
grpcServer.getBlockingStub().refresh(RefreshRequest.newBuilder().setIndexName(grpcServer.getTestIndex()).build());
VirtualField virtualField = VirtualField.newBuilder().setName("test_field").setScript(Script.newBuilder().setLang("test_lang").setSource("verify_score").build()).build();
SearchResponse searchResponse = grpcServer.getBlockingStub().search(SearchRequest.newBuilder().setIndexName(grpcServer.getTestIndex()).setStartHit(0).setTopHits(10).addVirtualFields(virtualField).addRetrieveFields("test_field").setQueryText("vendor_name:first vendor").build());
assertEquals(2, searchResponse.getHitsCount());
assertEquals(2.0, searchResponse.getHits(0).getFieldsOrThrow("test_field").getFieldValue(0).getDoubleValue(), Math.ulp(2.0));
assertEquals(2.0, searchResponse.getHits(1).getFieldsOrThrow("test_field").getFieldValue(0).getDoubleValue(), Math.ulp(2.0));
}
use of com.yelp.nrtsearch.server.grpc.VirtualField in project nrtsearch by Yelp.
the class ScoreScriptTest method testParams.
@Test
public void testParams() throws Exception {
GrpcServer.TestServer testAddDocs = new GrpcServer.TestServer(grpcServer, true, Mode.STANDALONE);
// 2 docs addDocuments
testAddDocs.addDocuments();
// manual refresh
grpcServer.getBlockingStub().refresh(RefreshRequest.newBuilder().setIndexName(grpcServer.getTestIndex()).build());
VirtualField virtualField = VirtualField.newBuilder().setName("test_field").setScript(Script.newBuilder().setLang("test_lang").setSource("test_params").putParams("text_param", Script.ParamValue.newBuilder().setTextValue("text_val").build()).putParams("bool_param", Script.ParamValue.newBuilder().setBooleanValue(false).build()).putParams("int_param", Script.ParamValue.newBuilder().setIntValue(100).build()).putParams("long_param", Script.ParamValue.newBuilder().setLongValue(1001).build()).putParams("float_param", Script.ParamValue.newBuilder().setFloatValue(1.123F).build()).putParams("double_param", Script.ParamValue.newBuilder().setDoubleValue(3.456).build()).build()).build();
SearchResponse searchResponse = grpcServer.getBlockingStub().search(SearchRequest.newBuilder().setIndexName(grpcServer.getTestIndex()).setStartHit(0).setTopHits(10).addVirtualFields(virtualField).addRetrieveFields("test_field").build());
assertEquals(2, searchResponse.getHitsCount());
assertEquals(4.5, searchResponse.getHits(0).getFieldsOrThrow("test_field").getFieldValue(0).getDoubleValue(), Math.ulp(4.5));
assertEquals(4.5, searchResponse.getHits(1).getFieldsOrThrow("test_field").getFieldValue(0).getDoubleValue(), Math.ulp(4.5));
}
use of com.yelp.nrtsearch.server.grpc.VirtualField in project nrtsearch by Yelp.
the class ScoreScriptTest method testQueryFieldScript.
private void testQueryFieldScript(String source, String registerFieldsFile, String addDocsFile, double expectedScore) throws Exception {
GrpcServer.TestServer testAddDocs = new GrpcServer.TestServer(grpcServer, false, Mode.STANDALONE);
new GrpcServer.IndexAndRoleManager(grpcServer).createStartIndexAndRegisterFields(Mode.STANDALONE, 0, false, registerFieldsFile);
AddDocumentResponse addDocumentResponse = testAddDocs.addDocuments(addDocsFile);
// manual refresh
grpcServer.getBlockingStub().refresh(RefreshRequest.newBuilder().setIndexName(grpcServer.getTestIndex()).build());
VirtualField virtualField = VirtualField.newBuilder().setName("test_field").setScript(Script.newBuilder().setLang("test_lang").setSource(source).build()).build();
SearchResponse searchResponse = grpcServer.getBlockingStub().search(SearchRequest.newBuilder().setIndexName(grpcServer.getTestIndex()).setStartHit(0).setTopHits(10).addVirtualFields(virtualField).addRetrieveFields("test_field").build());
assertEquals(2, searchResponse.getHitsCount());
assertEquals(expectedScore, searchResponse.getHits(0).getFieldsOrThrow("test_field").getFieldValue(0).getDoubleValue(), Math.ulp(expectedScore));
assertEquals(expectedScore, searchResponse.getHits(1).getFieldsOrThrow("test_field").getFieldValue(0).getDoubleValue(), Math.ulp(expectedScore));
}
use of com.yelp.nrtsearch.server.grpc.VirtualField in project nrtsearch by Yelp.
the class JsScriptEngineTest method testExpressionsWithDifferentParams.
@Test
public void testExpressionsWithDifferentParams() throws Exception {
Map<String, Script.ParamValue> params1 = new HashMap<>();
params1.put("param_1", Script.ParamValue.newBuilder().setLongValue(10).build());
params1.put("param_2", Script.ParamValue.newBuilder().setFloatValue(2.22F).build());
params1.put("param_3", Script.ParamValue.newBuilder().setDoubleValue(1.11).build());
Map<String, Script.ParamValue> params2 = new HashMap<>();
params2.put("param_1", Script.ParamValue.newBuilder().setLongValue(7).build());
params2.put("param_2", Script.ParamValue.newBuilder().setFloatValue(3.33F).build());
params2.put("param_3", Script.ParamValue.newBuilder().setIntValue(25).build());
VirtualField virtualField1 = VirtualField.newBuilder().setName("expr_1").setScript(Script.newBuilder().setLang("js").setSource("param_3*long_field+param_2*int_field+param_1*float_field").putAllParams(params1).build()).build();
VirtualField virtualField2 = VirtualField.newBuilder().setName("expr_2").setScript(Script.newBuilder().setLang("js").setSource("param_3*long_field+param_2*int_field+param_1*float_field").putAllParams(params2).build()).build();
List<VirtualField> fields = Arrays.asList(virtualField1, virtualField2);
SearchResponse searchResponse = getGrpcServer().getBlockingStub().search(SearchRequest.newBuilder().setIndexName(DEFAULT_TEST_INDEX).setStartHit(0).setTopHits(10).addAllVirtualFields(fields).addRetrieveFields("expr_1").addRetrieveFields("expr_2").build());
assertEquals(2, searchResponse.getHitsCount());
assertEquals(1020.08, searchResponse.getHits(0).getFieldsOrThrow("expr_1").getFieldValue(0).getDoubleValue(), 0.001);
assertEquals(2033.5, searchResponse.getHits(1).getFieldsOrThrow("expr_1").getFieldValue(0).getDoubleValue(), 0.001);
assertEquals(1010.06, searchResponse.getHits(0).getFieldsOrThrow("expr_2").getFieldValue(0).getDoubleValue(), 0.001);
assertEquals(1823.45, searchResponse.getHits(1).getFieldsOrThrow("expr_2").getFieldValue(0).getDoubleValue(), 0.001);
}
Aggregations