一、中文分词安装
1.下载中文分词器的jar包放入\Tomcat 8.5\webapps\solr\WEB-INF\lib中 jar包下载地址:https://pan.baidu.com/s/1mhThgSS
2.打开\solr_home\core1\conf\managed-schema.xml顺便改名为schema.xml,再在最后追加
3.启动tomcat测试中文分词器是否安装成功
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index" useSmart="false"
class="org.wltea.analyzer.lucene.IKAnalyzer" />
<analyzer type="query" useSmart="true"
class="org.wltea.analyzer.lucene.IKAnalyzer" />
</fieldType>
二、配置solr的ExtractingRequestHandler
4.将\solr-5.5.4\solr-5.5.4\contrib\extraction下的jar包复制到\solr_home\core1\lib(目录不存在就新建一个)下
5.将\solr-5.5.4\solr-5.5.4\dist下的solr-cell-5.5.4.jar复制到\solr_home\core1\lib下
6.在\solr_home\core1\conf\solrconfig.xml添加
<lib dir="./lib" regex=".*\.jar" />
<requestHandler name="/update/extract" class="org.apache.solr.handler.extraction.ExtractingRequestHandler">
<lst name="defaults">
<str name="fmap.Last-Modified">last_modified</str>
<str name="uprefix">ignored_</str>
</lst>
<lst name="date.formats">
<str>yyyy-MM-dd</str>
</lst>
</requestHandler>
三、使用solrJ1.导入相应的jar包,jar包在\solr-5.5.4\solr-5.5.4\dist\目录下可以找到 2.创建索引文件
public static void create() throws Exception {
// 链接到本地的core1核心文件
HttpSolrClient server = new HttpSolrClient(URL);
ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update/extract");
up.addFile(new File("C:/docs/7.doc"), "application/word");
up.setParam("literal.id", "doc");
up.setParam("fmap.content", "attr_content");
up.setParam("fmap.content_type", "documentFormat");
up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
server.request(up);
List list = new ArrayList<String>();
// 创建数据
//SolrInputDocument doc = new SolrInputDocument();
//doc.addField("id", "2");
//doc.addField("title", "king");
//doc.addField("author", " asdfsdfsd asdasdsaking king king king king");
//doc.addField("",new File(""));
//server.add(doc);
server.commit();
}
3.查询
public static void query() throws Exception {
HttpSolrClient server = new HttpSolrClient(URL);
// 定义查询内容 * 代表查询所有 这个是基于结果集
SolrQuery query = new SolrQuery("*"); // 定义查询内容
query.setStart(0);// 起始页
query.setRows(4);// 每页显示数量
QueryResponse rsp = server.query(query);
SolrDocumentList results = rsp.getResults();
System.out.println(results.getNumFound());// 查询总条数
for (SolrDocument doc : results) {
System.out.println(doc);
}
}