pangpang
pangpang

Reputation: 8821

failed to parse search source. expected field name but got [START_OBJECT]

I want to express this SQL in Elasticsearch:

select * from ticket where user_id = 1 and (class_a = 1000010 or class_b = 16);

I use a combining filter as below:

curl 'localhost:9200/ticket/_search?pretty' -d'
{
    "query": {
        "bool": {
            "should": [
                {"term": {"class_a": 1000010}},
                {"term": {"class_b": 16}}
            ]
        },
        "filter": {
            "term": {
                "user_id": 1
            }
        }
    }
}'

but got the error as below:

{
  "error" : {
    "root_cause" : [ {
      "type" : "parse_exception",
      "reason" : "failed to parse search source. expected field name but got [START_OBJECT]"
    } ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query_fetch",
    "grouped" : true,
    "failed_shards" : [ {
      "shard" : 0,
      "index" : "ticket",
      "node" : "FO3-zhb1R1WCak381t88gQ",
      "reason" : {
        "type" : "parse_exception",
        "reason" : "failed to parse search source. expected field name but got [START_OBJECT]"
      }
    } ]
  },
  "status" : 400
}

Anyone can help me? Thanks in advance!

Upvotes: 0

Views: 1747

Answers (1)

Val
Val

Reputation: 217254

You're almost there, you need to rewrite your query like this (i.e. move your filter inside the bool clause):

curl 'localhost:9200/ticket/_search?pretty' -d'{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "term": {
            "class_a": 1000010
          }
        },
        {
          "term": {
            "class_b": 16
          }
        }
      ],
      "filter": {
        "term": {
          "user_id": 1
        }
      }
    }
  }
}'

Upvotes: 1

Related Questions