简体   繁体   English

Elasticsearch查询:范围限制的语法给出400

[英]Elasticsearch query: syntax for range restriction gives 400

I'm using python to communicate with an ElasticSearch server. 我正在使用python与ElasticSearch服务器通信。 I'm using the elasticsearch package, and I'm formatting queries and feeding them to the search function in the body parameter. 我正在使用elasticsearch软件包,并且正在格式化查询并将它们输入到body参数中的搜索功能中。

Sending queries worked just fine. 发送查询效果很好。 For example, this query works: 例如,此查询有效:

{'query': {'constant_score': {'filter': {'bool': {'must': {'terms': {'id.keyword': ['d42bdc8a-a38b-43fa-9283-13b5e5c08c6e']}}}}}}}

I now want to restrict the range, so I add a little segment (indentation for clarity): 现在,我想限制范围,因此我添加了一个小段(为清楚起见,缩进):

{'query': 
    {'constant_score': 
        {'filter': 
            {'bool': 
                {'must': 
                    {'range': 
                        {'startTime': 
                            {'format': "yyyy-MM-dd'T'HH:mm:ss.SSS",
                             'gte': '2018-01-20T17:19:43.393',
                             'lte': '2018-04-01T17:19:43.393'}
                        },
                     'terms':
                         {'id.keyword':
                             ['d42bdc8a-a38b-43fa-9283-13b5e5c08c6e']
                         }
                     }
                 }
             }
         }
     }
}

A query that looks identical (to me) worked in R. I'm getting a status 400 though (bad request). 在我看来,一个看起来完全相同的查询在R中工作。尽管(错误请求),我的状态还是为400。 Does anyone see what the problem is? 有人看到问题出在哪里吗?

You're almost there, this is the right query that will work: 您快到了,这将是正确的查询,它将起作用:

{'query': 
    {'constant_score': 
        {'filter': 
            {'bool': 
                {'must': [
                    {'range': 
                        {'startTime': 
                            {'format': "yyyy-MM-dd'T'HH:mm:ss.SSS",
                             'gte': '2018-01-20T17:19:43.393',
                             'lte': '2018-04-01T17:19:43.393'}
                        }
                    },
                    {
                     'terms':
                         {'id.keyword':
                             ['d42bdc8a-a38b-43fa-9283-13b5e5c08c6e']
                         }
                    }
                 ]
               }
             }
         }
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM