Elastic Search - Search string having spaces in it
Asked Answered
G

3

15

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

Guideline answered 28/4, 2015 at 15:6 Comment(0)
J
5

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

Jointly answered 28/4, 2015 at 15:41 Comment(1)
I'll just add that since the version 5.0, the type "string" is no longer supported and two new are introduces. "text" is default set to analyzed and "keyword" to not_analyzed. So if you are working with the newer versions, it should be enough to change the type to "keyword".Artur
B
0
PUT /index_1
{   
  "settings": {
    "analysis": {
      "normalizer": {
        "lowercase_normalizer": { "type": "custom", "char_filter": [],           "filter": ["lowercase"]}
      }
    }
  },
  "mappings": {
     "doc_type": {
            "properties": {
                "name":{"type": "keyword", "normalizer": "lowercase_normalizer"}
            }
     }
  }
}

I used the above the setting and mapping to define the index. Then pushed a few values into the database

POST index_1/doc_type/1
{
  "name" : "a b c"
}

POST index_1/doc_type/1
{
  "name" : "a c"
}

POST index_1/doc_type/1
{
  "name" : "a b"
}

Now if we search for individual letters in the name field of above index we get nothing in return

GET index_1/doc_type/_search
{
  "query" : 
    {"match": {"name": "A"}}
}

GET index_1/doc_type/_search
{
  "query" : 
    {"match": {"name": "b"}}
}

but if we search for

GET index_1/doc_type/_search
{
  "query" : 
    {"match": {"name": "A B C"}}
}

we will get the match

This helps searching complete keyword while avoiding case-senstivity

Badgett answered 4/9, 2017 at 13:21 Comment(0)
C
0

If by "ElasticSearch" you mean the ElasticSearch GUI which takes "KQL" filters, rather than a code / API interface, you can put double quotes around the string, as per this Elastic discussion

Covalence answered 8/9, 2023 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.