Elasticsearch在嵌套数组中搜索

时间:2014-11-20 09:55:20

标签: elasticsearch nested nosql

嗨我是弹性搜索新手,我遇到了查询问题。我有数据

{
    "user_id": "1",
    "date": "141462121",
    "name": "John",
    "surname": "Doe"
    "activity": [
      {
        "type": [
          "Outdoor",
          "Extreme"
        ],
        "name": "Example",
        "price": {
          "value": 50,
          "currency": "USD"
        }
      }
    ]
    "searchs": ""
  }
},
{
    "user_id": "2",
    "date": "141462121",
    "name": "Jane",
    "surname": "Doe"
    "activity": [
      {
        "type": [
          "Indoor"
        ],
        "name": "Example2",
        "price": {
          "value": 100,
          "currency": "USD"
        }
      },
      {
        "type": [
          "Outdoor"
        ],
        "name": "Example3",
        "price": {
          "value": 25,
          "currency": "USD"
        }
      }
    ]
    "searchs": ""
  }
}

我希望按活动类型搜索这些数据。我试过nested queries喜欢

"query": {
  "nested": {
     "path": "activity",
     "query": {
         "bool": {
             "must": [
                {"match": {
                   "activity.type": "outdoor"
                }}
           ]
       }
   }
}

并且喜欢

"query": {
  "nested": {
    "path": "activity",
    "query": {
      "nested": {
        "path": "type",
        "query": {
           "bool": {
               "must": [
                  {"match": {
                     "type.value": "outdoor"
                  }}
               ]
           }
        }
      }
    }
  }
}

但我无法成功。

如何按活动类型搜索这些数据?

1 个答案:

答案 0 :(得分:1)

如果您只是在查找活动类型为

的记录,则可以使用简单的匹配查询
{
  "query": {
    "match": {
      "activity.type": "Outdoor"
    }
  }
}