实现用户数据索引及查询
1. 启动solr
solr start
2. 创建collection
solr create -c user
3. schema中添加field
3.1 solr-5.2.1/server/solr/user/conf/managed-schema中添加
<!--定义IK分词类型-->
<fieldType name="text_ik" class="solr.TextField">
<!--索引时候的分词器-->
<analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<!--查询时候的分词器-->
<analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
<field name="username" type="text_ik" indexed="true" stored="true" multiValued="true"/>
<field name="age" type="text_ik" indexed="true" stored="true"/>
<field name="keywords" type="text_ik" indexed="true" stored="true"/>
3.2 添加IK分词库
filed定义中使用了IKAnalyzer,需要进入相关配置引用分词器
a. solr-5.2.1/contrib/analysis-extras/lib中添加IKAnalyzer3.2.8.jar 下载地址: http://download.csdn.net/detail/buyaore_wo/8946777
b. solrconfig.xml (/solr-5.2.1/server/solr/user/conf)中添加库引用配置,如下
<lib dir="${solr.install.dir:../../../..}/contrib/analysis-extras/lib" regex=".*\.jar" />
4.使用SolrJ添加索引数据
/**
* 添加文档
*/
@Test
public void addDoc() {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "12");
doc.addField("username", "哈哈");
doc.addField("keywords", "哈哈 你好");
doc.addField("age", "18");
UpdateResponse response;
try {
response = httpSolrClient.add(/*"user",*/ doc);
// 提交
httpSolrClient.commit();
// logger.info("########## Query Time :" + response.getQTime());
System.out.println("########## Query Time :" + response.getQTime());
// logger.info("########## Elapsed Time :" +
// response.getElapsedTime());
System.out.println("########## Elapsed Time :"
+ response.getElapsedTime());
// logger.info("########## Status :" + response.getStatus());
System.out.println("########## Status :" + response.getStatus());
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
5.查询数据
@Test
public void testQuery() {
SolrQuery solrQuery = new SolrQuery("keywords:*好");
// solrQuery.setFilterQueries("resourcename:*analytics*");
// SolrQuery solrQuery = new SolrQuery("*:*");
// solrQuery.setFields("id", "title");
solrQuery.setStart(0).setRows(5);
try {
QueryResponse queryResponse = httpSolrClient.query(/*"user",*/
solrQuery);
// logger.info("results:" +
// queryResponse.getResults().getNumFound());
System.out.println("results:"
+ queryResponse.getResults().getNumFound());
SolrDocumentList solrDocumentList = queryResponse.getResults();
for (SolrDocument solrDocument : solrDocumentList) {
Collection<String> fieldNames = solrDocument.getFieldNames();
// logger.info("==========================================");
System.out
.println("==========================================");
for (String field : fieldNames) {
// logger.info(field + ":" +
// solrDocument.getFieldValue(field));
System.out.println(field + ":"
+ solrDocument.getFieldValue(field));
}
}
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
中途可能遇到以下这样的异常
org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://192.168.0.12:8983/solr: Expected mime type application/xml but got text/html. <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Error 404 Not Found</title>
</head>
<body><h2>HTTP ERROR 404</h2>
<p>Problem accessing /solr/update. Reason:
<pre> Not Found</pre></p><hr><i><small>Powered by Jetty://</small></i><hr/>
</body>
</html>
原因是没有指定 collection_name