'ElasticSearch - Search fails for 2 string fields

I am running into search error - need some help. I have article index with id, title, artist, genre fields. When I run this query I get zero results-

POST /d3acampaign/article/_search

    {
       "query": {
            "filtered": {
                "query": {
                    "match": {"genre": "metal"}    
                },
                "filter": {
                    "term": {"artist": "Somanath"}         
                }

            }
        }
    } 

But if I change the query to something like - POST /d3acampaign/article/_search

    {
       "query": {
            "filtered": {
                "query": {
                    "match": {"genre": "metal"}    
                },
                "filter": {
                    "term": {"id": "7"}         
                }

            }
        }
    } 

i get following result -

    {
       "took": 1,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 1,
          "max_score": 1.4054651,
          "hits": [
             {
                "_index": "d3acampaign",
                "_type": "article",
                "_id": "7",
                "_score": 1.4054651,
                "_source": {
                   "id": "7",
                   "title": "The Last Airbender",
                   "artist": "Somanath",
                   "genre": "metal"
                }
             }
          ]
       }
    } 

Clarification - I am noticing search failing in case if I try against string e.g. artist, title



Solution 1:[1]

The reason of you get empty hits is when you query by using term query, it will match the exact term in index includes uppercase.

and the default analyzer will create index with lowercase.

There are many ways to analyze text: the default standard analyzer drops most punctuation, breaks up text into individual words, and lower cases them. https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

The solution:

   {
       "query": {
            "filtered": {
                "query": {
                    "match": {"genre": "metal"}    
                },
                "filter": {
                    "term": {"artist": "somanath"}  //to lower case      
                }

            }
        }
    } 

The other solution is change your index mapping to not_analyzed for your index type.

The full example:

#!/bin/bash

curl -XDELETE "localhost:9200/testindex"
curl -XPUT "localhost:9200/testindex/?pretty" -d '
{
    "mappings":{
        "test":{
            "properties":{
                "name":{
                    "index":"not_analyzed",
                        "type":"string"
                }
            }
        }
    }
}'

curl -XGET "localhost:9200/testindex/_mapping?pretty"

curl -XPOST "localhost:9200/testindex/test/1" -d '{
    "name": "Jack"
}'

sleep 1
echo -e
echo -e
echo -e
echo -e "Filtered Query Search in not_analyzed index:"
echo -e

curl -XGET "localhost:9200/testindex/test/_search?pretty" -d '{
    "query": {
        "filtered": {
                "filter": {
                    "term": {"name": "Jack"}  
                }

        }
    }
}'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1