我有一组名称存储在Elasticsearch DB中,如高级软件工程师助理软件工程师软件工程师集团软件工程师软件工程师
问题陈述:如果查询"软件工程师",结果的顺序不会优先考虑"软件"是第一个单词,而是显示"助理软件工程师"在最上面
我们如何配置搜索以提高搜索短语是开头部分的条目的排名?所以这个顺序应该是软件工程师助理软件工程师集团软件工程师高级软件工程师
Mapping used currently:
{
"properties": {
"name":{"type": "search_as_you_type"}
}
}
Query used for autocomplete search:
s = s.query(Q("match_phrase_prefix",name={"query":input_string})).sort('_score')
res = s.execute()
一种可能是使用补全建议器。它提供了"权重"。这样你就可以给你的建议排序了。下面我举了一个例子,让你可以操纵建议的顺序。我希望这对你有帮助。
PUT idx_test
{
"mappings": {
"properties": {
"role": {
"type": "text"
},
"suggest": {
"type": "completion"
}
}
}
}
POST idx_test/_doc
{
"role":"Senior Software Engineer Assistant",
"suggest": [
{
"input": "Senior Software Engineer Assistant",
"weight": 50
},
{
"input": "Software Engineer Assistant",
"weight": 50
}
]
}
POST idx_test/_doc
{
"role":"Software Engineer Staff",
"suggest": [
{
"input": "Software Engineer Staff",
"weight": 40
}
]
}
POST idx_test/_doc
{
"role":"Software Engineer Group",
"suggest": [
{
"input": "Software Engineer Group",
"weight": 20
}
]
}
POST idx_test/_doc
{
"role":"Software Engineer",
"suggest": [
{
"input": "Software Engineer",
"weight": 10
}
]
}
GET idx_test/_search
{
"suggest": {
"role-suggest": {
"prefix": "software engg",
"completion": {
"field": "suggest",
"fuzzy": {
"fuzziness": 2
}
}
}
}
}
结果:
{
"suggest": {
"role-suggest": [
{
"options": [
{
"_source": {
"role": "Senior Software Engineer Assistant",
"suggest": [
{
"input": "Senior Software Engineer Assistant",
"weight": 50
},
{
"input": "Software Engineer Assistant",
"weight": 50
}
]
}
},
{
"_source": {
"role": "Software Engineer Staff",
"suggest": [
{
"input": "Software Engineer Staff",
"weight": 40
}
]
}
},
{
"_source": {
"role": "Software Engineer Group",
"suggest": [
{
"input": "Software Engineer Group",
"weight": 20
}
]
}
},
{
"_source": {
"role": "Software Engineer",
"suggest": [
{
"input": "Software Engineer",
"weight": 10
}
]
}
}
]
}
]
}
}