Elastic Search 功能得分查询与查询字符串
我在用Elastic Search做搜索,代码是:
es.search(index="article-index", fields="url", body={
"query": {
"query_string": {
"query": "keywordstr",
"fields": [
"text",
"title",
"tags",
"domain"
]
}
}
})
现在我想在搜索评分中加入一个新的参数 - “recencyboost”(最近性加权)。
有人告诉我可以用function_score来解决这个问题。
res = es.search(index="article-index", fields="url", body={
"query": {
"function_score": {
"functions": {
"DECAY_FUNCTION": {
"recencyboost": {
"origin": "0",
"scale": "20"
}
}
},
"query": {
{
"query_string": {
"query": keywordstr
}
}
},
"score_mode": "multiply"
}
}
})
但是我遇到了一个错误,提示字典 {"query_string": {"query": keywordstr}}
不能被哈希。
1) 我该如何修复这个错误?
2) 我该如何修改衰减函数,让它对最近性加权更高的结果给予更大的权重?
3 个回答
-1
像这样使用它
query: {
function_score: {
query: {
filtered: {
query: {
bool: {
must: [
{
query_string: {
query: shop_search,
fields: [ 'shop_name']
},
boost: 2.0
},
{
query_string: {
query: shop_search,
fields: [ 'shop_name']
},
boost: 3.0
}
]
}
},
filter: {
// { term: { search_city: }}
}
},
exp: {
location: {
origin: { lat: 12.8748964,
lon: 77.6413239
},
scale: "10000m",
offset: "0m",
decay: "0.5"
}
}
// score_mode: "sum"
}
1
你不能把字典当作字典里的键。你在代码的以下部分做了这个:
"query": {
{"query_string": {"query": keywordstr}}
},
下面的代码应该可以正常工作
"query": {
"query_string": {"query": keywordstr}
},
2
你在搜索中似乎多加了一个query
,总共有三个,这样会导致出现一个不必要的顶层结构。你需要去掉这个顶层的query
,用function_score
来替代它,作为顶层的关键字。
res = es.search(index="article-index", fields="url", body={"function_score": {
"query": {
{ "query_string": {"query": keywordstr} }
},
"functions": {
"DECAY_FUNCTION": {
"recencyboost": {
"origin": "0",
"scale": "20"
}
}
},
"score_mode": "multiply"
})
注意:score_mode
的默认值是"multiply"
,而未使用的boost_mode
也是如此,所以你不需要特别去提供它。