Home > Blockchain >  How to build a "Terms" query in ElasticSearch console?
How to build a "Terms" query in ElasticSearch console?

Time:05-27

I'm fairly new to ElasticSearch and NoQSL in general. My problem seems simple and yet I can't seem to make it work.

The Index I'm fetching from looks something like this:

"hits" : [
      {
        "_index" : "movie",
        "_type" : "_doc",
        "_id" : "255239",
        "_score" : 1.0,
        "_source" : {
          "id" : 255239,
          "title" : "Passport to Pimlico",
          "original" : "Passport to Pimlico",
          "lead" : "",
          "text" : "blablabla",
          "classification" : 0,
          "year" : "1949",
          "color" : "b&w",
          "duration" : "85",
          "url" : "some url",
          "slug" : "something something",
          "openingDate" : "1900-01-01T00:00:00",
          "countries" : [
            "GB"
          ],
          "genres" : [
            "Comedy",
            "Action"
          ],
          "producers" : [ ],

I'm trying for search for any records that match any of the items inside the "genres" property on any of the Terms in this simple array '["Thriller", "Comedy"]'.

This is my query in the console:

GET movie/_search
{
  "query": {
    "terms": {
      "genres": ["Thriller", "Comedy"]
    }
  }
}

This query seems valid, but is returning me 0 results. What am I doing wrong?

Any help is appreciated, thanks.

CodePudding user response:

Try this instead, i.e. query on genres.keyword instead of genres:

GET movie/_search
{
  "query": {
    "terms": {
      "genres.keyword": ["Thriller", "Comedy"]
    }
  }
}

CodePudding user response:

This should work:

GET movie/_search
{
  "query": {
    "bool:{
       "should": {
         "terms":
          {
             "genres": ["Thriller", "Comedy"]
          }
       }
    }
  }
}

Should occurence type of a boolQuery will match a document with any of terms provided (because minimum_should_match is 1 by default).

If you want to match document that has both genres use minimum_should_match: 2

More in docs: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

  • Related