Search code examples
elasticsearchnosql

How to build a "Terms" query in ElasticSearch console?


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.


Solution

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

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