如何在elasticsearch中查询嵌套字段中的多个参数

时间:2017-07-01 11:55:30

标签: elasticsearch

我试图搜索关键字,然后为设施添加嵌套查询,这是一个对象数组的嵌套字段。

通过下面的查询,我可以搜索我只匹配一个礼品ID,但是当我有多个礼品时,它不会返回任何内容。

任何人都知道我的查询有什么问题?

{
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "_geo_distance": {
        "geolocation": [
          100,
          10
        ],
        "order": "asc",
        "unit": "m",
        "mode": "min",
        "distance_type": "sloppy_arc"
      }
    }
  ],
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "fields": [
              "name^2",
              "city",
              "state",
              "zip"
            ],
            "fuzziness": 5,
            "query": "complete"
          }
        },
        {
          "nested": {
            "path": "amenities",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "amenities.id": "1"
                    }
                  },
                  {
                    "term": {
                      "amenities.id": "2"
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:1)

当你这样做时:

Sun Jul 02 '17 05.45 PM » Successful Login: JohnDoe
Sun Jul 02 '17 05.46 PM » Logout: JohnDoe

您实际上在说的是找到"must": [ { "term": { "amenities.id": "1" } }, { "term": { "amenities.id": "2" } }] 哪个文档,除非"amenities.id"="1" and "amenities.id"="2"是一个值列表,否则它将无法正常工作。 你可能想说的是找到"amenities.id"的任何文件 为此,您应该使用"amenities.id"="1" or "amenities.id"="2"代替should

must