实现用户数据索引及查询

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

solr实战-(一)的更多相关文章

  1. Solr实战:使用Hue+Solr实现标签查询

    公司最近在研究多条件组合查询方案,Google的一位技术专家Sam和我们讨论了几个备选方案. Sam的信: 我做了进一步研究,目前有这么几种做法: 1) 最直接粗暴,只做一个主index,比如按行业+ ...

  2. solr集群与项目实战

    什么是 SolrCloud : SolrCloud(solr 云)是 Solr 提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时 ...

  3. Solr安装入门、查询详解

    Solr安装入门:http://www.importnew.com/12607.html 查询详解:http://www.360doc.com/content/14/0306/18/203871_35 ...

  4. solr与.net系列课程(二)solr的配置文件及其含义

    solr与.net系列课程(二)solr的配置文件及其含义  本节内容还是不会涉及到.net与数据库的内容,但是不要着急,这都是学时solr必学要掌握的东西,solr可不是像其他的dll文件一样,只需 ...

  5. solr的配置文件及其含义

    solr与.net系列课程(二)solr的配置文件及其含义    solr与.net系列课程(二)solr的配置文件及其含义  本节内容还是不会涉及到.net与数据库的内容,但是不要着急,这都是学时s ...

  6. Solr安装入门

    Solr安装入门:http://www.importnew.com/12607.html 查询详解:http://www.360doc.com/content/14/0306/18/203871_35 ...

  7. json数据处理实战:Kafka+Flume+Morphline+Solr+Hue数据组合索引

    背景:Kafka消息总线的建成,使各个系统的数据得以在kafka节点中汇聚,接下来面临的任务是最大化数据的价值,让数据“慧”说话. 环境准备: Kafka服务器*3. CDH 5.8.3服务器*3,安 ...

  8. Solr集群、KI分词、项目实战

    Solr是一个高性能,采用Java开发,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展并对查询性能进行了优化,并且提供了一个完善 ...

  9. 转:手把手教你如何玩转Solr(包含项目实战)

    原文地址:手把手教你如何玩转Solr(包含项目实战) 参考原文

随机推荐

  1. codevs1288 埃及分数(IDA*)

    1288 埃及分数  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 在古埃及,人们使用单位分数的和(形如1/a的 ...

  2. [Luogu 1850] noip16 换教室

    [Luogu 1850] noip16 换教室 好久没有更博客了,先唠嗑一会,花了两天的空闲时间大致做完了昨年的noip真题 虽然在经过思考大部分题目都可出解(天天爱跑步除外),但是并不知道考试时候造 ...

  3. 6.10---mybatis的实体---接口---接口映射---主配置文件

  4. Node.js文件系统Api总结

    //公共引用 var fs = require('fs'), path = require('path'); 1.读取文件readFile函数 //readFile(filename,[options ...

  5. Android Unresolved Dependencies

    在Android Studio的开发中,在软件中集成了ButterKnife插件,另外需要集成ButterKnife的jar包.因为本地没有现成的,所以在module的build.gradle文件中添 ...

  6. 安装Windows服务,一直提示系统正在关机的错误。

    错误截图如下: 问题概况: 在本机安装没问题,程序没问题. 安装到公司的测试环境就报错了!以管理员身份运行也不行. 解决方案: 1.最后发现是360安全防护中心拦截了.具体解决过程如下: 2.进入36 ...

  7. python迭代器,生成器,推导式

    可迭代对象 字面意思分析:可以重复的迭代的实实在在的东西. list,dict(keys(),values(),items()),tuple,str,set,range, 文件句柄(待定) 专业角度: ...

  8. BZOJ 2959: 长跑 LCT_并查集_点双

    真tm恶心...... Code: #include<bits/stdc++.h> #define maxn 1000000 using namespace std; void setIO ...

  9. BZOJ 5466: [Noip2018]保卫王国 动态DP

    Code: // luogu-judger-enable-o2 #include<bits/stdc++.h> #define ll long long #define lson (now ...

  10. eas之事件

    选择事件 选择事件是在选择块发生变化后发出.table.addKDTSelectListener(new KDTSelectListener(){    public void tableSelect ...