Search in sources :

Example 1 with VirtualFieldDef

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

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

the class JsScriptBindingsTest method testExtendedInvalidName.

@Test(expected = IllegalArgumentException.class)
public void testExtendedInvalidName() {
    Map<String, Object> params = new HashMap<>();
    params.put("param1", 100);
    params.put("param2", 1.11);
    DoubleValuesSource field1Source = new DummyValuesSource();
    DoubleValuesSource field2Source = new DummyValuesSource();
    Map<String, FieldDef> fieldDefMap = new HashMap<>();
    fieldDefMap.put("field1", new VirtualFieldDef("field1", field1Source));
    fieldDefMap.put("field2", new VirtualFieldDef("field2", field2Source));
    JsScriptBindings bindings = new JsScriptBindings(new FieldDefBindings(fieldDefMap), params);
    bindings.getDoubleValuesSource("doc['invalid'].value");
}
Also used : VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) HashMap(java.util.HashMap) FieldDefBindings(com.yelp.nrtsearch.server.luceneserver.field.FieldDefBindings) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) Test(org.junit.Test)

Example 3 with VirtualFieldDef

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

the class JsScriptBindingsTest method testGetBindingForField.

@Test
public void testGetBindingForField() {
    Map<String, Object> params = new HashMap<>();
    params.put("param1", 100);
    params.put("param2", 1.11);
    DoubleValuesSource field1Source = new DummyValuesSource();
    DoubleValuesSource field2Source = new DummyValuesSource();
    Map<String, FieldDef> fieldDefMap = new HashMap<>();
    fieldDefMap.put("field1", new VirtualFieldDef("field1", field1Source));
    fieldDefMap.put("field2", new VirtualFieldDef("field2", field2Source));
    JsScriptBindings bindings = new JsScriptBindings(new FieldDefBindings(fieldDefMap), params);
    DoubleValuesSource f1Source = bindings.getDoubleValuesSource("field1");
    DoubleValuesSource f2Source = bindings.getDoubleValuesSource("field2");
    assertNotSame(f1Source, f2Source);
    assertSame(f1Source, field1Source);
    assertSame(f2Source, field2Source);
}
Also used : VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) HashMap(java.util.HashMap) FieldDefBindings(com.yelp.nrtsearch.server.luceneserver.field.FieldDefBindings) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) Test(org.junit.Test)

Example 4 with VirtualFieldDef

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

the class JsScriptBindingsTest method testInvalidName.

@Test(expected = IllegalArgumentException.class)
public void testInvalidName() {
    Map<String, Object> params = new HashMap<>();
    params.put("param1", 100);
    params.put("param2", 1.11);
    DoubleValuesSource field1Source = new DummyValuesSource();
    DoubleValuesSource field2Source = new DummyValuesSource();
    Map<String, FieldDef> fieldDefMap = new HashMap<>();
    fieldDefMap.put("field1", new VirtualFieldDef("field1", field1Source));
    fieldDefMap.put("field2", new VirtualFieldDef("field2", field2Source));
    JsScriptBindings bindings = new JsScriptBindings(new FieldDefBindings(fieldDefMap), params);
    bindings.getDoubleValuesSource("invalid");
}
Also used : VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) HashMap(java.util.HashMap) FieldDefBindings(com.yelp.nrtsearch.server.luceneserver.field.FieldDefBindings) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) Test(org.junit.Test)

Example 5 with VirtualFieldDef

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

the class JsScriptBindingsTest method testExtendedMissingProperty.

@Test(expected = IllegalArgumentException.class)
public void testExtendedMissingProperty() {
    Map<String, Object> params = new HashMap<>();
    params.put("param1", 100);
    params.put("param2", 1.11);
    DoubleValuesSource field1Source = new DummyValuesSource();
    DoubleValuesSource field2Source = new DummyValuesSource();
    Map<String, FieldDef> fieldDefMap = new HashMap<>();
    fieldDefMap.put("field1", new VirtualFieldDef("field1", field1Source));
    fieldDefMap.put("field2", new VirtualFieldDef("field2", field2Source));
    JsScriptBindings bindings = new JsScriptBindings(new FieldDefBindings(fieldDefMap), params);
    bindings.getDoubleValuesSource("doc['field1']");
}
Also used : VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) HashMap(java.util.HashMap) FieldDefBindings(com.yelp.nrtsearch.server.luceneserver.field.FieldDefBindings) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) Test(org.junit.Test)

Aggregations

FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)9 VirtualFieldDef (com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)9 HashMap (java.util.HashMap)8 FieldDefBindings (com.yelp.nrtsearch.server.luceneserver.field.FieldDefBindings)7 DoubleValuesSource (org.apache.lucene.search.DoubleValuesSource)7 Test (org.junit.Test)6 IndexableFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef)3 IndexState (com.yelp.nrtsearch.server.luceneserver.IndexState)2 ScoreScript (com.yelp.nrtsearch.server.luceneserver.script.ScoreScript)2 JsonObject (com.google.gson.JsonObject)1 ProtocolStringList (com.google.protobuf.ProtocolStringList)1 NumericRangeType (com.yelp.nrtsearch.server.grpc.NumericRangeType)1 VirtualField (com.yelp.nrtsearch.server.grpc.VirtualField)1 DoubleFieldDef (com.yelp.nrtsearch.server.luceneserver.field.DoubleFieldDef)1 FloatFieldDef (com.yelp.nrtsearch.server.luceneserver.field.FloatFieldDef)1 IdFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IdFieldDef)1 IntFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IntFieldDef)1 LongFieldDef (com.yelp.nrtsearch.server.luceneserver.field.LongFieldDef)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1