如何过滤掉与查询不匹配的数组中的元素?

时间:2015-09-24 08:09:05

标签: elasticsearch

stackoverflow不会让我编写那么多示例代码,所以我把它放在gist上。

所以我有index

使用此mapping

这里是sample document我插入新创建的映射

这是我的查询

GET products/paramSuggestions/_search
{
   "size": 10,
   "query": {
      "filtered": {
         "query": {
            "match": {
               "paramName": {
                  "query": "col",
                  "operator": "and"
               }
            }
         }
      }
   }
}

这是我从上一个查询中获得的不受欢迎的结果

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.33217794,
      "hits": [
         {
            "_index": "products",
            "_type": "paramSuggestions",
            "_id": "1",
            "_score": 0.33217794,
            "_source": {
               "productName": "iphone 6",
               "params": [
                  {
                     "paramName": "color",
                     "value": "white"
                  },
                  {
                     "paramName": "capacity",
                     "value": "32GB"
                  }
               ]
            }
         }
      ]
   }
}

最后想要结果,我希望查询结果看起来像

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.33217794,
      "hits": [
         {
            "_index": "products",
            "_type": "paramSuggestions",
            "_id": "1",
            "_score": 0.33217794,
            "_source": {
               "productName": "iphone 6",
               "params": [
                  {
                     "paramName": "color",
                     "value": "white"
                  },
               ]
            }
         }
      ]
   }
}

查询应如何使用与查询匹配的筛选数组字段来实现所需结果?换句话说,所有其他不匹配的数组项目不应出现在最终结果中

1 个答案:

答案 0 :(得分:0)

最终结果是您编入索引的_source文档。没有任何功能可以让您从Elasticsearch响应中掩盖文档的字段元素。

也就是说,根据您的目标,您可以查看HighlightersSuggesters如何识别与查询匹配的结果字词,或者可能使用{{3进行自己的客户端屏蔽在您的查询中设置info returned