类 若 实现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接口的类