弹性搜索嵌套查询构建

时间:2019-04-23 17:29:29

标签: elasticsearch nested

我是弹性搜索的新手,想为特定字段的所有值编写查询吗?我的意思是说我有一个字段“ Number”和“ change_manager_group” ...我尝试了下面的查询来打印过去12个小时中与“ change_manager_group”匹配的所有数字

curl -XGET "http://localhost:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
> {
>     "query": {
>       "bool": {
>         "must": [
>           {
>             "match_all": {}
>           },
>           {
>             "exists": {
>               "field": "Number"
>             }
>           },
>           {
>             "match_phrase": {
>               "change_manager_group": {
>                 "query": "Change Managers - 2"
>                }
>             }
>           },
>           {
>             "range": {
>               "actual_start": {
>                 "gte": 1555952852234,
>                 "lte": 1556039252234,
>                 "format": "epoch_millis"
>               }
>             }
>           }
>         ],
>         
>       }
>    }
> }
> '

不幸的是,当我运行上述查询时,出现以下错误:

{
  "error" : {
    "root_cause" : [
      {
        "type" : "json_parse_exception",
        "reason" : "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@5c8e3895; line: 32, column: 8]"
      }
    ],
    "type" : "json_parse_exception",
    "reason" : "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@5c8e3895; line: 32, column: 8]"
  },
  "status" : 500
}

我在这里做什么错了?

谢谢。

1 个答案:

答案 0 :(得分:0)

must数组后面有一个逗号,请将其删除以使其成为有效查询

curl -XGET "http://localhost:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
 {
     "query": {
       "bool": {
         "must": [
           {
             "match_all": {}
           },
           {
             "exists": {
               "field": "Number"
             }
           },
           {
             "match_phrase": {
               "change_manager_group": {
                 "query": "Change Managers - 2"
                }
             }
           },
           {
             "range": {
               "actual_start": {
                 "gte": 1555952852234,
                 "lte": 1556039252234,
                 "format": "epoch_millis"
               }
             }
           }
         ]                 <---- removed comma here

       }
    }
 }