類 若 實現NativeScriptFactory接口。A factory to create instances of either {@link ExecutableScript} or {@link SearchScript}
只是一個工廠類,仍需要 創建 上面二者之一。實際中 需 創建 類 繼承?SearchScript接口的實現類AbstractSearchScript 的?子類@ AbstractLongSearchScript?@AbstractDoubleSearchScript。
?
我們使用 它是因為public List<NativeScriptFactory> getNativeScripts() 需要返回的是工廠。?
?
?
NativeScriptFactory
/**
* A factory to create instances of either {@link ExecutableScript} or {@link SearchScript}. Note,
* if this factory creates {@link SearchScript}, it must extend {@link AbstractSearchScript}.
*
* @see AbstractExecutableScript
* @see AbstractSearchScript
* @see AbstractLongSearchScript
* @see AbstractDoubleSearchScript
*/
public interface NativeScriptFactory {
?
ExecutableScript (一般不用,忽略)
* An executable script, can't be used concurrently.
?
SearchScript??接口
AbstractSearchScript (核心類,提供了絕大部分功能的實現)
/**
* A base class for any script type that is used during the search process (custom score, aggs, and so on).
* <p>
* If the script returns a specific numeric type, consider overriding the type specific base classes
* such as {@link AbstractDoubleSearchScript} and {@link AbstractLongSearchScript}
* for better performance.
* <p>
* The use is required to implement the {@link #run()} method.
*/
public abstract class AbstractSearchScript extends AbstractExecutableScript implements LeafSearchScript {
?
它 的核心是 屬性:
private LeafSearchLookup lookup;
private Scorer scorer;
所有方法的實現同和這兩個屬性有關。
setLookup()實現lookup的初始化
通過SearchLookup調用lookup.getLeafSearchLookup(context)實現
searchLookUp則通過DefaultSearchContext.lookup()實現初始化
lookup(): getQueryShardContext().lookup();
DefaultSearchContext則通過createContext實現初始化
也就是通過QueryShardContext.lookup() 實現。
QueryShardContext :?lookup = new SearchLookup(getMapperService(), indexFieldDataService, types);
? 其主要屬性 及初始化:?
public class SearchLookup {
final DocLookup docMap;
final SourceLookup sourceLookup;
final FieldsLookup fieldsLookup;
public SearchLookup(MapperService mapperService, IndexFieldDataService fieldDataService, @Nullable String[] types) {
docMap = new DocLookup(mapperService, fieldDataService, types);
sourceLookup = new SourceLookup();
fieldsLookup = new FieldsLookup(mapperService, types);
}
?
public class FieldsLookup {
private final MapperService mapperService;
@Nullable
private final String[] types;
FieldsLookup(MapperService mapperService, @Nullable String[] types) {
this.mapperService = mapperService;
this.types = types;
}
?
然后追蹤 傳參的來源:
queryShardContext.setTypes(ShardSearchRequest.types());
?
LocalTransport.sendRequest()
targetTransport.receiveMessage(version, data, action, requestId, this);
processReceivedMessage(data, action, sourceTransport, version, requestId);
StreamInput stream = StreamInput.wrap(data);
handleRequest(stream, requestId, data.length, sourceTransport, version);
request.readFrom(stream);
TaskId.readFromStream(in);
ShardSearchTransportRequest.readFrom()
shardSearchLocalRequest.innerReadFrom(in);
types = in.readStringArray();
總結: 數據有了,直接用
?
?plsSearchScript 繼承自AbstractSearchScript 類。
?覆寫了run(),run方法會執行plsExScript接口的run().
我們只需要提供一個實現plsExScirpt接口的類
?