refactor field funtion to use internal docmap to be used in scripts
This commit is contained in:
parent
807c485a38
commit
1e75638b31
|
@ -39,7 +39,7 @@ import java.util.Set;
|
||||||
/**
|
/**
|
||||||
* @author kimchy (shay.banon)
|
* @author kimchy (shay.banon)
|
||||||
*/
|
*/
|
||||||
public class ScriptFieldsFunction implements FieldsFunction, Map {
|
public class ScriptFieldsFunction implements FieldsFunction {
|
||||||
|
|
||||||
private static ThreadLocal<ThreadLocals.CleanableValue<Map<String, FieldData>>> cachedFieldData = new ThreadLocal<ThreadLocals.CleanableValue<Map<String, FieldData>>>() {
|
private static ThreadLocal<ThreadLocals.CleanableValue<Map<String, FieldData>>> cachedFieldData = new ThreadLocal<ThreadLocals.CleanableValue<Map<String, FieldData>>>() {
|
||||||
@Override protected ThreadLocals.CleanableValue<Map<String, FieldData>> initialValue() {
|
@Override protected ThreadLocals.CleanableValue<Map<String, FieldData>> initialValue() {
|
||||||
|
@ -53,44 +53,61 @@ public class ScriptFieldsFunction implements FieldsFunction, Map {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
final Object script;
|
|
||||||
|
|
||||||
final MapperService mapperService;
|
|
||||||
|
|
||||||
final FieldDataCache fieldDataCache;
|
|
||||||
|
|
||||||
final ScriptService scriptService;
|
final ScriptService scriptService;
|
||||||
|
|
||||||
final Map<String, FieldData> localCacheFieldData = cachedFieldData.get().get();
|
final Object script;
|
||||||
|
|
||||||
IndexReader reader;
|
final DocMap docMap;
|
||||||
|
|
||||||
int docId;
|
|
||||||
|
|
||||||
public ScriptFieldsFunction(String script, ScriptService scriptService, MapperService mapperService, FieldDataCache fieldDataCache) {
|
public ScriptFieldsFunction(String script, ScriptService scriptService, MapperService mapperService, FieldDataCache fieldDataCache) {
|
||||||
this.scriptService = scriptService;
|
this.scriptService = scriptService;
|
||||||
this.mapperService = mapperService;
|
|
||||||
this.fieldDataCache = fieldDataCache;
|
|
||||||
this.script = scriptService.compile(script);
|
this.script = scriptService.compile(script);
|
||||||
|
this.docMap = new DocMap(cachedFieldData.get().get(), mapperService, fieldDataCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public void setNextReader(IndexReader reader) {
|
@Override public void setNextReader(IndexReader reader) {
|
||||||
this.reader = reader;
|
docMap.setNextReader(reader);
|
||||||
localCacheFieldData.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public Object execute(int docId, Map<String, Object> vars) {
|
@Override public Object execute(int docId, Map<String, Object> vars) {
|
||||||
this.docId = docId;
|
docMap.setNextDocId(docId);
|
||||||
if (vars == null) {
|
if (vars == null) {
|
||||||
vars = cachedVars.get().get();
|
vars = cachedVars.get().get();
|
||||||
vars.clear();
|
vars.clear();
|
||||||
}
|
}
|
||||||
vars.put("doc", this);
|
vars.put("doc", docMap);
|
||||||
return scriptService.execute(script, vars);
|
return scriptService.execute(script, vars);
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Map implementation for doc field data lookup
|
// --- Map implementation for doc field data lookup
|
||||||
|
|
||||||
|
static class DocMap implements Map {
|
||||||
|
|
||||||
|
private final Map<String, FieldData> localCacheFieldData;
|
||||||
|
|
||||||
|
private final MapperService mapperService;
|
||||||
|
|
||||||
|
private final FieldDataCache fieldDataCache;
|
||||||
|
|
||||||
|
private IndexReader reader;
|
||||||
|
|
||||||
|
private int docId;
|
||||||
|
|
||||||
|
DocMap(Map<String, FieldData> localCacheFieldData, MapperService mapperService, FieldDataCache fieldDataCache) {
|
||||||
|
this.localCacheFieldData = localCacheFieldData;
|
||||||
|
this.mapperService = mapperService;
|
||||||
|
this.fieldDataCache = fieldDataCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNextReader(IndexReader reader) {
|
||||||
|
this.reader = reader;
|
||||||
|
localCacheFieldData.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNextDocId(int docId) {
|
||||||
|
this.docId = docId;
|
||||||
|
}
|
||||||
|
|
||||||
@Override public Object get(Object key) {
|
@Override public Object get(Object key) {
|
||||||
// assume its a string...
|
// assume its a string...
|
||||||
String fieldName = key.toString();
|
String fieldName = key.toString();
|
||||||
|
@ -162,4 +179,5 @@ public class ScriptFieldsFunction implements FieldsFunction, Map {
|
||||||
public Set entrySet() {
|
public Set entrySet() {
|
||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,6 +110,9 @@ public interface SearchHit extends Streamable, ToXContent, Iterable<SearchHitFie
|
||||||
*/
|
*/
|
||||||
Explanation getExplanation();
|
Explanation getExplanation();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The hit field matching the given field name.
|
||||||
|
*/
|
||||||
public SearchHitField field(String fieldName);
|
public SearchHitField field(String fieldName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue