I am trying to query ElasticSearch using hibernateSearh 6. Following is the Json query which is sent to ElasticSearch. It looks fine as per documentation here : https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
{"query":{"query_string":{"fields":["addresses.address_key"],"query":"3g5g36ee-45b0-4636-79fe-9aaf446b7ab6"}}}
Howver, getting following Exception with message:
org.hibernate.search.util.common.SearchException: HSEARCH400007: Elasticsearch request failed: HSEARCH400090: Elasticsearch response indicates a failure.
Request: POST /employee-read/_search with parameters {from=0, size=10, track_total_hits=true}
Response: 400 'Bad Request' from 'http://localhost:9200' with body
{
"error": {
"root_cause": [
{
"type": "parsing_exception",
"reason": "unknown query [query]",
"line": 1,
"col": 19
}
],
"type": "parsing_exception",
"reason": "unknown query [query]",
"line": 1,
"col": 19,
"caused_by": {
"type": "named_object_not_found_exception",
"reason": "[1:19] unknown field [query]"
}
},
"status": 400
}
Here are the entities:
@Indexed(index = "employee")
public class Employee {
@FullTextField(name = "employee_key")
private String employeeKey;
@FullTextField(name = "first_name")
private String firstName;
@IndexedEmbedded(includeEmbeddedObjectId = true, includeDepth = 2)
private Address addresses;
}
public class Address {
@FullTextField(name = "address_key")
private String addressKey;
@FullTextField(name = "street_name")
private String streetName;
}
and following is the code to fetch data from Elastic where predicateFunction is: (ElasticsearchSearchPredicateFactory f) -> f.fromJson(queryJson)
SearchSession searchSession = Search.session(entityManager);
SearchResult<Employee> searchResult = searchSession.search(Employee.class)
.extension(ElasticsearchExtension.get())
.where(searchPredicateFactory -> {
return predicateFunction.apply(searchPredicateFactory);
})
.fetch(Math.toIntExact(page.getOffset()), page.getPageSize());
Hibernate Search expects that you'd pass the query itself without the wrapper JSON object. See an example here. So in your case you should pass a:
{
"query_string":{
"fields":[
"addresses.address_key"
],
"query":"3g5g36ee-45b0-4636-79fe-9aaf446b7ab6"
}
}