Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Search - Search string having spaces in it

I am looking for ElasticSearch query which will provide exact match on string having spaces in it.

for example - I want to search for a word like 'XYZ Company Solutions'. I tried querystring query but it gives me all the records irrespective of search result. Also i read on the post and found that we have to add some mappings for the field. I tried 'Not_Analyzed' analyzer on the field but still it does not worked.

If anyone have complete example or steps then can you please share with me?

Thanks in advance.

Thanks, Sameer

like image 900
Sameer Deshmukh Avatar asked Apr 28 '15 15:04

Sameer Deshmukh


1 Answers

Since you didn't post your code it's hard to tell what's wrong, but "index": "not_analyzed" in your mapping is the right way to handle this.

Here is a simple working example. First I create a mapping that uses "index": "not_analyzed":

PUT /test_index
{
    "mappings": {
        "doc": {
            "properties": {
                "name":{
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
}

Then add a couple of documents for testing

POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"name":"XYZ Company Solutions"}
{"index":{"_id":2}}
{"name":"Another Company"}

Now I can get the document I want with a simple term query:

POST /test_index/doc/_search
{
    "query": {
        "term": {
           "name": {
              "value": "XYZ Company Solutions"
           }
        }
    }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 1,
            "_source": {
               "name": "XYZ Company Solutions"
            }
         }
      ]
   }
}

A term filter or even match query would also work in this case.

Here is the code I used to test it:

http://sense.qbox.io/gist/90fcc7f7a88d58f098c50d5aaf0315fdf06e9e9a

like image 105
Sloan Ahrens Avatar answered Sep 21 '22 10:09

Sloan Ahrens