参考官网: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 自聚类实现的更多相关文章

  1. Solr调研总结

    http://wiki.apache.org/solr/ Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境 ...

  2. solr教程,值得刚接触搜索开发人员一看

    http://blog.csdn.net/awj3584/article/details/16963525 Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍sol ...

  3. Solr总结

    http://www.cnblogs.com/guozk/p/3498831.html Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注 ...

  4. 【转载】solr教程,值得刚接触搜索开发人员一看

    转载:http://blog.csdn.net/awj3584/article/details/16963525 Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍 ...

  5. Solr调研总结(转)

    Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境搭建及调试.两个核心配置文件介绍.中文分词器配置.维护索引 ...

  6. Solr调研总结(很详细很全面)

    Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境搭建及调试;两个核心配置文件介绍;维护索引;查询索引,和在 ...

  7. solr入门教程-较详细

    Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境搭建及调试;两个核心配置文件介绍;维护索引;查询索引,和在 ...

  8. Lucene/Solr搜索引擎开发笔记 - 第1章 Solr安装与部署(Jetty篇)

    一.为何开博客写<Lucene/Solr搜索引擎开发笔记> 本人毕业于2011年,2011-2014的三年时间里,在深圳前50强企业工作,从事工业控制领域的机器视觉方向,主要使用语言为C/ ...

  9. 1.7.1 solr Searching概述

    1. Overview of Searching in Solr 在用户运行一个solr搜索时,搜索查询会被request handler处理.一个request handler就是一个请求处理插件, ...

随机推荐

  1. WebDriver自动化测试工具(2)---基本操作

    一.设置打开的浏览器大小/位置 driver.Manage().Window.Maximize(); //最大化 driver.Manage().Window.Position = , ); //设置 ...

  2. Jenkins发布PHP代码

    实验环境 10.0.0.12 Jenkins服务器 10.0.0.13 远程服务器 一个远程的公开的git仓库(php代码在这个仓库里) 一.检查插件是否安装并安装插件 在通过Jenkins发布php ...

  3. 在centos 6.9安装wordpress,浏览器不能访问问题

    在centos 6.9安装wordpress浏览器访问先出现403错误,然后提示access denied nginx错误打印FastCGI sent in stderr: "Unable ...

  4. Druid数据迁移小计

    Druid数据迁移小计 Druid 官方网站上讲了相关的 Dump Segment 和 Insert Segment 相关的功能,但是经过测试这些功能都不好用,报 Guice 的依赖错误,懒得找具体原 ...

  5. YII2源码阅读:autoload_real.php 22~23行

    spl_autoload_register(array('ComposerAutoloaderInit32b8eb537f8e12e57c5e7bade69d01f0', 'loadClassLoad ...

  6. mysql查看表结构,字段等命令

    mysql查看表结构命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名;

  7. Container With Most Water(LintCode)

    Container With Most Water Given n non-negative integers a1, a2, ..., an, where each represents a poi ...

  8. Poj2482 Stars in Your Window(扫描线)

    题面 Poj 题解 下面内容引用自"李煜东 <算法竞赛进阶指南>"(对原文略有缩减,侵删): 因为矩形的大小固定,所以矩形可以由它的任意一个顶点唯一确定.我们可以考虑把 ...

  9. phonegap安卓视频播放解决方案

    使用phonegap开发的时候,视频播放很多人一开始选择用html5的Video标签作为备选方案,实际上这种方案在真实环境下常常是无法满足需求的,因为目前HTML5的Video标签只支持MP4,OGG ...

  10. css总结——position

    CSS(Cascading Style Sheet),中文翻译为层叠样式表,是用于控制网页样式并允许将样式信息与网页内容分离的一种标记性语言.在css控制页面中,主要有四种样式:行内样式(style ...