[英]Spring Data Elasticsearch - How use Criteria with nested fields
我正在使用 spring-data-elasticsearch (4.0.1) 和 elasticsearch 來查詢文檔。 我想使用 Criteria 對嵌套文檔進行嵌套查詢,我注釋了實體類,但無法使用引用嵌套字段的條件進行查詢。
{
"user" : {
"mappings" : {
"properties" : {
"contacts" : {
"type" : "nested",
"properties" : {
"description" : {
"type" : "text"
},
"id" : {
"type" : "long"
}
}
},
"id" : {
"type" : "long"
},
"name" : {
"type" : "text"
}
}
}
}
}
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "user",
"_type" : "_doc",
"_id" : "MgHFrXUBYAZ",
"_score" : 1.0,
"_source" : {
"id" : 1,
"name" : "Peter",
"contacts" : [
{
"id" : 1,
"description" : "foo"
},
{
"id" : 2,
"description" : "bar"
}
]
}
}
]
}
}
@Document(indexName = "user", createIndex = false)
public class User {
@Id
private Long id;
@Field(type = FieldType.Nested)
private List<Contacts> contactos;
}
public class Contacts {
@Field(type = FieldType.Long)
private Long id;
@Field(type = FieldType.Text)
private String description;
}
Criteria criteria = new Criteria("contacts.id").is(1);
CriteriaQuery query = new CriteriaQuery(criteria).setPageable(pageable);
SearchHits<User> searchHits = this.elasticsearchRestTemplate.search(query, User.class);
我沒有得到任何結果,我做錯了什么?
您可能希望使用 Spring Data 中的NativeSearchQueryBuilder實現此嵌套查詢。 這是一個關於它的外觀的片段:
QueryBuilder builder = nestedQuery("contactos", boolQuery().must(termQuery("contacts.id", 1)));
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
List<User> users = elasticsearchTemplate.queryForList(searchQuery, User.class);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.