根据整数值提高得分- Elasticsearch

5

我是一名对ElasticSearch不是很有经验的人,希望了解如何根据特定整数值提高搜索结果的优先级。

以下是一个文档示例:

{
    "_index": "links",
    "_type": "db1",
    "_id": "mV32vWcBZsblNn1WqTcN",
    "_score": 8.115617,
    "_source": {
        "url": "example.com",
        "title": "Example website",
        "description": "This is an example website, used for various of examples around the world",
        "likes": 9,
        "popularity": 543,
        "tags": [
            {
                "name": "example",
                "votes": 5
            },
            {
                "name": "test",
                "votes": 2
            },
            {
                "name": "testing",
                "votes": 1
            }
        ]
    }
}

现在这个特定的搜索,重点在于标签,我想知道如何提高_score并将其乘以标签投票中的整数。

如果这不可能(或者很难实现),我只想知道如何通过投票(而不是标签下的投票)来提高_score

例如,为投票中的每个整数添加0.1到_score

这是我目前正在使用的搜索查询(仅用于搜索标签):

{
    "query": {
        "nested": {
            "path": "tags",
            "query": {
                "bool":{
                    "should":{
                        "match":{
                            "tags.name":"example,testing,something else"
                        }
                    }
                }
            }
        }
    }
}

我在网上找不到太多信息,希望有人能帮助我。

如何使用整数值提高 _score


更新

以下是映射的更多信息:

{
    "links": {
        "mappings": {
            "db1": {
                "properties": {
                    "url": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "title": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "description": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "likes": {
                        "type": "long"
                    },
                    "popularity": {
                        "type": "long"
                    },
                    "tags": {
                        "type": "nested",
                        "properties": {
                            "name": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            },
                            "votes": {
                                "type": "long"
                            }
                        }
                    }
                }
            }
        }
    }
}

更新 2

tags.likes/tags.dislikes更改为tags.votes,并在tags中添加了一个nested属性。


你能展示一下你的映射吗?tagsnested 类型吗? - Val
@Val 我更新了问题,并添加了映射。我不知道这是否是嵌套的。 - Typewar
谢谢,问题在于如果你想达到期望的效果,tags应该是嵌套的,否则你无法查询特定嵌套标签以获取它们的赞/踩值来提高分数。 - Val
@Val 我会预料到类似的结果。但是通过嵌套标记,这是否意味着我不能设置自定义/新的“名称”?映射需要覆盖世界上所有标记吗?还是只需要稍微更改一下映射即可? 我正在考虑制作一个db2,并逐步将所有文档迁移到新的映射结构中。 - Typewar
请查看此链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html - Val
@Val 更新了问题并将嵌套属性添加到标签中。 - Typewar
2个回答

4

我花了很长时间才弄清楚这个问题。在这个过程中我学到了很多东西。

以下是最终结果:

{
    "query": {
        "nested": {
            "path": "tags",
            "query": {
                "function_score": {
                    "query": {
                        "bool": {
                            "should": [
                                {
                                    "match": {
                                        "tags.name": "example"
                                    }
                                },
                                {
                                    "match": {
                                        "tags.name": "testing"
                                    }
                                },
                                {
                                    "match": {
                                        "tags.name": "test"
                                    }
                                }
                            ]
                        }
                    },
                    "functions": [
                        {
                            "field_value_factor": {
                                "field": "tags.votes"
                            }
                        }
                    ],
                    "boost_mode": "multiply"
                }
            }
        }
    }
}
< p >在 should 中的数组非常有帮助,我很高兴能将其与 function_score 结合使用。


2
你正在查看“function score query”:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html 以及“field value factor” https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor
来自文档的片段:
GET /_search
{
    "query": {
        "function_score": {
            "field_value_factor": {
                "field": "tags.dislikes",
                "factor": 1.2,
                "modifier": "sqrt",
                "missing": 1
            }
        }
    }
}

或者使用 脚本分数,因为您的嵌套标签字段(不确定字段值分数是否适用于嵌套结构)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接