如何在 Elasticsearch 中查找相似文档

How to find similar documents in Elasticsearch

我的文档是由各种fields组成的。现在给定一个输入文档,我想使用输入文档字段找到 similar documents。我怎样才能实现它?

您没有提到您的字段类型。一般方法是使用捕获所有字段(使用 copy_to) with the more like this 查询。

{
    "query": {
        "more_like_this" : {
            "ids" : ["12345"],
            "fields" : ["field_1", "field_2"],
            "min_term_freq" : 1,
            "max_query_terms" : 12
        }
    }

}

您将获得与 id 12345 类似的文档。这里您只需要指定 id 和字段,如标题、类别、名称等,而不是它们的值。

这是另一个不使用 id 的代码,但您需要指定具有值的字段。示例:获取具有类似标题的类似文档: elasticsearch 很快

{
    "query": {
        "more_like_this" : {
            "fields" : ["title"],
            "like" : "elasticsearch is fast",
            "min_term_freq" : 1,
            "max_query_terms" : 12
        }
    }

}

您可以添加更多字段及其值

{
            "query": {
                "more_like_this" : {
                    "fields" : ["first name", "last name", "address", "etc"],
                    "like" : "your_query",
                    "min_term_freq" : 1,
                    "max_query_terms" : 12
                }
            }

 }  

将所有内容放入 your_query。您可以增加或减少 min_term_freqmax_query_terms