我在排序上下文中使用脚本来实现自定义排序。以下是我的脚本排序节点,允许自定义排序。
{
"_script": {
"type": "string",
"script": {
"source": """
def sortingValue = "";
for(query in params.queries) {
if (doc['firstName.keyword'].size() > 0 && doc['firstName.keyword'].value.startsWith(query)) {
sortingValue = '1' +":"+ doc['fullName.keyword'].value;
break;
} else if (doc['lastName.keyword'].size() > 0 && doc['lastName.keyword'].value.startsWith(query)) {
sortingValue = '2' +":"+ doc['fullName.keyword'].value;
break;
} else if (doc['companyName.keyword'].size() > 0 && doc['companyName.keyword'].value.contains(query)) {
sortingValue = '3' + ":" + doc['companyName.keyword'].value;
break;
} else {
sortingValue = '4';
}
}
return sortingValue;
""",
"params": {
"queries": [
"mo",
"buga"
]
}
},
"order": "asc"
}
}
使用脚本,我按以下方式对结果进行分类
搜索文字:莫布加
第一类: 对名字以 Mo 或 Buga 开头的联系人进行排序
第二类: 对姓氏以 Mo 或 Buga 开头的联系人进行排序
第三类: 将公司名称以 Mo 或 Buga 开头的联系人排序为第三类
第四类: 所有其他匹配结果都属于第四类
我如何确保没有不需要的重新编译。
您可以通过
edge_ngram
过滤器和 multi_match
查询对没有脚本的点击进行排序
映射
PUT /first_last_company_name
{
"mappings": {
"properties": {
"first_name": {
"type": "text",
"analyzer": "whitespace_edge_ngram_filter_analyzer"
},
"last_name": {
"type": "text",
"analyzer": "whitespace_edge_ngram_filter_analyzer"
},
"company_name": {
"type": "text",
"analyzer": "whitespace_edge_ngram_filter_analyzer"
}
}
},
"settings": {
"analysis": {
"analyzer": {
"whitespace_edge_ngram_filter_analyzer": {
"tokenizer": "whitespace",
"filter": [
"lowercase",
"edge_ngram_filter"
]
}
},
"filter": {
"edge_ngram_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 11
}
}
}
}
}
具有不同名字、姓氏和公司名称组合的文件
PUT /first_last_company_name/_bulk
{"create":{"_id":1}}
{"first_name":"mogun","last_name":"nomo","company_name":"nobuga"}
{"create":{"_id":2}}
{"first_name":"bugatti","last_name":"nomo","company_name":"nobuga"}
{"create":{"_id":3}}
{"first_name":"nomon","last_name":"motto","company_name":"nobuga"}
{"create":{"_id":4}}
{"first_name":"nomon","last_name":"bugar","company_name":"nobuga"}
{"create":{"_id":5}}
{"first_name":"nomon","last_name":"nobugam","company_name":"more"}
{"create":{"_id":6}}
{"first_name":"nomon","last_name":"nobugam","company_name":"bugastein"}
{"create":{"_id":7}}
{"first_name":"nomon","last_name":"bugav","company_name":"monoq"}
{"create":{"_id":8}}
{"first_name":"nomon","last_name":"mower","company_name":"bugano"}
{"create":{"_id":9}}
{"first_name":"nomon","last_name":"nobuga","company_name":"nomobuga"}
multi_match
带响应过滤器的查询
GET /first_last_company_name/_search?filter_path=hits.hits._source, hits.hits._score,hits.hits._id
{
"query": {
"multi_match": {
"query": "mo buga",
"tie_breaker": 0,
"fields": [
"first_name^100",
"last_name^10",
"company_name"
]
}
}
}
回应
{
"hits" : {
"hits" : [
{
"_id" : "2",
"_score" : 373.28677,
"_source" : {
"first_name" : "bugatti",
"last_name" : "nomo",
"company_name" : "nobuga"
}
},
{
"_id" : "1",
"_score" : 337.62857,
"_source" : {
"first_name" : "mogun",
"last_name" : "nomo",
"company_name" : "nobuga"
}
},
{
"_id" : "4",
"_score" : 27.299335,
"_source" : {
"first_name" : "nomon",
"last_name" : "bugar",
"company_name" : "nobuga"
}
},
{
"_id" : "7",
"_score" : 27.299335,
"_source" : {
"first_name" : "nomon",
"last_name" : "bugav",
"company_name" : "monoq"
}
},
{
"_id" : "3",
"_score" : 24.707626,
"_source" : {
"first_name" : "nomon",
"last_name" : "motto",
"company_name" : "nobuga"
}
},
{
"_id" : "8",
"_score" : 24.707626,
"_source" : {
"first_name" : "nomon",
"last_name" : "mower",
"company_name" : "bugano"
}
},
{
"_id" : "6",
"_score" : 2.7447402,
"_source" : {
"first_name" : "nomon",
"last_name" : "nobugam",
"company_name" : "bugastein"
}
},
{
"_id" : "5",
"_score" : 2.4951272,
"_source" : {
"first_name" : "nomon",
"last_name" : "nobugam",
"company_name" : "more"
}
}
]
}
}
_score
在匹配名字、姓氏或公司名称方面存在显着差异