elasticsearch 相似文档推荐

参考:https://www.elastic.co/guide/en/elasticsearch/reference/7.5/query-dsl-mlt-query.html

对应的代码为:

{
    "query": {
        "more_like_this" : {
            "fields" : ["name.first", "name.last"],
            "like" : [
            {
                "_index" : "marvel",
                "doc" : {
                    "name": {
                        "first": "Ben",
                        "last": "Grimm"
                    },
                    "_doc": "You got no idea what I'd... what I'd give to be invisible."
                  }
            },
            {
                "_index" : "marvel",
                "_id" : "2"
            },
            "你好,我是第一次使用这个"
            ],
            "min_term_freq" : 1,
            "min_doc_freq" : 1,
            "max_query_terms" : 12,
            "minimum_should_match":1,
        }
    }
}

说明:从以上可以看出,总共可以支持三种以及他们之间组合使用

第一种:是根据文档_id来查找和其相似文档

第二种:自定义文档,来查找和其相似文档

第三种:字符串,来查找这个字符串和文档中"name.first", "name.last"这两个属性最相似的文档

参数说明, More like this query | Elasticsearch Guide [7.10] | Elastic

此外,其还可以和时间衰减相结合使用:

{
    "query": {
        "function_score": {
            "query": {
                "more_like_this": {
                    "fields": [
                        "content"
                    ],
                    "like": [
                        {
                            "_index": "marvel",
                            "doc": {
                                "name": {
                                    "first": "Ben",
                                    "last": "Grimm"
                                },
                                "_doc": "You got no idea what I'd... what I'd give to be invisible."
                            }
                        },
                        {
                            "_index": "marvel",
                            "_id": "2"
                        },
                        "你好,我是第一次使用这个"
                    ],
                    "min_term_freq": 0,
                    "min_doc_freq": 0,
                    "max_query_terms": 120
                }
            },
            "functions": [
                {
                    "gauss": {
                        "publish_time": {
                            "origin": "now",
                            "offset": "2d",
                            "scale": "7d",
                            "decay": 0.5
                        }
                    }
                }
            ]
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会发paper的学渣

您的鼓励和将是我前进的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值