« 中文分词 mmseg4j 在 lucene 中的使用示例Google App Engine 应用绑定自己的域名 »
solr 实现去掉重复的搜索结果,打SOLR-236_collapsing.patch补丁
发表于:2009年4月30日 | 分类:Solr | 标签: patch, solr | views(9,762)
版权信息: 可以任意转载, 转载时请务必以超链接形式标明文章原文出处, 即下面的声明.
原文出处:http://blog.chenlb.com/2009/04/apply-solr-collapsing-patch-remove-duplicate-result.html
打上SOLR-236_collapsing.patch补丁,实现 solr 搜索结果折叠、除去重复的搜索结果,可以实现类似google搜索结果的“站内的其它相关信息 ”。solr collapsing patch 是用 hash 某个字段来实现折叠重复结果的。下面我演示下应用这个补丁并搜索试用下。
其实 solr 上已经有了这功能的实现:solr 1.3 collapse patch, 请看:https://issues.apache.org/jira/browse/SOLR-236,我这里下载是了新的:https://issues.apache.org/jira/secure/attachment/12403590/SOLR-236_collapsing.patch。
下载好后就需要打上补丁了,先准备一份源码在D:/apache-solr-1.3.0目录下。没有可以去下载:http://archive.apache.org/dist/lucene/solr/1.3.0/apache-solr-1.3.0.zip。把SOLR-236_collapsing.patch文件放在D:/apache-solr-1.3.0目录下, 打补丁有我知道的有两种:用linux工具 patch(windows 下有 cygwin);用 ant 的 patch。
windows cygwin 的 patch:
D:\apache-solr-1.3.0>patch -p0 < SOLR-236_collapsing.patch
patching file src/test/org/apache/solr/search/TestDocSet.java
patching file src/java/org/apache/solr/search/CollapseFilter.java
patching file src/java/org/apache/solr/search/DocSet.java
patching file src/java/org/apache/solr/search/NegatedDocSet.java
patching file src/java/org/apache/solr/search/SolrIndexSearcher.java
patching file src/java/org/apache/solr/common/params/CollapseParams.java
patching file src/java/org/apache/solr/handler/component/CollapseComponent.java
ant patch,把下面的内容保存为 patch-build.xml 放到 D:\apache-solr-1.3.0 目录下:
<?xml version="1.0" encoding="UTF-8"?>
<project name="solr-patch" default="apply-patch" basedir=".">
<target name="apply-patch" description="Apply a patch file. Set -Dpatch.file">
<patch patchfile="${patch.file}" strip="0"/>
</target>
</project>
ant 打补丁:
D:\apache-solr-1.3.0>ant -Dpatch.file=SOLR-236_collapsing.patch -f patch-build.xml
Buildfile: patch-build.xml
apply-patch:
[patch] patching file src/test/org/apache/solr/search/TestDocSet.java
[patch] patching file src/java/org/apache/solr/search/CollapseFilter.java
[patch] patching file src/java/org/apache/solr/search/DocSet.java
[patch] patching file src/java/org/apache/solr/search/NegatedDocSet.java
[patch] patching file src/java/org/apache/solr/search/SolrIndexSearcher.java
[patch] patching file src/java/org/apache/solr/common/params/CollapseParams.java
[patch] patching file src/java/org/apache/solr/handler/component/CollapseComponent.java
BUILD SUCCESSFUL
Total time: 0 seconds
源码打上了补丁,然后用 ant 构建源码:
D:\apache-solr-1.3.0>ant dist
在 D:/apache-solr-1.3.0/dist 目录下可以找到编译好的 solr 了。然后把 solr 放到 tomcat 中去运行它,把下面的内容保存在 TOMCAT_HOME/conf/Catalina/localhost/solr.xml 文件中:
<Context docBase="D:\apache-solr-1.3.0\dist\apache-solr-1.3.0.war" reloadable="true" >
<Environment name="solr/home" type="java.lang.String" value="D:\apache-solr-1.3.0\example\solr" override="true" />
</Context>
修改 D:\apache-solr-1.3.0\example\solr\conf\solrconfig.xml 使 solr 可以支持 collapse。
定义搜索组件,在 QueryComponent 附近:
<searchComponent name="collapse" class="org.apache.solr.handler.component.CollapseComponent" />
定义一个 handler 使用上面的搜索组件:
<requestHandler name="collapse" class="solr.SearchHandler">
<!-- default values for query parameters -->
<lst name="defaults">
<str name="echoParams">explicit</str>
</lst>
<arr name="components">
<str>collapse</str>
<str>debug</str>
</arr>
</requestHandler>
安装启动 tomcat,现在提交一些数据给它,用官方的示例数据就可以了。运行:
D:\apache-solr-1.3.0\example\exampledocs>java -Durl=http://localhost:8080/solr/update -Dcommit=yes -jar post.jar *.xml
SimplePostTool: version 1.2
SimplePostTool: WARNING: Make sure your XML documents are encoded in UTF-8, other encodings are not currently supported
SimplePostTool: POSTing files to http://localhost:8080/solr/update..
SimplePostTool: POSTing file hd.xml
SimplePostTool: POSTing file ipod_other.xml
SimplePostTool: POSTing file ipod_video.xml
SimplePostTool: POSTing file mem.xml
SimplePostTool: POSTing file monitor.xml
SimplePostTool: POSTing file monitor2.xml
SimplePostTool: POSTing file mp500.xml
SimplePostTool: POSTing file sd500.xml
SimplePostTool: POSTing file solr.xml
SimplePostTool: POSTing file spellchecker.xml
SimplePostTool: POSTing file utf8-example.xml
SimplePostTool: POSTing file vidcard.xml
SimplePostTool: COMMITting Solr index changes..
http://localhost:8080/solr/admin/stats.jsp 有结果了? 有了。然后开始查询试试看。
查询:http://localhost:8080/solr/select/?q=*%3A*&indent=on&qt=collapse&collapse=true&collapse.field=popularity&collapse.threshold=1
结果: