elasticsearch - 按数组中的id过滤并仅返回具有id的项目Elasticsearch 1.7

标签 elasticsearch

如何通过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/

相关文章:

elasticsearch - Elasticsearch 排序问题

php - elasticsearch-php创建索引返回\BadRequest400Exception

elasticsearch - Elasticsearch所有索引的计数

c# - ElasticSearch 7.x NEST 客户端与 ES 6.x 的兼容性

elasticsearch - gz 文件的 Filebeat

scala - 如何使elastic4s存储_timestamp字段?

search - 使用River插件时如何创建初始Elasticsearch设置

sorting - Elasticsearch-按输入顺序排序

elasticsearch - Elasticsearch/Nest-组合多个范围查询(OIC语法)

python - Elasticsearch 滚动结束不返回任何内容