Elasticsearch query plugin
I am upgrading Elasticsearch plugin from 2.4.0 to 5.4.0. This plugin implements a custom request, but with new changes to the Elasticsearch Java Api. I'm a little confused about how to register a new request. I search the Elasticsearch site and I found that I have to implement the interfaces SearchPlugin
and override the method getQueries
, but I am still confused and how to do it. Any help?
source to share
You need something like this (java8):
public class MyQueryPlugin extends Plugin implements SearchPlugin {
@Override public List<QuerySpec<?>> getQueries() {
return Arrays.asList(
new QuerySpec<>("my_query", MyQueryBuilder::new, MyQueryBuilder::fromXContent)
);
}
}
Or this (java 7):
public class MyQueryPlugin extends Plugin implements SearchPlugin {
@Override
public List<QuerySpec<?>> getQueries() {
return Arrays.asList(new QuerySpec<>(
"my_query",
new Writeable.Reader<MyQueryBuilder>() {
@Override
public MyQueryBuilder read(StreamInput in) throws IOException {return new MyQueryBuilder(in);}
},
new QueryParser<MyQueryBuilder>() {
@Override
public Optional<MyQueryBuilder> fromXContent(QueryParseContext parseContext) throws IOException {return MyQueryBuilder.fromXContent(parseContext);}
})
);
}
}
MyQueryBuilder
is likely to expand AbstractQueryBuilder<MyQueryBuilder>
. Most of the other queries ES provides extend this - they are a good source for copying.
MyQueryBuilder::fromXContent
- this is another change that swept me - it should do the same as org.elasticsearch.index.query.QueryParser#parse(QueryParseContext parseContext)
from 2.x.
MyQueryPlugin
is what you would specify in your plugin-descriptor.properties
as:
description=my-query example
version=1.0.0
name=my-query
java.version=1.8
elasticsearch.version=5.4.0
classname=com.example.MyQueryPlugin
source to share