solr 相似查询-MoreLikeThis

  参考文档:https://lucene.apache.org/solr/guide/6_6/morelikethis.htmlMoreLikeThisMoreLikeThisHandler

  

  在solr中有两种方式实现MoreLikeThis:
  1、SearchHandler中的MoreLikeThisComponent,MoreLikeThis以组件的身份出现,适于简单应用。
  2、MoreLikeThisHandler,MoreLikeThis作为一个单独的Handler来处理,可以应用过滤等较复杂操作

  采用相似查询的field的存储方式最好采用TermVectors方式,如果field没有采用TermVectors方式,MoreLikeThis将会从store存储中生成terms。

  

  <field name="text" ... termVectors ="true"/>

  

  参数说明:

  • mlt.fl      :设置相似查询字段,最好采用TermVectors存储。
  • mlt.mintf   :最小分词频率,源文档中小于该频率的分词将被忽略掉。tf:分词后的词在该文档中的频率
  • mlt.mindf   :最小文档频率,该词所在文档的个数小于这个值时将不用于相似判断。df:该词所在文档的个数。
  • mlt.minwl :词的最小长度,当词的长度小于该值时不用于相似判断。
  • mlt.maxwl  :词的最大长度,当词的长度大于该值时不用于相似判断。
  • mlt.maxqt  :构造相似查询的terms的最大数量。
  • mlt         : true ,开启相似查询。
  • mlt.count   :为每一个相似查询结果返回指定数量的相似文档。
  • mlt.boost :相似查询是否开启加权功能。true/false
  • mlt.qf     :相似查询field字段加权设置。 

  简单的例子:

  http://localhost:8983/solr/data/select/?q=text:Martin&mlt=true&mlt.fl=text&mlt.mindf=1&mlt.mintf=1&mlt.fl=text&fl=id,text&mlt.count=3

  http://localhost:8983/solr/test/select/?q=*:*&mlt=true&mlt.mindf=1&mlt.mintf=1&mlt.fl=text&fl=id,text&mlt.count=3

  http://localhost:8983/solr/data/select/?q=id:20160229001cn.pdf&mlt=true&mlt.mindf=1&mlt.mintf=1&mlt.fl=text&fl=id,text&mlt.count=3

  java代码package main.java;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;


import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient.Builder;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.util.SimpleOrderedMap;

public class morelike {
    private static SolrClient solr;

    public morelike() {
    }

    public static void Init(String urlString) {
        solr = (new Builder(urlString)).build();
    }

    public static void main(String[] args) throws SolrServerException, IOException {
        String urlString = "http://localhost:8983/solr/mycore";
//        String path = "D:/work/Solr/ImportData";
        Init(urlString);
        getMorelikeTextById(""160920 陈悦悦 从付费墙到会员制,国外媒体内容变现新方式?传统媒体内容变现新趋势?.docx"");
        System.exit(0);
    }

    private static void getMorelikeTextById(String id) throws SolrServerException, IOException {
        SolrQuery params = new SolrQuery();
        params.setQuery("id:" + id);
        params.setParam("mlt", true);
        params.setParam("mlt.fl", new String[]{"text"});
        params.setFields(new String[]{"id,text"});
        params.setParam("mlt.count", new String[]{"1"});
        params.setParam("mlt.mintf", new String[]{"10"});
        params.setParam("mlt.mindf", new String[]{"5"});
        QueryResponse queryResponse = solr.query(params);
        SimpleOrderedMap<SolrDocumentList> mltResults = (SimpleOrderedMap<SolrDocumentList>)queryResponse.getMoreLikeThis();
//        SimpleOrderedMap<SolrDocumentList> mltResults = (SimpleOrderedMap<SolrDocumentList>)queryResponse.getResponse().get("moreLikeThis");
        for (int i = 0; i < mltResults.size(); i++) {
            SolrDocumentList items = mltResults.getVal(i);
            for (SolrDocument doc : items) {
                String id1 = doc.getFieldValue("id").toString();
                System.out.println(id1);
            }
        }


    }
}
}
原文地址:https://www.cnblogs.com/shaosks/p/8004968.html