Hot*_*f68 5 search elasticsearch
我想根据数组中嵌套对象中字段的权重修改ElasticSearch(v2 +)中的评分.
例如,使用此数据:
PUT index/test/0
{
"name": "red bell pepper",
"words": [
{"text": "pepper", "weight": 20},
{"text": "bell","weight": 10},
{"text": "red","weight": 5}
]
}
PUT index/test/1
{
"name": "hot red pepper",
"words": [
{"text": "pepper", "weight": 15},
{"text": "hot","weight": 11},
{"text": "red","weight": 5}
]
}
Run Code Online (Sandbox Code Playgroud)
我想要一个像{"words.text":"red pepper"}这样的查询,它将"红辣椒"排在"红辣椒"之上.
我正在考虑这个问题的方法是"首先匹配'text'字段,然后根据'weight'字段修改评分".不幸的是,我不知道如何实现这一点,如果它甚至可能,或者我有正确的方法来做这样的事情.
如果提出替代方法,请尝试保留一个广义的想法,其中有大量不同的类似案例(例如:简单地修改"红辣椒"文件得分更高并不是真正合适的替代方案).
示例实现如下所示:
PUT test
PUT test/test/_mapping
{
"properties": {
"name": {
"type": "string"
},
"words": {
"type": "nested",
"properties": {
"text": {
"type": "string"
},
"weight": {
"type": "long"
}
}
}
}
}
PUT test/test/0
{
"name": "red bell pepper",
"words": [
{"text": "pepper", "weight": 20},
{"text": "bell","weight": 10},
{"text": "red","weight": 5}
]
}
PUT test/test/1
{
"name": "hot red pepper",
"words": [
{"text": "pepper", "weight": 15},
{"text": "hot","weight": 11},
{"text": "red","weight": 5}
]
}
post test/_search
{
"query": {
"bool": {
"disable_coord": true,
"must": [
{
"match": {
"name": "red pepper"
}
}
],
"should": [
{
"nested": {
"path": "words",
"query": {
"function_score": {
"functions": [
{
"field_value_factor": {
"field" : "words.weight",
"missing": 0
}
}
],
"query": {
"match": {
"words.text": "red pepper"
}
},
"score_mode": "sum",
"boost_mode": "replace"
}
},
"score_mode": "total"
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
"hits": [
{
"_index": "test",
"_type": "test",
"_id": "0",
"_score": 26.030865,
"_source": {
"name": "red bell pepper",
"words": [
{
"text": "pepper",
"weight": 20
},
{
"text": "bell",
"weight": 10
},
{
"text": "red",
"weight": 5
}
]
}
},
{
"_index": "test",
"_type": "test",
"_id": "1",
"_score": 21.030865,
"_source": {
"name": "hot red pepper",
"words": [
{
"text": "pepper",
"weight": 15
},
{
"text": "hot",
"weight": 11
},
{
"text": "red",
"weight": 5
}
]
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
简而言之,查询将对满足以下must条款的文档进行评分:weights将匹配的嵌套文档与must子句的分数相加.
| 归档时间: |
|
| 查看次数: |
1146 次 |
| 最近记录: |