solr 自聚类实现
参考官网:https://lucene.apache.org/solr/guide/6_6/result-clustering.html
最近用到solr自聚类的,先简单介绍如下:
1、配置文件
主要配置文件必须配置如下内容:
<lib dir="${solr.install.dir:../../..}/contrib/clustering/lib/" regex=".*\.jar" />
<lib dir="${solr.install.dir:../../..}/dist/" regex="solr-clustering-\d.*\.jar" />
<searchComponent name="clustering" enable="${solr.clustering.enabled:true}" class="solr.clustering.ClusteringComponent">
<!-- Lingo clustering algorithm -->
<lst name="engine">
<str name="name">lingo</str>
<!--<bool name="optional">true</bool>-->
<str name="carrot.algorithm">org.carrot2.clustering.lingo.LingoClusteringAlgorithm</str>
<str name="carrot.resourcesDir">clustering/carrot2</str>
</lst>
<!-- An example definition for the STC clustering algorithm. -->
<lst name="engine">
<str name="name">stc</str>
<bool name="optional">true</bool>
<str name="carrot.algorithm">org.carrot2.clustering.stc.STCClusteringAlgorithm</str>
<str name="carrot.resourcesDir">clustering/carrot2</str>
</lst>
<lst name="engine">
<str name="name">kmeans</str>
<!--<bool name="optional">true</bool>-->
<str name="carrot.algorithm">org.carrot2.clustering.kmeans.BisectingKMeansClusteringAlgorithm</str>
<str name="carrot.resourcesDir">clustering/carrot2</str>
</lst>
</searchComponent>
下面的配置文件根据自己的实际情况进行修改:
<requestHandler name="/clustering"
startup="lazy"
class="solr.SearchHandler">
<lst name="defaults">
<bool name="clustering">true</bool>
<bool name="clustering.results">true</bool> <!-- Field name with the logical "title" of a each document (optional) -->
<str name="carrot.title">keyword</str>
<!-- Logical field to physical field mapping. -->
<str name="carrot.url">id</str>
<!-- Field name with the logical "content" of a each document (optional) -->
<str name="carrot.snippet">summary</str>
<!-- Apply highlighter to the title/ content and use this for clustering. -->
<bool name="carrot.produceSummary">true</bool>
<!-- the maximum number of labels per cluster -->
<!--<int name="carrot.numDescriptions">5</int>-->
<!-- produce sub clusters -->
<bool name="carrot.outputSubClusters">false</bool> <!-- Configure any other request handler parameters. We will cluster the
top 100 search results so bump up the 'rows' parameter. -->
<!--<str name="defType">edismax</str>
<str name="qf">
text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0 manu^1.1 cat^1.4
</str>
<str name="q.alt">*:*</str>-->
<str name="defType">edismax</str>
<!--<str name="qf">
summary^0.5 category^1.2 id^10.0
</str>-->
<str name="qf">keyword^0.5 title^1.2 id^10.0</str>
<str name="rows">100</str>
<str name="fl">*,score</str>
</lst> <!-- Append clustering at the end of the list of search components. -->
<arr name="last-components">
<str>clustering</str>
</arr>
</requestHandler>
managed-schema配置文件包含以下内:
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<analyzer type="query" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<field name="id" type="string" multiValued="false" indexed="true" required="true" stored="true"/>
<field name="text" type="text_ik" multiValued="false" indexed="true" stored="true" termVectors ="true"/>
<field name="title" type="text_ik" multiValued="false" indexed="true" stored="true" />
<field name="snippet" type="text_ik" multiValued="false" indexed="true" stored="true" />
<field name="keyword" type="text_ik" multiValued="false" indexed="true" stored="true" />
<field name="category" type="text_ik" multiValued="false" indexed="true" stored="true" />
<field name="summary" type="text_ik" multiValued="false" indexed="true" stored="true"/>
<field name="path" type="string" multiValued="false" indexed="true" stored="true"/>
注意:text_ik对应的分词组件,要引用对应的jar包,具体参见:http://www.cnblogs.com/shaosks/p/8204615.html
2、测试索引的文件
启动solr服务,在浏览器输入:http://localhost:8983/solr/mycore/clustering?q=*:*&rows=10
结果如下:

3、java查询代码
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;
import org.apache.solr.client.solrj.response.Cluster;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.ClusteringResponse;
import org.apache.solr.common.SolrDocument; import java.io.IOException;
import java.util.List; /**
* @Author:sks
* @Description:
* @Date:Created in 9:41 2018/1/18
* @Modified by:
**/
public class AutoCluster { private static SolrClient solr; /**
* @Author:sks
* @Description:初始化solr客户端
* @Date:
*/
public static void Init(String urlString){ solr = new HttpSolrClient.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);
getAutoClusterInfo();
System.exit(0);
} /**
* @Author:sks
* @Description:获取聚类数据
* @Date:
*/
private static void getAutoClusterInfo() throws SolrServerException,IOException {
//使用这个对象做查询
SolrQuery params = new SolrQuery();
//查询所有数据
params.set("qt", "/clustering");
params.setQuery("*:*");
params.setStart(0);
params.setRows(30); QueryResponse queryResponse = solr.query(params);
ClusteringResponse clr = queryResponse.getClusteringResponse();
List<Cluster> list = clr.getClusters();
//拿到聚类数据集合,返回查询结果 String txt = "";
for(Cluster c :list){
//类别标签
List<String> lblist = c.getLabels();
for(String lb:lblist){
System.out.println(lb);
}
//聚类文档ID
List<String> doclist = c.getDocs();
for(String doc:doclist){
System.out.println(" " + doc);
}
} } }
查询结果如下:

solr 自聚类实现的更多相关文章
- Solr调研总结
http://wiki.apache.org/solr/ Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境 ...
- solr教程,值得刚接触搜索开发人员一看
http://blog.csdn.net/awj3584/article/details/16963525 Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍sol ...
- Solr总结
http://www.cnblogs.com/guozk/p/3498831.html Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注 ...
- 【转载】solr教程,值得刚接触搜索开发人员一看
转载:http://blog.csdn.net/awj3584/article/details/16963525 Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍 ...
- Solr调研总结(转)
Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境搭建及调试.两个核心配置文件介绍.中文分词器配置.维护索引 ...
- Solr调研总结(很详细很全面)
Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境搭建及调试;两个核心配置文件介绍;维护索引;查询索引,和在 ...
- solr入门教程-较详细
Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境搭建及调试;两个核心配置文件介绍;维护索引;查询索引,和在 ...
- Lucene/Solr搜索引擎开发笔记 - 第1章 Solr安装与部署(Jetty篇)
一.为何开博客写<Lucene/Solr搜索引擎开发笔记> 本人毕业于2011年,2011-2014的三年时间里,在深圳前50强企业工作,从事工业控制领域的机器视觉方向,主要使用语言为C/ ...
- 1.7.1 solr Searching概述
1. Overview of Searching in Solr 在用户运行一个solr搜索时,搜索查询会被request handler处理.一个request handler就是一个请求处理插件, ...
随机推荐
- OpenCL学习笔记(一):摩尔定律,异构计算与OpenCL初印象
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入. 关于摩尔定律: 摩尔定律19 ...
- .NET 二进制序列化器,SOAP序列化器,XML序列化器
这里就不说JSON序列化了,只介绍三种:二进制序列化器,SOAP序列化器,XML序列化器 直接上代码: /// <summary> /// 二进制序列化器. /// 最节省流量,压缩程度最 ...
- 走进 UnitTest for Xamarin.Forms
之前讲了 Xamarin.Forms 的 UITest 走进 UITest for Xamarin.Forms 走进 Xamarin Test Recorder for Xamarin.Forms 但 ...
- 【笔试题】Spring笔试题
spring笔试题 1.Spring支持的事务管理类型 Spring支持两种类型的事务管理: 编程式事务管理:这意味你通过编程的方式管理事务,给你带来极大的灵活性,但是难维护. 声明式事务管理:这意味 ...
- HDU 4607.Park Visit-树的直径(BFS版)+结论公式(乱推公式)-备忘(加油!)
Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- 转:Fuzzing Apache httpd server with American Fuzzy Lop + persistent mode
Fuzzing Apache httpd server with American Fuzzy Lop + persistent mode 小结:AFL主要以文件作为输入进行fuzz,本文介绍如何对网 ...
- Hibernate + Oracle 创建自增序列ID
1.创建自增序列 2.对ID创建触发器 3.Userinfo.hbm.xml使得<generator class="increment"> 序列: MAXVALUE I ...
- 将Electron桌面应用转化为Win10的UWP应用
如果有小伙伴不知道如何打包Electron桌面应用的,请戳我. 微软提供了一款快捷工具:electron-windows-store,用于将electron桌面应用打包成Windows10系统上.ap ...
- POJ 2425 A Chess Game 博弈论 sg函数
http://poj.org/problem?id=2425 典型的sg函数,建图搜sg函数预处理之后直接求每次游戏的异或和.仍然是因为看不懂题目卡了好久. 这道题大概有两个坑, 1.是搜索的时候vi ...
- 【贪心】【线性基】bzoj2460 [BeiJing2011]元素
题意:让你求一些数在XOR下的带权极大无关组. 带权极大无关组可以用贪心,将这些数按权值从大到小排序之后,依次检验其与之前的数是否全都线性无关.可以用线性基来搞. 可以用拟阵严格证明,不过也可以脑补一 ...