Elasticsearch:字符串数组的精确匹配查询

Rem*_*sky 1 elasticsearch

鉴于此文档:

{"name": "Perfect Sunny-Side Up Eggs","ingredientList": ["canola oil","eggs"]}
Run Code Online (Sandbox Code Playgroud)

如何在弹性搜索中构建查询,以在给定查询术语“oil Eggs”的情况下返回字符串数组的精确匹配项,到目前为止,这是我所拥有的,但它返回其他不相关的文档:

POST /recipes/recipe/_search
{
   "query": {
      "match": {
         "ingredientList": {
            "query": [
               "oil",
               "eggs"
            ],
            "operator": "and"
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

例如,该文档被返回,但它不包含“oil”。结果应该只包含“油”和“鸡蛋”:

{"name": "Quick Baked French Toast","ingredientList": ["butter","cinnamon raisin bread","eggs"]}
Run Code Online (Sandbox Code Playgroud)

fyl*_*lie 5

您的查询将如下所示:

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "ingredientList": "oil"
          }
        },
        {
          "term": {
            "ingredientList": "eggs"
          }
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

给我结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "ingredients",
      "_type" : "recipe",
      "_id" : "AVeprXFrNutW6yNguPqp",
      "_score" : 1.0,
      "_source" : {
        "name" : "Perfect Sunny-Side Up Eggs",
        "ingredientList" : [ "canola oil", "eggs" ]
      }
    } ]
  }
}
Run Code Online (Sandbox Code Playgroud)