实现用户数据索引及查询

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. centos安装lamp步骤还可以

    1. 用yum安装Apache,Mysql,PHP. 1.1安装Apache yum install httpd httpd-devel 安装完成后,用/etc/init.d/httpd start  ...

  2. python 46 边界圆角 、a_img_list标签 、伪类选择器

    一:边界圆角 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <ti ...

  3. ccf 201803-4 棋局评估 (对抗搜索)

    棋局评估 问题描述 Alice和Bob正在玩井字棋游戏. 井字棋游戏的规则很简单:两人轮流往3*3的棋盘中放棋子,Alice放的是“X”,Bob放的是“O”,Alice执先.当同一种棋子占据一行.一列 ...

  4. BZOJ 4517 组合数+错排

    思路: 预处理错排 然后C(n,m)*s[n-m-1]就是答案了 特判n-m-1<0 //By SiriusRen #include <cstdio> using namespace ...

  5. java 实现yaml 数据转json与map

    首先引入snakeyaml-1.16.jar的包. 直接上代码: package com.ming.yaml; import java.util.Map; import org.yaml.snakey ...

  6. C#url相关知识

    C#中Url地址重定向的方法: 1:Response.Redirect(url); 这个跳转页面的方法跳转的速度不快,因为它要走2个来回(2次postback),但他可以跳转到任何页面,没有站点页面限 ...

  7. windows phone数据网络开发

    LINQ LINQ的全称是Language INtegrated Query,即语言集成查询.LINQ是一种查询语言,不仅可以对数字库进行查询,还可以对.net的数据集.数组.Xml文档等对象进行查询 ...

  8. 【C++】cin、cout的效率比scanf和printf低的解决方法

    玩竞赛的同学应该发现了C++中直接调用cout.cin的效率要比printf和scanf的效率要低. 要解决这个问题,只需要在前面加上一句 std::ios::sync_with_stdio(fals ...

  9. 【sqli-labs】 less26a GET- Blind based -All you SPACES and COMMENTS belong to us -String-single quotes-Parenthesis(GET型基于盲注的去除了空格和注释的单引号括号注入)

    这个和less26差不多,空格还是用%a0代替,26过了这个也就简单了 ;%00 可以代替注释,尝试一下 order by 3 http://192.168.136.128/sqli-labs-mas ...

  10. 点击button 触发另一个button 事件

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs&quo ...