Reputation: 39
For the below query, I am searching the text "christ". the result is not showing the correct order. I have a record for "Christine" but it is not showing as a first document. Can you help me to fix this order issue? I am using Elasticsearch 8.5.3. but the same is correctly working in Elasticsearch 2.x version.
Mapping and Settings are below,
{
"template": {
"settings": {
"index": {
"max_ngram_diff": "3",
"routing": {
"allocation": {
"include": {
"_tier_preference": "data_content"
}
}
},
"analysis": {
"analyzer": {
"special_char_edge_1_gram_analyzer": {
"filter": [
"lowercase"
],
"char_filter": [
"string_special_character_char_filter"
],
"type": "custom",
"tokenizer": "edge_1_gram_tokenizer"
},
"tokenizer": {
"edge_1_gram_tokenizer": {
"token_chars": [
"letter",
"digit"
],
"min_gram": "1",
"type": "edge_ngram",
"max_gram": "100"
}
},
"number_of_shards": "1",
"number_of_replicas": "1"
}
},
"mappings": {
"name": {
"properties": {
"first": {
"type": "text",
"fields": {
"special_char_edge_1_gram_analyzer": {
"type": "text",
"analyzer": "special_char_edge_1_gram_analyzer"
}
}
}
}
}
},
"aliases": {}
}
}
}
}
}
}
}
Query is below,
GET as.contact-154/_search
{
"query": {
"bool": {
"should": {
"multi_match": {
"type": "best_fields",
"query": "christ",
"fields": [ "name.first^2", "name.first.special_char_edge_1_gram_analyzer^5" ]
}
}
}
},
"_source": ["id","name.first","name.last"],
"highlight": {
"pre_tags": [
"<span class='text-highlight'>"
],
"post_tags": [
"</span>"
],
"fields": {
"name.first": {},
"id": {}
}
},
"from": 0,
"size": 100
}
Result,
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 125,
"relation": "eq"
},
"max_score": 62.842735,
"hits": [
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "127848",
"_score": 62.842735,
"_source": {
"name": {
"last": "Poeschl",
"first": "Chris"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Chris</span>"
]
}
},
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "128911",
"_score": 62.842735,
"_source": {
"name": {
"last": "Rhode",
"first": "Chris"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Chris</span>"
]
}
},
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "128925",
"_score": 62.842735,
"_source": {
"name": {
"last": "Hanson",
"first": "Chris"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Chris</span>"
]
}
},
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "129102",
"_score": 62.842735,
"_source": {
"name": {
"last": "Cooper",
"first": "Chris"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Chris</span>"
]
}
},
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "129116",
"_score": 62.842735,
"_source": {
"name": {
"last": "Sipos",
"first": "Chris"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Chris</span>"
]
}
},
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "129119",
"_score": 62.842735,
"_source": {
"name": {
"last": "Sipos",
"first": "Chris"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Chris</span>"
]
}
},
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "129147",
"_score": 62.842735,
"_source": {
"name": {
"last": "Dudlery",
"first": "Chris"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Chris</span>"
]
}
},
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "1264420",
"_score": 62.842735,
"_source": {
"name": {
"last": "Jaquess",
"first": "Chris"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Chris</span>"
]
}
},
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "1264421",
"_score": 62.842735,
"_source": {
"name": {
"last": "Jaquess",
"first": "Chris"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Chris</span>"
]
}
},
{
"_index": "es.contact-154.23-05-12-7-04-12",
"_id": "127930",
"_score": 60.612587,
"_source": {
"name": {
"last": "Vanderzanden",
"first": "Christine"
}
},
"highlight": {
"name.first.special_char_edge_1_gram_analyzer": [
"<span class='text-highlight'>Christ</span>ine"
]
}
}
]
}
}
Latest Result,
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 125,
"relation": "eq"
},
"max_score": 104.737885,
"hits": [
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "127848",
"_score": 104.737885,
"_source": {
"name": {
"last": "Poeschl",
"first": "Chris"
}
}
},
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "128911",
"_score": 104.737885,
"_source": {
"name": {
"last": "Rhode",
"first": "Chris"
}
}
},
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "128925",
"_score": 104.737885,
"_source": {
"name": {
"last": "Hanson",
"first": "Chris"
}
}
},
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "129102",
"_score": 104.737885,
"_source": {
"name": {
"last": "Cooper",
"first": "Chris"
}
}
},
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "129116",
"_score": 104.737885,
"_source": {
"name": {
"last": "Sipos",
"first": "Chris"
}
}
},
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "129119",
"_score": 104.737885,
"_source": {
"name": {
"last": "Sipos",
"first": "Chris"
}
}
},
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "129147",
"_score": 104.737885,
"_source": {
"name": {
"last": "Dudlery",
"first": "Chris"
}
}
},
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "1264420",
"_score": 104.737885,
"_source": {
"name": {
"last": "Jaquess",
"first": "Chris"
}
}
},
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "1264421",
"_score": 104.737885,
"_source": {
"name": {
"last": "Jaquess",
"first": "Chris"
}
}
},
{
"_index": "es.contact-154.23-05-12-14-34-46",
"_id": "127930",
"_score": 102.020966,
"_source": {
"name": {
"last": "Vanderzanden",
"first": "Christine"
}
}
}
]
}
}
Upvotes: 0
Views: 128
Reputation: 217464
The problem is that the edge-ngram analyzer is also kicking in at search time, and hence, the searched string christ
is resolved to chris
by the analyzer at search time and matches the first records. That's not desired.
You should modified your mapping like this and specify a standard
search_analyzer
so that the searched text doesn't get edge-ngramed:
{
"mappings": {
"name": {
"properties": {
"first": {
"type": "text",
"fields": {
"special_char_edge_1_gram_analyzer": {
"type": "text",
"analyzer": "special_char_edge_1_gram_analyzer",
"search_analyzer": "standard"
}
}
}
}
}
}
}
Upvotes: 0