solr4.7
solr在tomcat下安装
单机版,无zookeeper
$SOLR_HOME为E:\learn_sorftware\solr\apache-tomcat-7.0.64\tomcat-solr\
1、从solr-4.7.0\example\webapps复制solr.war到$SOLR_HOME;
2、将solr-4.7.0\example下的solr文件复制到$SOLR_HOME目录;
3、在tomcat\conf\Catalina\localhost下新建solr.xml,如下:
<?xml version="1.0" encoding="utf-8"?>
<Context docBase="E:\learn_sorftware\solr\apache-tomcat-7.0.64\tomcat-solr\solr.war"
reloadable="true" >
<Environment name="solr/home" type="java.lang.String"
value="E:\learn_sorftware\solr\apache-tomcat-7.0.64\tomcat-solr\solr" override="true" />
</Context>
4、加入solr日志输出信息
a、solr-4.7.0\example\lib\ext下的jar包全部复制到tomcat\lib下;
b、solr-4.7.0\example\resources\log4j.properties复制到tomcat\lib下;
启动tomcat,输入localhost:8080\solr即可
配置中文分词
选择的是mmseg4j-solr,各种分词方式的比较:http://www.oschina.net/question/853816_223155
在对应的core下建立lib文件夹,将对应的mmseg4j-solr-2.0.0.jar、mmseg4j-core-1.10.0.jar、mmseg4j-analysis-1.9.1.jar添加进去
在对应的core的conf\schema.xml的fieldtype位置中加入:
<!-- mmseg4j -->
<fieldtype name="text_mmseg4j_complex" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="dic"/>
</analyzer>
</fieldtype>
<fieldtype name="text_mmseg4j_maxword" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" />
</analyzer>
</fieldtype>
<fieldtype name="text_mmseg4j_simple" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<!--<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="D:/custom/path/to/my_dic" />-->
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="dic"/>
</analyzer>
</fieldtype>
字段使用mmseg4j分词,在schema.xml的field位置中加入:
<field name="my_content_simple" type="text_mmseg4j_simple" indexed="true" stored="false" multiValued="true"/>
<field name="my_content_complex" type="text_mmseg4j_complex" indexed="true" stored="false" multiValued="true"/>
<field name="my_content_maxword" type="text_mmseg4j_maxword" indexed="true" stored="false" multiValued="true"/>
分词结果分析,在对应的core下的analysis的Index中输入一句话,选择具体的field或者type进行分析即可。
备注:
会报错,org.apache.solr.common.SolrException; java.lang.IllegalStateException: TokenStream contract violation: reset()/close() call missing, reset() called multiple times, or subclass does not call super.reset(). Please see Javadocs of TokenStream class for more information about the correct consuming workflow.
网上查资料说这是一个bug,需要修改源码:mmseg4j-analysis.jar下的MMSegTokenizer这个类,里面reset方法加一句super.reset();
public void reset() throws IOException {
super.reset();
mmSeg.reset(input);
}
最终完成效果对比
Text的效果:
都是单个字
搜索效果:
单字匹配
text_mmseg4j_complex的效果:
分词
搜索效果:
没有匹配
text_mmseg4j_maxword的效果:
搜索效果:
可以进行匹配,并且效果很好。
text_mmseg4j_simple的效果:
对比觉得text_mmseg4j_maxword的效果更好一些。
基于solr的java搜索代码
1、schema.xml的field位置中加入,已存在的就不用加了
<field name="description_test" type="text_mmseg4j_maxword" indexed="true" stored="true" multiValued="true"/>
<field name="name_test" type="text_mmseg4j_maxword" indexed="true" stored="true" multiValued="true"/>
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="price" type="float" indexed="true" stored="true"/>
2、创建product实体类,加上getset方法
@Field(value="id")
private int id;
@Field(value="name_test")
private String name;
@Field(value="price")
private float price;
@Field(value="description_test")
private String description;
3、代码测试
String urlString;
@Before
public void init(){
urlString = "http://localhost:8080/solr";
}
@Test
public void addIndex() throws SolrServerException, IOException{
SolrServer solr = new HttpSolrServer(urlString);
Product product=new Product();
product.setId(1);
product.setName("lucene 是一个全文检索工具包");
product.setDescription("solr 是一个 全文检索服务器...");
product.setPrice(98);
solr.addBean(product);
solr.commit();
}
@Test
public void deleteIndex() throws SolrServerException, IOException{
SolrServer solr = new HttpSolrServer(urlString);
solr.deleteById("1");
solr.commit();
}
@Test
public void updateIndex() throws IOException, SolrServerException{
SolrServer solr = new HttpSolrServer(urlString);
Product product=new Product();
product.setId(1);
product.setName("书籍");
product.setDescription("java");
product.setPrice(98);
solr.addBean(product);
solr.commit();
}
@Test
public void queryIndex() throws SolrServerException{
SolrServer solr = new HttpSolrServer(urlString);
SolrQuery params=new SolrQuery();
params.setQuery("name_test:检索");
params.setStart(0);
params.setRows(10);
//开启高亮的功能
params.setHighlight(true);
//设置高亮显示的格式
params.setHighlightSimplePre("<font color='red'>");
params.setHighlightSimplePost("</font>");
//需要将那些字段进行高亮 hl.fl针对哪个字段进行高亮
params.setParam("hl.fl", "name_test");
QueryResponse queryResponse=solr.query(params);
//返回没有高亮的结果
SolrDocumentList documentList=queryResponse.getResults();
///返回高亮的结果
/**
* 第一个key代表的是document id
* 第二个map代表的是高亮的字段
*/
Map<String, Map<String, List<String>>> maplist=queryResponse.getHighlighting();
for(SolrDocument solrDocument:documentList){
Map<String, List<String>> mapfield=maplist.get(solrDocument.get("id"));
List<String> listString=mapfield.get("name_test");
System.out.println("highlighting:"+listString.toString());
System.out.println("id:"+solrDocument.get("id").toString());
System.out.println("name:"+solrDocument.get("name_test").toString());
System.out.println("price:"+solrDocument.get("price").toString());
System.out.println("description:"+solrDocument.get("description_test").toString());
}
}