【问题标题】:getting [bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]得到 [bool] 格式错误的查询,应为 [END_OBJECT] 但找到 [FIELD_NAME]
【发布时间】:2022-01-14 07:19:04
【问题描述】:

我在 elasticsearch 查询中使用嵌套以及排序。当我在没有排序查询的情况下执行时,但是当我包含排序时,它会给出消息 [bool] 格式错误的查询,预期 [END_OBJECT] 但找到 [FIELD_NAME]。我正在用 python 构建这个查询。提前致谢。

    query = {
            'bool': {
            'should': [
                {'nested': {
                    'path': 'profile.summay',
                    'query': {
                        'query_string': {
                            'query': 'machine learning',
                            'fields': ['profile.summay.desc'],
                            'default_operator': "AND"
                            }
                    }
                }},
                {'nested': {
                    'path': 'internal.summary',
                    'query': {
                        'query_string': {
                            'query': 'machine learning',
                            'fields': ['internal.summary.desc'],
                            'default_operator': "AND"
                        }
                    }
                }}
            ]
        },
        "sort": [
            {
                'profile.summary.date':{
                        'order' : 'asc',
                        "nested": { "path": "profile.summary" }
                }
            }
        ]
    }

from elasticsearch import Elasticsearch
es_client = Elasticsearch(['some aws url'])
response = es_client.search(index=index_name, query=query)

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    TLDR;

    你的sort 放错了。

    {
      "query": {
        "bool": {
          "should": [
            {
              "nested": {
                "path": "profile.summay",
                "query": {
                  "query_string": {
                    "query": "machine learning",
                    "fields": ["profile.summay.desc"],
                    "default_operator": "AND"
                  }
                }
              }
            },
            {
              "nested": {
                "path": "internal.summary",
                "query": {
                  "query_string": {
                    "query": "machine learning",
                    "fields": ["internal.summary.desc"],
                    "default_operator": "AND"
                  }
                }
              }
            }
          ]
        }
      },
      "sort": [
        {
          "profile.summary.date": {
            "order": "asc",
            "nested": { "path": "profile.summary" }
          }
        }
      ]
    }
    

    根据文档[doc]sort 键需要与query 键处于相同深度。没有更深的层次。

    你的代码需要从

    {
      "query": {
        ... query,
        
        "sort": [
          ... sort
        ]
      }
    }
    

    收件人:

    {
      "query": {
        ... query
      },
      "sort": [
        ... sort
      ]
    }
    

    【讨论】:

    • 我也尝试过这种方式,但出现“RequestError(400, 'parsing_exception', 'no [query] registered for [query]')”错误。在我使用 python 构建查询和传递与我在问题中发布的相同之间。
    • 啊,我更明白为什么您的查询格式感觉不对。 (1)反驳我对您的问题的编辑,(2)查看您正在调用的python函数的文档,它们必须是排序no的另一个参数?
    • 你使用什么库?也许发布你的代码的 sn-p
    • 我已经编辑了包含lib信息和代码sn-p的问题
    猜你喜欢
    • 2018-02-10
    • 1970-01-01
    • 2018-04-25
    • 2021-04-22
    • 2020-02-26
    • 1970-01-01
    • 2018-01-30
    • 2022-10-25
    • 2017-12-01
    相关资源
    最近更新 更多