Konrad
Konrad

Reputation: 640

Elasticsearch: constructing bool query with filters

I have following document structure:

Movie:
{
    id: int,
    title: string,
    language: string,
    genre: string,
    description: string,
    cast: array[string],
    directors: array[string],
    (...)
}

Now, in the web interface, the user can choose (checkbox) the language, genre, directors etc and type some query to the search box.

Let's say I want to search within all thrillers (genre), that are in French or English (language), directed by James Cameron or George Lucas (directors) and I'm typing to the search box "abc" that I would like to find within title or description.

What I want as a result: - only movies only in French or English - only movies directed by James Cameron or George Lucas - only thrillers - movies that corresponds to "abc"

I'm not sure how to do the OR in the filters, but I have started from something like:

curl -X -XGET 'localhost:9200/movies/_search?pretty' -H 'Content-Type: application/json' -d'
{
   "query" : {
      "constant_score" : { 
         "filter" : {
            "bool" : {
              "should" : [
                 { "term" : {"language" : "french"}}, 
                 { "term" : {"language" : "english"}},
                 { "term" : {"directors" : "James Cameron"}},
                 { "term" : {"directors" : "George Lucas"}}
              ],
              "filter" : [
                 { "term" : {"genre" : "thriller"}}
              ]
           }
         }
      }
   }
}
'

Could you please give me some hints?

Upvotes: 0

Views: 136

Answers (1)

Eugene
Eugene

Reputation: 3957

As I understand, you need a query like this: (language is A or B) and (directors is C or D) and (genre is E) and (title or description is F). In this case you need the following query:

{
    "query": {
        "bool": {
            "filter": [
                {
                    "terms": {
                        "language": [
                            "french",
                            "english"
                        ]
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "match": {
                                    "directors": "James Cameron"
                                }
                            },
                            {
                                "match": {
                                    "directors": "George Lucas"
                                }
                            }
                        ]
                    }
                },
                {
                    "term": {
                        "genre": "thriller"
                    }
                },
                {
                    "multi_match": {
                        "query": "abc",
                        "fields": [
                            "title",
                            "description"
                        ]
                    }
                }
            ]
        }
    }
}

filter query will work as AND condition but will get you the same score for all matched documents. If you need to vary score depending on subqueries match, you'd better to use must instead of filter. terms query will match if specified field contains at least one term. multi_match query will match if at least one field contains specified query

Upvotes: 1

Related Questions