问题:基于整数值的提升分数 - 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并将其乘以tags下的votes中的整数。

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

例如,对于votes中的每个整数,在_score上加 0.1

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

{
    "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属性

解答

这花了很长时间才弄清楚。我在去那里的路上学到了很多东西。

这是最终结果:

{
    "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"
                }
            }
        }
    }
}

should中的数组帮助很大,很高兴我可以将它与function_score结合使用

Logo

欢迎大家访问Elastic 中国社区。由Elastic 资深布道师,Elastic 认证工程师,认证分析师,认证可观测性工程师运营管理。

更多推荐