1

I created a documents in elasticsearch in the following format

curl -XPUT "http://localhost:9200/my_base.main_candidate/" -d'
{
    "specific_location": {
        "location_name": "Mumbai",
        "location_tags": [
            "Mumbai"
        ],
        "tags": [
            "Mumbai"
        ]
    }
}'

My requirement is to search for location_tags containing one of the given options like ["Mumbai", "Pune"]. How do I do this?

I tried:

curl -XGET "http://localhost:9200/my_base.main_candidate/_search" -d '
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "terms": {
          "specific_location.location_tags" : ["Mumbai"]
        }
      }
    }
  }
}'

which didn't work.

I got this output :

{
  "took": 72,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}

1 Answer 1

3

There are a several ways to solve this. Perhaps the most immediate one is to search for mumbai instead of Mumbai.

If I create the index with no mapping,

curl -XDELETE "http://localhost:9200/my_base.main_candidate/"
curl -XPUT "http://localhost:9200/my_base.main_candidate/"

then add a doc:

curl -XPUT "http://localhost:9200/my_base.main_candidate/doc/1" -d'
{
   "specific_location": {
      "location_name": "Mumbai",
      "location_tags": [
         "Mumbai"
      ],
      "tags": [
         "Mumbai"
      ]
   }
}'

then run your query with the lower-case term

curl -XPOST "http://localhost:9200/my_base.main_candidate/_search" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "terms": {
               "specific_location.location_tags": [
                  "mumbai"
               ]
            }
         }
      }
   }
}'

I get back the expected doc:

{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "my_base.main_candidate",
            "_type": "doc",
            "_id": "1",
            "_score": 1,
            "_source": {
               "specific_location": {
                  "location_name": "Mumbai",
                  "location_tags": [
                     "Mumbai"
                  ],
                  "tags": [
                     "Mumbai"
                  ]
               }
            }
         }
      ]
   }
}

This is because, since no explicit mapping was used, Elasticsearch uses defaults, which means the location_tags field will be analyzed with the standard analyzer, which will convert terms to lower-case. So the term Mumbai does not exist, but mumbai does.

If you want to be able to use upper-case terms in your query, you will need to set up an explicit mapping that tells Elasticsearch not to analyze the location_tags field. Maybe something like this:

curl -XDELETE "http://localhost:9200/my_base.main_candidate/"

curl -XPUT "http://localhost:9200/my_base.main_candidate/" -d'
{
   "mappings": {
      "doc": {
         "properties": {
            "specific_location": {
               "properties": {
                  "location_tags": {
                     "type": "string",
                     "index": "not_analyzed"
                  },
                  "tags": {
                     "type": "string",
                     "index": "not_analyzed"
                  }
               }
            }
         }
      }
   }
}'

curl -XPUT "http://localhost:9200/my_base.main_candidate/doc/1" -d'
{
   "specific_location": {
      "location_name": "Mumbai",
      "location_tags": [
         "Mumbai"
      ],
      "tags": [
         "Mumbai"
      ]
   }
}'

curl -XPOST "http://localhost:9200/my_base.main_candidate/_search" -d'
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "terms": {
               "specific_location.location_tags": [
                  "Mumbai"
               ]
            }
         }
      }
   }
}'

Here is all the above code in a handy place:

http://sense.qbox.io/gist/74844f4d779f7c2b94a9ab65fd76eb0ffe294cbb

[EDIT: by the way, I used Elasticsearch 1.3.4 when testing the above code]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.