# Keyword analyzer not working as expected

**URL:** <https://discuss.elastic.co/t/keyword-analyzer-not-working-as-expected/32000>\
**Category:** Elasticsearch\
**Created:** [October 12, 2015, 10:54am UTC](https://discuss.elastic.co/t/keyword-analyzer-not-working-as-expected/32000 "2015-10-12T10:54:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shoebalig](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@shoebalig](https://discuss.elastic.co/u/shoebalig)\
**Post date:** [October 12, 2015, 10:54am UTC](https://discuss.elastic.co/t/keyword-analyzer-not-working-as-expected/32000/1 "2015-10-12T10:54:05Z")

</div>

Hi There,

## I have index mapping given below.

## { "es\_test": { "mappings": { "es\_test\_type": { "\_all": { "enabled": true }, "properties": { "Field1": { "type": "string", "store": true, "analyzer": "keyword" }, "Field2": { "type": "string", "store": true, "analyzer": "standard" }, "Field3": { "type": "string", "store": true, "analyzer": "standard" }, "RecordID": { "type": "string", "store": true, "analyzer": "standard" }, "Status": { "type": "string", "index": "not\_analyzed", "analyzer": "standard" }, "Status.Code": { "type": "string", "index": "not\_analyzed", "analyzer": "standard" }, "Status.Description": { "type": "string", "index": "not\_analyzed", "analyzer": "standard" } } } } } }

## Input data Field1,Field2,Field3 GAURAV GUPTA,SAURABH,MANI GAURAV KUMAR GUPTA,SAURABH SHARMA,MANI RANA GAURAV,SAURABH,MANI gaurav gupta,saurabh sharma,mani

## Query:- POST /es\_test/es\_test\_type/\_search { "query": { "simple\_query\_string": { "query": "GAURAV GUPTA", "fields": ["Field2", "Field3", "Field1"], "analyzer": "keyword", "default\_operator": "or" } } }

## Out put

{  
"took": 11,  
"timed\_out": false,  
"\_shards": {  
"total": 1,  
"successful": 1,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": 0.25604635,  
"hits": [  
{  
"\_index": "es\_test",  
"\_type": "es\_test\_type",  
"\_id": "3",  
"\_score": 0.25604635,  
"\_source": {  
"RecordID": "3",  
"Status": null,  
"Status.Code": null,  
"Status.Description": null,  
"Field1": "GAURAV",  
"Field2": "SAURABH",  
"Field3": "MANI"  
}  
}  
]  
}  
}

* * *

Out is not proper It should return first document  
GAURAV GUPTA,SAURABH,MANI

however it is returning 3rd document  
GAURAV,SAURABH,MANI

Please suggest if i am doing something wrong??

---

<div class="post-metadata">

**Author:** ![davidbkemp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidbkemp/32/594_2.png) [@davidbkemp](https://discuss.elastic.co/u/davidbkemp)\
**Post date:** [October 12, 2015, 9:07pm UTC](https://discuss.elastic.co/t/keyword-analyzer-not-working-as-expected/32000/2 "2015-10-12T21:07:10Z")

</div>

Despite declaring your query analyser to be keyword, your simple query string query still gets parsed into query terms. So your query was equivalent to ("GAURAV" OR "GUPTA")

To get the behaviour that you are expecting, you need to enclose your query term in escaped double quotes. I.e.

"query": ""GAURAV GUPTA""

---

<div class="post-metadata">

**Author:** ![shoebalig](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@shoebalig](https://discuss.elastic.co/u/shoebalig)\
**Post date:** [October 15, 2015, 11:16am UTC](https://discuss.elastic.co/t/keyword-analyzer-not-working-as-expected/32000/3 "2015-10-15T11:16:56Z")

</div>

Thats really help full but for below given query i have doubt again  
POST /es\_standalone\_keyword/es\_standalone\_keyword\_type/\_search  
{  
"query" : {  
"simple\_query\_string" : {  
"query" : ""GAURAV"",  
"fields" : ["Field2", "Field3", "Field1"],  
"analyzer" : "keyword",  
"default\_operator" : "and"  
}  
},  
"fields" : ["Field1", "Field2", "Field3"]  
}  
OUtput is:-  
{  
"took": 3,  
"timed\_out": false,  
"\_shards": {  
"total": 1,  
"successful": 1,  
"failed": 0  
},  
"hits": {  
"total": 2,  
"max\_score": 4.8796144,  
"hits": [  
{  
"\_index": "es\_standalone\_keyword",  
"\_type": "es\_standalone\_keyword\_type",  
"\_id": "1",  
"\_score": 4.8796144,  
"fields": {  
"Field1": [  
"GAURAV"  
],  
"Field2": [  
"GAURAV"  
],  
"Field3": [  
"GAURAV"  
]  
}  
},  
{  
"\_index": "es\_standalone\_keyword",  
"\_type": "es\_standalone\_keyword\_type",  
"\_id": "11",  
"\_score": 1.322617,  
"fields": {  
"Field1": [  
"GAURAV"  
],  
"Field2": [  
"SAURABH"  
],  
"Field3": [  
"MANI"  
]  
}  
}  
]  
}  
}  
My understanding says it should only returns first record not second record.  
Any input much appreciated in advance.

---

<div class="post-metadata">

**Author:** ![davidbkemp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/davidbkemp/32/594_2.png) [@davidbkemp](https://discuss.elastic.co/u/davidbkemp)\
**Post date:** [October 15, 2015, 8:20pm UTC](https://discuss.elastic.co/t/keyword-analyzer-not-working-as-expected/32000/4 "2015-10-15T20:20:00Z")

</div>

Looks to me like your index has a document with GAURAV in the first, second, and third fields. That's in addition to the documents you state are part of your "input".

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [October 16, 2015, 8:28am UTC](https://discuss.elastic.co/t/keyword-analyzer-not-working-as-expected/32000/5 "2015-10-16T08:28:42Z")

</div>

I assume your question relates to the `default_operator` you are using. Note that the operator only applies to the way the query terms are combined, the fields are always combined with an OR (or in boolean should-clauses in Lucene speak).

See the difference for these two queries, the output shows the Lucene query that gets executed:

```auto
GET /test/_validate/query?explain
{
  "query": {
    "simple_query_string": {
      "query": "t1",
      "fields": ["field1","field2","field3"],
      "default_operator": "AND"
    }
  }
}

=> "explanation": "field1:t1 field3:t1 field2:t1"  

```

so here `t1` can appear in any of the three fields, whereas

```auto
GET /test/_validate/query?explain
{
  "query": {
    "simple_query_string": {
      "query": "t1 t2",
      "fields": ["field1","field2","field3"],
      "default_operator": "AND"
    }
  }
}
=> "explanation": "+(field1:t1 field3:t1 field2:t1) +(field1:t2 field3:t2 field2:t2)"

```

Here `t1` and `t2` both need to appear (because of the AND operator) but it doesn't matter in which field.

---

<div class="post-metadata">

**Author:** ![shoebalig](https://avatars.discourse-cdn.com/v4/letter/s/c57346/32.png) [@shoebalig](https://discuss.elastic.co/u/shoebalig)\
**Post date:** [October 20, 2015, 9:42am UTC](https://discuss.elastic.co/t/keyword-analyzer-not-working-as-expected/32000/6 "2015-10-20T09:42:53Z")

</div>

Thanks , Thats really helpful.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:43pm UTC](https://discuss.elastic.co/t/keyword-analyzer-not-working-as-expected/32000/7 "2017-07-05T23:43:53Z")

</div>


