我正在尝试使用 hibernateSearh 6 查询 ElasticSearch。以下是发送到 ElasticSearch 的 Json 查询。根据此处的文档,它看起来很好: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"}}}
但是,收到以下异常消息:
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
}
以下是实体:
@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;
}
以下是从 Elastic 获取数据的代码,其中 predicateFunction 是:
(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 希望您传递查询本身,而无需包装器 JSON 对象。请参阅此处的示例。所以在你的情况下你应该通过:
{
"query_string":{
"fields":[
"addresses.address_key"
],
"query":"3g5g36ee-45b0-4636-79fe-9aaf446b7ab6"
}
}