Toggle navigation
首页
技术
骑行
羽毛球
资讯
联络我
登录
elasticsearch实现相似搜索思路
2020-03-10
Elasticsearch
> 本文介绍如何在elk search实现相似搜索的思路 ## 相似搜索的应用场景 * 回答问题:如果已有一系列常见问题,则可通过文本相似度来查找与用户所输入问题相似的问题。 * 文章搜索:从一系列文章中,返回与用户查询内容相关的文章。 * 图片搜索:对于由包含说明的图片组成的数据集,从中查找哪些图片的说明与用户描述相似。 ## 如何实现? 目前elasticsearch有两种方案: 1. dense-vector 2. More Like This Query [dense-vector](https://www.elastic.co/guide/en/elasticsearch/reference/master/dense-vector.html)在elk 7.3版本开始支持,需要x-pack模块,且需要在索引的时候就处理并生成dense-vector,相对比较麻烦 [More Like This Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html)是一个查询语法,在任意版本均可用,无需x-pack,且无需改动已经index的内容,语法如下 ```json GET /_search { "query": { "more_like_this" : { "fields" : ["title", "description"], "like" : "Once upon a time", "min_term_freq" : 1, "max_query_terms" : 12 } } } ``` * fields:要执行查询的栏位 * like:要查询相似的文本 * min_term_freq:最小词频率,低于该频率的词将被忽略 * max_query_terms:提取词的最大个数,其余的词将被忽略 另外,它还可以以某几篇具体的文章为标准来查询相似内容 ```json GET /_search { "query": { "more_like_this" : { "fields" : ["title", "description"], "like" : [ { "_index" : "imdb", "_id" : "1" }, { "_index" : "imdb", "_id" : "2" }, "and potentially some more text here as well" ], "min_term_freq" : 1, "max_query_terms" : 12 } } } ``` ### 原理 MLT查询从输入文档中提取文本,通常使用该字段中的同一分析器对其进行分析,然后选择tf-idf最高的前K个词构成这些词的析取查询。 ## 参考 * [text-similarity-search](https://www.elastic.co/blog/text-similarity-search-with-vectors-in-elasticsearch) * [dense-vector](https://www.elastic.co/guide/en/elasticsearch/reference/master/dense-vector.html) * [More Like This Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html)
×
本文为博主原创,如需转载,请注明出处:
http://www.supperxin.com
返回博客列表