我的文档就像:
class Foo{
private Integer idDl;
private String Name;
private String Add;
@Field(type = FieldType.Nested)
private List< Bar> Bar;
}
class Bar{
private Integer barId;
private List<String> barData
}
和Foo
样本数据是这样的:
{
"idDl": 123,
"Name": "ABCD",
"Add": "FL",
"Bar": [
{
"barId": 456,
"barData": [
"Bar1",
"Bar2"
]
},
{
"barId": 985,
"barData": [
"Bar4",
"Bar5"
]
}
]
}
我想返回所有匹配的Foo
对象,Bar.barId
但就像Bar
列表中的对象一样Foo
,Foo
只能包含一个Bar
ID与用户提供的ID匹配的对象.spring NativeSearchQueryBuilder
-data- elasticsearch提供的对象为:
String[] includeFields = new String[]{"idDl", "Name"};
String[] excludeFields = new String[]{"Add"}; // to exclude Add field of Foo
Query searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchQuery("Bar.barId", 456))
//.withQuery(termQuery("Bar.barId", 456))
.withSourceFilter(new FetchSourceFilter(includeFields, excludeFields))
.build();
return elasticsearchRestTemplate.queryForList( searchQuery, Foo.class);
我得到的响应包括所有Bar
对象,而与ID无关,这是示例响应:
[
{
"idDl": 123,
"Name": "ABCD",
"Add": "FL",
"Bar": [
{
"barId": 456,
"barData": [
"Bar1",
"Bar2"
]
},
{
"barId": 985,
"barData": [
"Bar4",
"Bar5"
]
}
]
},
{
"idDl": 758,
"Name": "PQR",
"Add": "NY",
"Bar": [
{
"barId": 456,
"barData": [
"Bar1",
"Bar2"
]
},
{
"barId": 671,
"barData": [
"Bar24",
"Bar25"
]
}
]
}
]
我尝试使用termQuery
摘要中的注释,但没有得到响应,因为matchQuery
我得到了如上所述的响应。在响应中,Bar
必须仅包含ID为456的对象,即在查询中发送的ID。任何建议都会有帮助
您正在查询Foo
存在Bar
与条件匹配的对象,而Elasticsearch返回这些对象Foo
。如果只想Bar
匹配s,则需要在查询中添加inner_hits。
检查此问题并回答如何检索这些内容,使用Spring Data Elasticsearch检索内部匹配将随版本4.1一起提供。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句