# Different Result when Query has Double Slash Escape (\\\\)

**URL:** https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434
**Category:** Elasticsearch
**Created:** [August 30, 2019, 2:31am UTC](https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434 "2019-08-30T02:31:07Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![salvadorcanillas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/salvadorcanillas/32/53338_2.png) [@salvadorcanillas](https://discuss.elastic.co/u/salvadorcanillas)
#### Post date: [August 30, 2019, 2:31am UTC](https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434/1 "2019-08-30T02:31:07Z")

</div>

Hello,

I am using Elasticsearch in Magento.

Elasticsearch version 5.6  
Magento Enterprise 2.2.9

I have set the sku type to keyword but getting a different result when query has escape character

"sku": {  
"type": "keyword"  
}

1. Magento Query with \:

> {  
> "match": {  
> "sku": {  
> "query": "414\-123",  
> "operator": "OR",  
> "prefix\_length": 0,  
> "max\_expansions": 50,  
> "fuzzy\_transpositions": true,  
> "lenient": false,  
> "zero\_terms\_query": "NONE",  
> "boost": 2  
> }  
> }  
> }

Result:  
{  
"hits": {  
"total": 0,  
"max\_score": null,  
"hits":   
}  
}

1. Removing //  
{  
"match": {  
"sku": {  
"query": "414-123",  
"operator": "OR",  
"prefix\_length": 0,  
"max\_expansions": 50,  
"fuzzy\_transpositions": true,  
"lenient": false,  
"zero\_terms\_query": "NONE",  
"boost": 2  
}  
}  
}

"hits": {  
"total": 1,  
"max\_score": 0.8630463,  
"hits": [  
{  
"\_index": "magento2\_product\_1\_v40",  
"\_type": "document",  
"\_id": "1",  
"\_score": 0.8630463  
}  
]  
}

The analyzer also shows same result so I was wondering why Elasticsearch is not returning the result when the - is escaped with \.

{  
"analyzer" : "standard",  
"text" : "414\-123"  
}

{  
"analyzer" : "standard",  
"text" : "414-123"  
}

Both return the same result  
{  
"tokens": [  
{  
"token": "414",  
"start\_offset": 0,  
"end\_offset": 3,  
"type": "",  
"position": 0  
},  
{  
"token": "123",  
"start\_offset": 5,  
"end\_offset": 8,  
"type": "",  
"position": 1  
}  
]  
}

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [August 30, 2019, 8:28am UTC](https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434/2 "2019-08-30T08:28:02Z")

</div>

hey,

the `sku` field is of type `keyword` - which means it is stored as-is in the inverted index, where as your analyze API is using an analyzer to tokenize the field, the keyword type is not doing anything before storing the term. This means you need to search for the exact same term in a search as it is stored in the inverted index.

hope this makes sense!

Side nit: please take the time to properly format your JSON, as it is otherwise super hard to read. This forum supports markdown for formatting.

---

<div class="post-metadata">

### Author: ![salvadorcanillas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/salvadorcanillas/32/53338_2.png) [@salvadorcanillas](https://discuss.elastic.co/u/salvadorcanillas)
#### Post date: [August 30, 2019, 11:26am UTC](https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434/3 "2019-08-30T11:26:11Z")

</div>

Hello

Thank you for your response. Analyzers makes sense now.

However, I still would like to know why Elasticsearch is not giving me the result when my query text has an escaped hyphen versus when I remove the escape characters.

"query": "414\-123" - No result  
"query": "414-123" - One result as expected

PS Sorry for the bad json.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [August 30, 2019, 12:27pm UTC](https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434/4 "2019-08-30T12:27:18Z")

</div>

because the escaped version is different than the none escaped one - which is the only one in the index. the escaping is forwarded as part of the search.

---

<div class="post-metadata">

### Author: ![salvadorcanillas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/salvadorcanillas/32/53338_2.png) [@salvadorcanillas](https://discuss.elastic.co/u/salvadorcanillas)
#### Post date: [September 2, 2019, 8:15am UTC](https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434/5 "2019-09-02T08:15:27Z")

</div>

How about this one. Below API is returning the document:

GET `http://localhost:9200/magento2_product_1_v45/_search?q=sku:414\-123`

While below API does not:

POST `http://localhost:9200/magento2_product_1_v45/_search?pretty`

```
{
"query": {
	"should": [
		{
			"match": {
				"sku": {
					"query": "414\\-123",
					"operator": "OR"
				}
			}
		}
	]
}

```

}

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [September 2, 2019, 8:19am UTC](https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434/6 "2019-09-02T08:19:49Z")

</div>

those are two different types of queries - one is a `match` query, the other one is a `query_string` query.

---

<div class="post-metadata">

### Author: ![salvadorcanillas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/salvadorcanillas/32/53338_2.png) [@salvadorcanillas](https://discuss.elastic.co/u/salvadorcanillas)
#### Post date: [September 2, 2019, 8:40am UTC](https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434/7 "2019-09-02T08:40:19Z")

</div>

But either way, ES should be able to un-escape the query text, read it as "414-123" and retrieve the document, same as what happens when using the first API call above.

---

<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: [September 30, 2019, 8:40am UTC](https://discuss.elastic.co/t/different-result-when-query-has-double-slash-escape/197434/8 "2019-09-30T08:40:22Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
