sagarpavan
sagarpavan

Reputation: 95

getting [bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

I am using nested inside the elasticsearch query along with sort. when I execute without sort query is working but when I include sort it gives the messages [bool] malformed query, expected [END_OBJECT] but found [FIELD_NAME]. I am building this query in python. Thanks in advance.

    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)

Upvotes: 1

Views: 667

Answers (1)

Paulo
Paulo

Reputation: 10346

TLDR;

You have a your sort placed wrong.

{
  "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" }
      }
    }
  ]
}

According to the documentation [doc], the sort key need to be on the same depth as the query key. Not a level deeper.

You code needs to go from

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

To:

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

Upvotes: 1

Related Questions