galdikas
galdikas

Reputation: 1669

Issue with elastic search when using nested query

I have following query:

{
    "from": 0,
    "size": 20,
    "sort": {
        "prices_count": "desc"
    },
    "query": {
        "bool": {
            "must": [{
                "terms": {
                    "category_ids": ["3"]
                }
            }, {
                "terms": {
                    "manufacturer_id": ["5"]
                }
            }, {
                "range": {
                    "prices_count": {
                        "gte": 1
                    }
                }
            }]
        },
        "nested": {
            "bool": {
                "must": [{
                    "match": {
                        "specs.model": "iphone-6s"
                    }
                }]
            }
        }
    }
}

I get the following error:

Fatal error: Uncaught exception 'Elastica\Exception\ResponseException' with message 'failed to parse search source. expected field name but got [START_OBJECT]' in

If i leave just the nested part in the query, it works as expected.

Is it not possible to have and 'ordinary' and nested query in the same request, or am I just doing it wrong?

Upvotes: 0

Views: 52

Answers (1)

Val
Val

Reputation: 217304

You need to write it like this:

{
  "from": 0,
  "size": 20,
  "sort": {
    "prices_count": "desc"
  },
  "query": {
    "bool": {
      "must": [
        {
          "terms": {
            "category_ids": [
              "3"
            ]
          }
        },
        {
          "terms": {
            "manufacturer_id": [
              "5"
            ]
          }
        },
        {
          "range": {
            "prices_count": {
              "gte": 1
            }
          }
        },
        {
          "nested": {
            "path": "specs",
            "query": {
              "match": {
                "specs.model": "iphone-6s"
              }
            }
          }
        }
      ]
    }
  }
}

Upvotes: 1

Related Questions