Prefer search query match at the beginning of the search result rather than at the end using elasticsearch
I have a request that I cannot receive to return the most relevant response.
If I search for "herp" the result is
- fu herp
- herp derp
- bar herp
- herp aa
desirably, I need this order
- herp aa
- herp derp
- bar herp
- fu herp
So the question I have, I'm guessing: how do I get the terms at the beginning of a sequence of words, get a higher score than the one closer to the end?
The analyzer used looks like this:
"analyzer":[
"autocomplete":[
"type":"custom",
"tokenizer":"standard",
"filter":[
"standard",
"lowercase",
"stop",
"edgeNGramAlpha"
]
],
"filter":[
"edgeNGramAlpha":[
"type":"edgeNGram",
"min_gram":1,
"max_gram":20
]
]
]
and the display looks like this (field loot, but they all look the same)
"name": [
"type": "multi_field",
"fields" : [
"untouched": [
"type": "string",
"index": "not_analyzed"
],
"name": [
"type": "string"
],
"autocomplete": [
"analyzer":"${language}_autocomplete",
"type":"string",
]
]
]
and the request looks like this:
{
"from": 0,
"size": 10,
"query": {
"filtered": {
"query": {
"multi_match": {
"query": "herp",
"fields": [
"name^8",
"name.autocomplete^4",
"historic_name.autocomplete"
],
"type": "cross_fields",
"operator": "AND",
"analyzer": "standard"
}
}
}
}
}
+3
source to share
1 answer
One way to achieve this is to provide some extra boost for terms at the start of the field using span
Example:
{
"from": 0,
"size": 10,
"query": {
"bool": {
"disable_coord": true,
"must": [
{
"multi_match": {
"query": "herp",
"fields": [
"name^8",
"name.autocomplete^4",
"historic_name.autocomplete"
],
"analyzer": "standard"
}
}
],
"should": [
{
"span_first": {
"match": {
"span_term": {
"name": "herp"
}
},
"end": 1,
"boost": 1
}
},
{
"span_first": {
"match": {
"span_term": {
"historic_name": "herp"
}
},
"end": 1,
"boost": 1
}
}
],
"minimum_number_should_match": 0
}
}
}
+1
source to share