Search in sources :

Example 1 with VirtualField

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;
}
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 2 with VirtualField

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));
}
Also used : VirtualField(com.yelp.nrtsearch.server.grpc.VirtualField) GrpcServer(com.yelp.nrtsearch.server.grpc.GrpcServer) SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse) Test(org.junit.Test)

Example 3 with VirtualField

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));
}
Also used : VirtualField(com.yelp.nrtsearch.server.grpc.VirtualField) GrpcServer(com.yelp.nrtsearch.server.grpc.GrpcServer) SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse) Test(org.junit.Test)

Example 4 with VirtualField

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));
}
Also used : VirtualField(com.yelp.nrtsearch.server.grpc.VirtualField) GrpcServer(com.yelp.nrtsearch.server.grpc.GrpcServer) AddDocumentResponse(com.yelp.nrtsearch.server.grpc.AddDocumentResponse) SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse)

Example 5 with VirtualField

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);
}
Also used : VirtualField(com.yelp.nrtsearch.server.grpc.VirtualField) HashMap(java.util.HashMap) SearchResponse(com.yelp.nrtsearch.server.grpc.SearchResponse) Test(org.junit.Test)

Aggregations

VirtualField (com.yelp.nrtsearch.server.grpc.VirtualField)5 SearchResponse (com.yelp.nrtsearch.server.grpc.SearchResponse)4 GrpcServer (com.yelp.nrtsearch.server.grpc.GrpcServer)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 AddDocumentResponse (com.yelp.nrtsearch.server.grpc.AddDocumentResponse)1 IndexState (com.yelp.nrtsearch.server.luceneserver.IndexState)1 FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)1 IndexableFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef)1 VirtualFieldDef (com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)1 ScoreScript (com.yelp.nrtsearch.server.luceneserver.script.ScoreScript)1