首页 > 解决方案 > 基于整数值的提升分数 - Elasticsearch

问题描述

我对 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
            }
        ]
    }
}

现在在这个特定的搜索中,重点是tags,我想知道如何提升_score并将其乘以votesunder中的整数tags

如果这是不可能的(或很难实现),我只想知道如何_score通过votes(而不是下tags)来提升

示例,将 0.1 添加到中的_score每个整数votes

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

{
    "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.dislikestags.votesnested并向tags

标签: elasticsearchboostfilter

解决方案


您正在查看function score queryhttps ://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
            }
        }
    }
}

或者script score因为你的嵌套tags字段(不确定field value score嵌套结构是否可以正常工作)。


推荐阅读