我尝试将我的自定义插件从 ES5.5 升级到 ES6.3。 在ES5.5中,我自己的插件运行良好。但是在 ES6.3 中,QuerySpec 构造函数无法推断它。 以下是 ES6.3 中的代码片段:
- 抽象向量查询
public abstract class AbstractVectorQuery extends Query
{
protected String field;
protected float[] qVector;
protected VectorFieldType vectorFieldType;
protected DistanceMeasure distHelper;
protected AbstractVectorQuery(String field, float[] qVector, DistanceMeasure distHelper, VectorFieldType fieldType)
{
this.field = field;
this.qVector = qVector;
this.distHelper = distHelper;
this.vectorFieldType = fieldType;
}
}
- VectorIterQuery
public class VectorIterQuery extends AbstractVectorQuery
{
public VectorIterQuery(String field, float[] vector, DistanceMeasure distHelper, VectorFieldType vectorFieldType)
{
super(field, vector, distHelper, vectorFieldType);
}
}
- DistanceVectorQueryBuilder
public abstract class DistanceVectorQueryBuilder<T extends DistanceVectorQueryBuilder<T>>
extends AbstractVectorQueryBuilder<T>
{
public static final DistanceType DEFAULT_DISTANCE_TYPE = DistanceType.L2;
protected DistanceType distanceType = DEFAULT_DISTANCE_TYPE;
protected abstract void innerInnerDoXContent(XContentBuilder builder, Params params) throws IOException;
protected DistanceVectorQueryBuilder(){}
protected DistanceVectorQueryBuilder(StreamInput in) throws IOException
{
super(in);
this.distanceType = in.readEnum(DistanceType.class);
}
}
- IterVectorQueryBuilder
public class IterVectorQueryBuilder extends DistanceVectorQueryBuilder<IterVectorQueryBuilder>
{
public static final String QUERY_NAME = "iter";
public IterVectorQueryBuilder(StreamInput input) throws IOException
{
super(input);
}
}
- 矢量插件
在这里,IDE 报告:
无法推断参数(无法解析构造函数(
public class VectorPlugin extends Plugin implements MapperPlugin, SearchPlugin
{
@Override
public List<QuerySpec<?>> getQueries()
{
List<QuerySpec<?>> list = new ArrayList<>();
QuerySpec<IterVectorQueryBuilder> iterQuery = new QuerySpec<>(
IterVectorQueryBuilder.QUERY_NAME,
IterVectorQueryBuilder::new,
IterVectorQueryBuilder::fromXContent
);
list.add(iterQuery);
}
}
任何建议都会非常感激。
解决了,实际上问题在我发布的代码片段中不存在,很抱歉造成误解。
更改 IterVectorQueryBuilder 的返回类型::fromXContent 从
Optional<IterVectorQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException { ... }
自
IterVectorQueryBuilder fromXContent(QueryParseContext parseContext) throws IOException { ... }
会解决这个问题。