如何通过offers
字段过滤id
数组并返回具有搜索ID的结果对象?
当前搜索正确地通过offer数组中的id查找数据,但它还会返回所有对象:
GET activities/activity/_search
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"offers.id": "12"
}
}
]
}
}
}
}
}
当前结果,我想过滤报价并仅使用
"id": 12
获得:"hits": [
{
"_index": "activities",
"_type": "activity",
"_id": "AVtr4-UV81wMr8KFD246",
"_score": null,
"_source": {
"offers": [
{
"title": "merge",
"id": 11
},
{
"title": "order test",
"id": 12
}
],
"event": "candidate_remove",
"created_at": "2017-04-14T09:55:49.115174Z"
}
}
]
Activity 类型的商品映射:
"offers": {
"type": "nested",
"include_in_parent": true,
"properties": {
"id": {
"type": "long"
},
"title": {
"type": "string",
"index": "not_analyzed"
}
}
},
最佳答案
您需要inner_hits
功能和nested
查询:
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "offers",
"query": {
"term": {
"offers.id": "12"
}
},
"inner_hits":{}
}
}
]
}
}
}
}
}
这将在响应中添加另一个名为
inner_hits
的部分,其中显示匹配的嵌套文档。如果您不需要原始的
offers
值,则可以在原始查询中添加以下内容:{
"_source": {"exclude": "offers"},
"query": {
"filtered": {
关于elasticsearch - 按数组中的id过滤并仅返回具有id的项目Elasticsearch 1.7,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410279/