solor5.4学习笔记

1.下载地址:http://archive.apache.org/dist/lucene/solr/

2.与tomcat的整合http://jingyan.baidu.com/article/d8072ac4625b07ec95cefdbe.html

3.为solr添加用户.

  A.在tomcat的配置tomcat-users.xml添加角色也用户

   <role rolename="solr"/>
   <user username="solr" password="solr" roles="solr"/>

  B.在solr的web.xml中配置

 <security-constraint>

<web-resource-collection>
<web-resource-name>Solr Lockdown</web-resource-name>
<url-pattern>/</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<description>This applies only to the "tomcat" security role</description>
<role-name>solr</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>

<login-config>
<auth-method>BASIC</auth-method>
<realm-name>Solr</realm-name>
</login-config>

4.为solr添加code:
在solrhome下新建一个mysoso,把configsetssample_techproducts_configs的config目录拷贝到mysoso中,并在mysoso中添加core.properties(里面写name=mysoso)即可实现code的加载.


5.配置mmseg4j 下载:http://download.csdn.net/detail/yupengdahappy/9037909 (甚至支持到5.5)
A.添加jar包以及dic
B.修改mysoso中的schema.xml来配置mmseg4j的字段类型(其实就是配置分词器) 从https://github.com/chenlb/mmseg4j-solr中复制
注意dicPath最好写绝对路径.
C.把某些字段设置为textComplex的解析器,如title,subject,description


6.添加solrj的支持
A.添加jar 从dist中拷贝solr-solrj-5.4.1.jar,solrj-lib中的全部,以及commons-codec-1.10.jar
B.参考http://my.oschina.net/daxiong0615/blog/521566?p=1 中的MySolr.java,需啊注意的是你可能需要在链接中授权.

7.如果想买包base64的错误,可以尝试去掉登陆认证.(使用8的那种方式,可以解决我的base64的报错)

8.在Java unit中可以通过.但是在web访问中,执行到new HttpSolrClient(URL+"/"+SERVER);就会报错.参见

http://stackoverflow.com/questions/32105513/solr-bad-return-type-error,使用

SystemDefaultHttpClient httpClient = new SystemDefaultHttpClient();
client = new HttpSolrClient(URL + "/" + SERVER,httpClient);

即可解决问题.

9.SB搜索----多个字段同时搜索,使用copyField,参见http://www.icoolxue.com/play/2347的copyField

<field name="a_all" type="textComplex" indexed="true" stored="false" multiValued="true"/>

添加copyField并指定默认field

<defaultSearchField>a_all</defaultSearchField>
<copyField source="a_title" dest="a_all"/>
<copyField source="a_summary" dest="a_all"/>
<copyField source="a_content" dest="a_all"/>
<copyField source="a_author" dest="a_all"/>
<copyField source="a_tagName" dest="a_all"/>
<copyField source="a_catalog" dest="a_all"/>
<copyField source="a_resource" dest="a_all"/>
<copyField source="a_createTime" dest="a_all"/>

10.设置高亮(高亮字段必须store)
query.addHighlightField("a_title").addHighlightField("a_content").setHighlightSimplePre("<em style='color:red;'>").setHighlightSimplePost("</em>").setHighlight(true);

以下地址可以作为配置的参考:
http://my.oschina.net/daxiong0615/blog/521566?fromerr=Q4d0kVy9

http://blog.csdn.net/a925907195/article/details/42491157

11.倒入数据库(建议html过滤关闭,自己在代码中过滤好了再交给solr)

 data-config.xml内容如下:

<dataConfig>
<dataSource name="saicheDB" driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/saichedata?useUnicode=true&amp;characterEncoding=utf-8"
user="root" password="123456" transformer="DateFormatTransformerk,HTMLStripTransformer" />
<document>
<entity dataSource="saicheDB" name="article" query="select id,title as a_title,content as a_content,summary as a_summary,createTime as a_createTime,resource as a_resource,tagName as a_tarName from article">
<field column="a_content" stripHTML="true" />
<field column="a_summary" stripHTML="true" />
</entity>
</document>
</dataConfig>

schema.xml添加charFilter 

<fieldtype name="textComplex" class="solr.TextField" positionIncrementGap="100">
<analyzer>
<charFilter class="solr.HTMLStripCharFilterFactory"/>
<tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="D:/programFiles/solr/mysosodic"/>
</analyzer>

原文地址:https://www.cnblogs.com/yanjunwu/p/5360362.html