POST /user_info/_bulk
{"index" : {"_type":"user","_id":"1"}}
{"name":"高X"}
{"index" : {"_type":"user","_id":"2"}}
{"name":"高XX"}
{"index" : {"_type":"user","_id":"3"}}
{"name":"X高X"}
{"index" : {"_type":"user","_id":"4"}}
{"name":"X高X"}
{"index" : {"_type":"user","_id":"5"}}
{"name":"XX高"}
{"index" : {"_type":"user","_id":"6"}}
{"name":"高XXX"}GET user_info/_search?size=20
{
"query": {
"query_string" : {
"query" : "(name:(*高*))"
}}}POST _scripts/user_info_score
{
"script": {
"lang":"painless",
"source": """
double position_score = 0;
double similarity_score = 0;
int pos = doc["name"].value.indexOf(params.keyword);
if (pos != -1)
{
position_score = 10 - pos;
if (position_score < 0) {position_score = 0;}}double similarity = Math.abs(1.0*doc["name"].value.length()- params.keyword.length());
similarity_score = 10 - similarity;
if (similarity_score < 0) similarity_score = 0;
return position_score * 0.6 + similarity_score * 0.4;
"""
}
}GET user_info/_search?size=20
{
"query":{
"function_score": {
"query": { "query_string": {
"query": "(name(*高*))"
}},
"script_score": {
"script": {"id": "user_info_score",
"params": {
"keyword":"高"
}}
},
"boost_mode": "sum"
}
}
}