smashedtomato
smashedtomato

Reputation: 29

Elasticsearch Search by Index Type and Field

I want to search in a single index type and filter it further by matching a single field. Let say I have books, movies and toys types. Now, I want to search a movie in Thriller genre, what I have to do? I have this code that doesn't work and produces some error:

{
            "from" : 0, 
            "size" : 10,
            "query": {
                "filtered" : {
                    "filter" : {
                        "type" : { 
                            "value" : "movies"
                        },
                        "term" : { 
                            "genre" : "Thriller"
                        }
                    }
                }
            },
            "sort": [
                {
                    "date_entered" : {
                        "order": "desc"
                    }
                }
            ]
        }

The error is this:

{"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;

Upvotes: 0

Views: 123

Answers (1)

Sloan Ahrens
Sloan Ahrens

Reputation: 8718

Your syntax is wrong; you can't specify two filters that way. Try using a bool must filter.

I set up a simple index, and then added three docs of different types but with the same "genre":

PUT /test_index

POST /test_index/_bulk
{"index":{"_type":"books"}}
{"genre":"Thriller"}
{"index":{"_type":"movies"}}
{"genre":"Thriller"}
{"index":{"_type":"toys"}}
{"genre":"Thriller"}

Then ran this query:

POST /test_index/_search
{
   "from": 0,
   "size": 10,
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "bool": {
               "must": [
                  {
                     "type": {
                        "value": "movies"
                     }
                  },
                  {
                     "term": {
                        "genre": "thriller"
                     }
                  }
               ]
            }
         }
      }
   }
}

and got back the right doc:

{
   "took": 10,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "movies",
            "_id": "uReb0g9KSLKS18sTATdr3A",
            "_score": 1,
            "_source": {
               "genre": "Thriller"
            }
         }
      ]
   }
}

Notice that I had to use lower-case "thriller" in the term filter. Since I didn't specify an analyzer, the standard analyzer will be used, which will transform the text "Thriller" into the term "thriller", so if I'm going to use a term filter I'll need to use the lower-case version. If I use a match query instead, I can use "Thriller" as the query text.

Here is the code I used to test:

http://sense.qbox.io/gist/2f5dc5f15f0ecb50fb9772bbf4d52849031be41d

Upvotes: 1

Related Questions