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

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
}
]
}

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.

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.

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.

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"
				}
			}
		}
	]
}

}

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

1 Like

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.

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