参考: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
}
}
}
]
}
}
}