solr实战-(一)
来源:互联网 发布:mysql 登录失败 编辑:程序博客网 时间:2024/06/05 04:32
实现用户数据索引及查询
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添加索引数据
/** * 添加文档 */@Testpublic 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
5.查询数据
@Testpublic 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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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>
<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
0 0
- solr实战-(一)
- Window+solr+tomcat+IKAnalyzer (solr实战一)
- 深入Solr实战
- solr 实战--起步
- solr实战问题集合
- solr实战代码事例
- Solr学习实战
- 实战: SOLR的分布式部署(复制)CollectionDistribute 快照分发 (一)
- Solr 简介(一)
- solr 一 getting start
- Solr云(一)
- 一、Solr综述
- solr笔记一
- 一、solr配置
- solr总结(一)
- (一)Solr介绍
- Solr例子一
- Solr学习(一)
- 百度地图提供服务——本地搜索,范围搜索
- spring事务声明的几种传播特性
- 集合数据分批处理
- HDOJ 3420 Bus Fair(动规)
- JSP基础(三)JSP内置对象 利用application对象做一个简单的网页计数器
- solr实战-(一)
- 小议进程与线程
- 多线程
- 百度地图提供服务——本地搜索,范围搜索
- EularProject 42:单词解码出来的三角形数
- android用户界面屏幕分辨率参考
- 百度地图提供服务——本地搜索,范围搜索
- 百度地图提供服务——本地搜索,范围搜索
- 百度地图提供服务——本地搜索,范围搜索