smartius
smartius

Reputation: 653

Combine filtered and bool query

i am trying to get the closest locations to the current location with the following query and it runs very well.

{
  "from": 0,
  "size": 3,
  "query": {
    "filtered": {
      "query": {
        "matchAll": {}
      },
      "filter": {
        "exists": {
          "field": "pin"
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "pin": {
          "lat": 52.51515,
          "lon": 13.38019
        },
        "order": "asc",
        "unit": "km",
        "distance_type":"arc"
      }
    }
  ]
}

Furthermore, i want to combine the query above with a bool query, cause i don't want to have the current location ( which is the closest one cause the data for _geo_distance is the current locations data ) in my result list. I know that i could simply set from to 1 and size to 4, but i want to know how to combine the query above with bool one.

    "bool": {
      "must_not": [
        {
          "query": {
            "matchAll": {
              "slug": "adlon-hotel"
            }
          }
        }
      ]
    }

I just read the docs but i didn't copy how to combine both queries.

Thank you.

Upvotes: 2

Views: 109

Answers (1)

smartius
smartius

Reputation: 653

I just answered it myself

{
  "from": 0,
  "size": 3,
  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must_not": [
            {
              "match": {
                "slug": "adlon-hotel"
              }
            }
          ]
        }
      },
      "filter": {
        "exists": {
          "field": "pin"
        }
      }
    }
  },
  "sort": [
    {
      "_geo_distance": {
        "pin": {
          "lat": 52.51515,
          "lon": 13.38019
        },
        "order": "asc",
        "unit": "km",
        "distance_type": "arc"
      }
    }
  ]
}

and make sure that 'slug' is not analyzed.

Upvotes: 2

Related Questions