solr的moreLikeThis实现“相似数据”功能

在我们使用网页搜索时,会注意到每一个结果都包含一个 “相似页面” 链接,单击该链接,就会发布另一个搜索请求,查找出与起初结果类似的文档。Solr 使用 MoreLikeThisComponent(MLT)和 MoreLikeThisHandler 实现了一样的功能。如上所述,MLT 是与标准 SolrRequestHandler 集成在一起的;MoreLikeThisHandler 与 MLT 结合在一起,并添加了一些其他选项,但它要求发布一个单一的请求。我将着重讲述 MLT,因为使用它的可能性更大一些。幸运的是,不需要任何设置就可以查询它,所以您现在就可以开始查询。

  MLT 要求字段被储存或使用检索词向量,检索词向量以一种以文档为中心的方式储存信息。MLT 通过文档的内容来计算文档中关键词语,然后使用原始查询词语和这些新词语创建一个新的查询。提交新查询就会返回其他查询结果。所有这些都可以用检索词向量来完成:只需将 termVectors="true" 添加到 schema.xml 中的 <field> 声明。

1、solrconfig.xml配置

要想使用匹配相似首先在 solrconfig.xml 中配置 MoreLikeThisHandler

<requestHandler    name="/mlt" class="solr.MoreLikeThisHandler">  
</requestHandler>

2、managed-scheme配置

对需要作为相似度匹配字段的field添加 termVectors="true" 

 1 ...
 2   <field name="ds_topic_tags" type="strings" multiValued="true" termVectors="true" />
 3   <field name="ds_class_tags" type="strings" multiValued="true" />
 4   <field name="ds_total_size" type="plong"  />
 5   <field name="hit_count" type="plong"  />
 6   <field name="down_count" type="plong"  />
 7   <field name="id" type="string" indexed="true" required="true" stored="true"/>
 8   <field name="meta_code" type="string"  />
 9   <field name="organization_id" type="string"  />
10   <field name="organization" type="text_cjk" termVectors="true" />
11   <field name="title_cn" type="text_cjk" multiValued="false" termVectors="true"  />
12 ...

3、python代码

1     si = get_si()
2     siq = si.query(id=id).mlt('title_cn, ds_category, ds_topic_tags, ds_subject_tags', count=10, mintf=1, mindf=1).field_limit(
3         ["id", "title_cn"])
4     results = siq.execute()
5     rm_ids = [r['id'] for r in results.more_like_these[id].docs]
6     r_m = []
7     for rid in rm_ids:
8         r_meta = Metadata.query.get(rid)
9         r_m.append(r_meta)

 

参考文献:

1、https://scorched.readthedocs.io/en/latest/query.html

2、https://doc.lucidworks.com/fusion-server/5.0/solr-reference-guide/7.2.1/the-term-vector-component.html

3、https://lucene.apache.org/solr/guide/7_6/morelikethis.html

4、solr相似匹配

5、solr找到与查询结果相似的文档

6、solr 相似查询-MoreLikeThis

7、https://lucene.apache.org/solr/guide/7_6/morelikethis.html

 

posted @ 2019-12-11 10:52  万里沙来手一挥  阅读(445)  评论(0编辑  收藏  举报