When trying to implement a similar usecase as the elasticsearch doicumentation for the percolate query with the java api client I ran into an issue and wonder if I'm doing things wrong, or there is something missing in the java api.
Specifically it concerns registering a query in the percolator. Everything works fine with the HTTP Api but the JSON generated by the Java api client looks different.
I simplified my use case. If I have this mapping:
PUT /my-firstname-index
{
"mappings": {
"properties": {
"firstname": {
"type": "text"
},
"query": {
"type": "percolator"
}
}
}
}
I can register this query fine:
PUT /my-firstname-index/_doc/1?refresh
{
"query": {
"term": {
"firstname": {
"value": "Bob"
}
}
}
}
The term query will be saved to the 'query' field in the index just fine. However, when I try to do this with the Java api the JSON being generated looks like this:
{
"term": {
"firstname": {
"value": "Bob"
}
}
}
This will be saved in the index to the field 'term' (with the value {"firstname":{"value":"Bob"}} ). This doesn't really work when later retrieving the query since I specify the field where the query is saved as well. I tried around different ways of doing this but nothing gave me the same JSON as in the example/documentation. It feels this should be doable in the Java api client without messing around with JSON serialization and custom objects to pass as the 'document'.
My simple code looks like this:
client.index(index -> index
.index("my-firstname-index")
.id("1")
.document(
TermQuery.of(termsQuery -> termsQuery
.field("firstname")
.value("Bob")
)._toQuery()
)
.refresh(Refresh.True)
);
I have tried some simple variations on this code and some less straightforward ones but no luck so far. I havent really come across a working Java example of this either.