Search in sources :

Example 6 with FieldDefBindings

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

the class JsScriptBindingsTest method testGetBindingForParam.

@Test
public void testGetBindingForParam() throws IOException {
    Map<String, Object> params = new HashMap<>();
    params.put("param1", 100);
    params.put("param2", 1.11);
    JsScriptBindings bindings = new JsScriptBindings(new FieldDefBindings(Collections.emptyMap()), params);
    DoubleValuesSource p1Source = bindings.getDoubleValuesSource("param1");
    assertEquals(100D, p1Source.getValues(null, null).doubleValue(), 0.001);
    DoubleValuesSource p2Source = bindings.getDoubleValuesSource("param2");
    assertEquals(1.11, p2Source.getValues(null, null).doubleValue(), 0.001);
}
Also used : HashMap(java.util.HashMap) FieldDefBindings(com.yelp.nrtsearch.server.luceneserver.field.FieldDefBindings) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) Test(org.junit.Test)

Example 7 with FieldDefBindings

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

the class JsScriptBindingsTest method testExtendedIntIndex.

@Test(expected = IllegalArgumentException.class)
public void testExtendedIntIndex() {
    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[1].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 8 with FieldDefBindings

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

the class JsScriptBindingsTest method testExtendedInvalidPrefix.

@Test(expected = IllegalArgumentException.class)
public void testExtendedInvalidPrefix() {
    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("not_doc['field1'].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 9 with FieldDefBindings

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

the class RegisterFieldsHandler method parseOneVirtualFieldType.

private FieldDef parseOneVirtualFieldType(IndexState indexState, Map<String, FieldDef> pendingFieldDefs, String fieldName, Field currentField) throws RegisterFieldsException {
    ScoreScript.Factory factory = ScriptService.getInstance().compile(currentField.getScript(), ScoreScript.CONTEXT);
    Map<String, Object> params = ScriptParamsUtils.decodeParams(currentField.getScript().getParamsMap());
    // doing this (or maybe updating index state in general).
    if (currentField.getScript().getLang().equals(JsScriptEngine.LANG)) {
        params = new HashMap<>(params);
        Map<String, FieldDef> allFields = new HashMap<>(indexState.getAllFields());
        allFields.putAll(pendingFieldDefs);
        params.put("bindings", new FieldDefBindings(allFields));
    }
    DoubleValuesSource values = factory.newFactory(params, indexState.docLookup);
    return new VirtualFieldDef(fieldName, values);
}
Also used : IdFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IdFieldDef) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) ScoreScript(com.yelp.nrtsearch.server.luceneserver.script.ScoreScript) HashMap(java.util.HashMap) FieldDefBindings(com.yelp.nrtsearch.server.luceneserver.field.FieldDefBindings) JsonObject(com.google.gson.JsonObject) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)

Aggregations

FieldDefBindings (com.yelp.nrtsearch.server.luceneserver.field.FieldDefBindings)9 HashMap (java.util.HashMap)9 DoubleValuesSource (org.apache.lucene.search.DoubleValuesSource)9 Test (org.junit.Test)8 FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)7 VirtualFieldDef (com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef)7 JsonObject (com.google.gson.JsonObject)1 IdFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IdFieldDef)1 IndexableFieldDef (com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef)1 ScoreScript (com.yelp.nrtsearch.server.luceneserver.script.ScoreScript)1