Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch No query registered for [exists]]

After running the below query, i am getting the exception as No query registered for [exists]. Please help me.

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": {
            "match": {
              "_all": {
                "query": "cardio new york"
              }
            }
          }
        }
      },
      "functions": [
        {
          "gauss": {
            "geo_location": {
              "origin": {
                "lat": 40.7127,
                "lon": -74.0059
              },
              "scale": "100km",
              "offset": "0km",
              "decay": 0.9
            }
          }
        },
        {
          "gauss": {
            "startdate": {
              "origin": "now",
              "scale": "30d",
              "offset": "30d"
            }
          },
          "weight": 0.5
        }
      ],
      "filter": {
        "query": {
          "bool": {
            "must": {
              "match": {
                "_all": {
                  "query": "cardio new york"
                }
              }
            },
            "should": {
              "exists": {
                "fields": [
                  "venue",
                  "geo_location"
                ]
              }
            }
          }
        }
      }
    }
  }
}

I am trying to filter the search results after the function_score with combining bool match query.

like image 243
sandeep Avatar asked Sep 17 '25 19:09

sandeep


2 Answers

exists is not a query, it's filter you cannot use it in a bool query, instead I would use bool filter and wrap only match into query filter like this:

 "filter": {
    "bool": {
      "must": [{
        "query": {
          "match": {
            "_all": {
              "query": "cardio new york"
            }
          }
        }
      }, {
        "exists": {
          "fields": [
            "venue",
            "geo_location"
          ]
        }
      }]
    }
  }
like image 137
imotov Avatar answered Sep 23 '25 05:09

imotov


This depends on your version, before version 2.0 you would use the Exists filter, as demonstrated in imotov's answer.

With 2.0 and after exists filter has been replaced by exists query documentation for current version

thus the newer exists query would look like the following:

{
 "index": "foobars",
 "type": "foo",
 "body": {
  "query": {
   "bool": {
    "must":
        {"exists" : { "field" : "bar" }}
   }
  },
  "from": 0,
  "size": 20
 }
}
like image 31
user254694 Avatar answered Sep 23 '25 04:09

user254694