refactor field funtion to use internal docmap to be used in scripts

This commit is contained in:
kimchy 2010-09-02 17:19:50 +03:00
parent 807c485a38
commit 1e75638b31
2 changed files with 95 additions and 74 deletions

View File

@ -39,7 +39,7 @@ import java.util.Set;
/**
* @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>>>() {
@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 Map<String, FieldData> localCacheFieldData = cachedFieldData.get().get();
final Object script;
IndexReader reader;
int docId;
final DocMap docMap;
public ScriptFieldsFunction(String script, ScriptService scriptService, MapperService mapperService, FieldDataCache fieldDataCache) {
this.scriptService = scriptService;
this.mapperService = mapperService;
this.fieldDataCache = fieldDataCache;
this.script = scriptService.compile(script);
this.docMap = new DocMap(cachedFieldData.get().get(), mapperService, fieldDataCache);
}
@Override public void setNextReader(IndexReader reader) {
this.reader = reader;
localCacheFieldData.clear();
docMap.setNextReader(reader);
}
@Override public Object execute(int docId, Map<String, Object> vars) {
this.docId = docId;
docMap.setNextDocId(docId);
if (vars == null) {
vars = cachedVars.get().get();
vars.clear();
}
vars.put("doc", this);
vars.put("doc", docMap);
return scriptService.execute(script, vars);
}
// --- 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) {
// assume its a string...
String fieldName = key.toString();
@ -163,3 +180,4 @@ public class ScriptFieldsFunction implements FieldsFunction, Map {
throw new UnsupportedOperationException();
}
}
}

View File

@ -110,6 +110,9 @@ public interface SearchHit extends Streamable, ToXContent, Iterable<SearchHitFie
*/
Explanation getExplanation();
/**
* The hit field matching the given field name.
*/
public SearchHitField field(String fieldName);
/**