2

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

1 Answer 1

6

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"
          }
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.