我在订购ElasticSearch
中的部分匹配之前订购了确切的字符串匹配假设我有三个文件:
如果我搜索 micha ,它只会返回第三个文档。但是,如果我搜索 micha * ,我会按照上面列出的顺序获取所有这三份文件。
我的问题是,如何在其他结果之前显示第三个文件,即完全匹配?
:analysis => {
:filter => {
"my_edge_ngram_filter" => {
"type" => "edgeNGram",
"min_gram" => 1,
"max_gram" => 128,
"side" => "front",
},
"my_phone_filter" => {
"type" => "pattern_replace",
"pattern" => "\\D",
"replacement" => ""
}
},
:analyzer => {
"default_index" => {
"tokenizer" => "uax_url_email",
"filter" => [
"standard",
"lowercase",
"stop",
"my_edge_ngram_filter"
],
"type" => "custom"
},
"my_phone_analyzer" => {
"tokenizer" => "keyword",
"filter" => [
"my_phone_filter",
],
"type" => "custom"
},
"default_search" => {
"type" => "standard"
}
}
}
}
答案 0 :(得分:3)
您可以使用multi_field + boosting:
"name" : { "type" : "multi_field", "fields" : { "name" : { "type" : "string", "index" : "analyzed", "index_analyzer" : "standard", "search_analyzer" : "standard", "boost" : 10.0 }, "partial": { "type" : "string", "index" : "analyzed", "index_analyzer" : "your_ngram_analyzer", "search_analyzer" : "standard", "boost" : 1.0 } } }
然后在两个字段上搜索,例如:
"query": {
"query_string": {
"query": "micha",
"fields": [
"name.partial",
"name"
],
"default_operator": "and"
}
}
PS:您还可以动态指定查询中的字段提升,例如:
"query": {
"query_string": {
"query": "micha",
"fields": [
"name.partial^1",
"name^10"
],
"default_operator": "and"
}
}