首页 > 解决方案 > 布尔查询格式错误

问题描述

错误消息: “[bool] 格式错误的查询,应为 [END_OBJECT] 但找到 [FIELD_NAME]”)

询问:

{'query':{
     'bool': {
          'must': {
               'bool': {
                    'should': [
                          {'match': {'title' : a[0]}},
                          {'match': {'title' : a[1]}}  
                              ],
                    'minimum_should_match': 1
                       }
                  }

                                           
              }
          },

 }


我已经搜索了几个问题,但仍然无法弄清楚查询中的错误位置。

更新:这完全是我的问题。我认为错误出在布尔查询上,所以我删除了一些我认为与原始问题无关的代码。实际查询是这样的:

search_body_try1 = {'query':{
                             'bool': {
                                 'must': {
                                     'bool': {
                                         'should': [
                                             {'match': {'title' : a[0]}},
                                             {'match': {'title' : a[1]}}  
                                                   ],
                                         'minimum_should_match': 1
                                             }
                                          }

                                           
                                     },
                              'sort':[{'_score': {'order':'desc'}},
                                      {'_id': {'order':'desc'}}]
                                 },
                            'size':200
                            }

现在在我看来问题是'sort'应该放在外面query

标签: elasticsearch

解决方案


查询确实格式不正确。sort需要与和处于同一水平size-query即类似于以下内容:

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "match": {
                  "title": a[0]
                }
              },
              {
                "match": {
                  "title": a[1]
                }
              }
            ],
            "minimum_should_match": 1
          }
        }
      ]
    }
  },
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    },
    {
      "_id": {
        "order": "desc"
      }
    }
  ],
  "size": 200
}

如果您有score字段,请替换_scorescore. 与_id.


推荐阅读