Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search query empty list

I am new when it comes to Elasticsearch. I have an endpoint:

http://localhost:8000/v1/scholarship

This returns all scholarships in the database. I can add a filter:

http://localhost:8000/v1/scholarship?institution=Michigan State

This will return all scholarships associated with a specific institution (in this case, Michigan State)

My scholarship model has an institution field that defaults to an empty list if no institution is affiliated:

"institution" : [],

How would I go about filtering all scholarships that have no institutions?

I tried this query but all the scholarships are returned (since there was no match)

http://localhost:8000/v1/scholarship?intitution=[]

Any ideas? I was thinking of creating a new end point, but that seems to defeat the purpose of using filters / Elasticsearch

like image 642
user3007294 Avatar asked Apr 10 '17 15:04

user3007294


1 Answers

You can use an Exists Query to look for empty fields:

GET localhost:8000/v1/scholarship/_search
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "scholarship"
                }
            }
        }
    }
}

This matches the below fields:

{ "scholarship": null }
{ "scholarship": [] } 
{ "scholarship": [null] }
like image 100
Christian Häckh Avatar answered Oct 25 '22 14:10

Christian Häckh