user3791980
user3791980

Reputation: 445

What is wrong with my elasticsearch query ? Getting a expected end object error

I'm trying to do a elasticsearch query that does geolocation filter and does some matching on nested documents, but I'm getting this error whenever I add in the nested query.

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

{
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "bool": {
      "filter": {
        "geo_distance": {
          "distance": "10km",
          "geolocation": [
            -73.980090948125,
            40.747844918436
          ]
        }
      },
      "must": {
        "multi_match": {
          "query": "New York",
          "fields": [
            "name^2",
            "city",
            "state",
            "zip"
          ],
          "type": "best_fields"
        }
      }
    },
    "nested": {
      "path": "amenities",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "amenities.name": "Pool"
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "reviews": {
      "nested": {
        "path": "reviews"
      },
      "aggs": {
        "avg_rating": {
          "avg": {
            "field": "reviews.rating"
          }
        }
      }
    }
  }
}

Upvotes: 2

Views: 3350

Answers (1)

Val
Val

Reputation: 217254

You just has misplaced the nested query, try like this:

{
  "sort": [
    {
      "_score": {
        "order": "desc"
      }
    }
  ],
  "query": {
    "bool": {
      "filter": {
        "geo_distance": {
          "distance": "10km",
          "geolocation": [
            -73.980090948125,
            40.747844918436
          ]
        }
      },
      "must": [
        {
          "multi_match": {
            "query": "New York",
            "fields": [
              "name^2",
              "city",
              "state",
              "zip"
            ],
            "type": "best_fields"
          }
        },
        {
          "nested": {
            "path": "amenities",
            "query": {
              "match": {
                "amenities.name": "Pool"
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "reviews": {
      "nested": {
        "path": "reviews"
      },
      "aggs": {
        "avg_rating": {
          "avg": {
            "field": "reviews.rating"
          }
        }
      }
    }
  }
}

Upvotes: 6

Related Questions