我只想在ElasticSearch中搜索与多个字段匹配的用户,并在那里找到文档ID。目前,我有此代码只能匹配多个字段,并且运行良好

{
"query" :
    {
         "multi_match": {
             "fields": [
                "user",
                "email"
             ],
             "query": "John",
             "operator": "and",
             "type": "phrase_prefix"
         }
    }
}

但是现在只需要一点帮助,我如何才能确保仅搜索文档ID为[“5”,“4”]的文档?

我试过了
{
"query" :
    {
         "multi_match": {
             "fields": [
                "user",
                "email"
             ],
             "query": "John",
             "operator": "and",
             "type": "phrase_prefix"
         },
         "filter" : {
          "ids" : {
              "values" : ["5","4"]
             }
         }
    }
}

但这是行不通的。这是错的吗?在存储每个文档时,我给自己一个唯一的ID。我没有过滤器的查询结果是这样的
"hits": [
        {
            "_index": "MyIndex",
            "_type": "users",
            "_id": "4",
            "_score": 0.70710677,
            "_source": {
                "user": "John Ibraheem",
                "email": "johnibrhaeem@yahoo.com"
            }
        },
        {
            "_index": "MyIndex",
            "_type": "users",
            "_id": "9",
            "_score": 0.70710677,
            "_source": {
                "user": "Johnathan",
                "email": "johnathan@yahoo.com"
            }
        },
        {
            "_index": "MyIndex",
            "_type": "users",
            "_id": "5",
            "_score": 0.30685282,
            "_source": {
                "user": "Johnsons",
                "email": "johnsons@gmail.com"
            }
        }
    ]

我只想获取_id为[“5”,“4”]而不是“9”的John *。

最佳答案

试试这个

{
 "query":
 {
  "filtered":
  {
     "query":
     {
         "multi_match":
         {
            "fields": ["user","email"],
            "query": "i",
            "operator": "and",
            "type": "phrase_prefix"
         },
         "filter" : {
          "ids" : {
              "values" : ["5","9"]
             }
         }
      }
  }
 }
}

10-04 22:41
查看更多