Solr中初学Demo
import java.util.Collection;
import java.util.Date; import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test; public class TestSolr { String baseURL = "http://192.168.1.99:8983/solr";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); /**
* 获取操作solr的客户端对象
* @throws Exception
*/
@Test
public void test1() {
//指定solr的连接地址,注意:默认情况下连接的是collection1这个索引库
//下面的两个baseurl效果一样
//String baseURL = "http://192.168.1.171:8983/solr/collection1";
String baseURL = "http://192.168.1.99:8983/solr";
HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL);
System.out.println(httpSolrServer.toString());
} /**
* 建立索引-1
*
* add(HttpSolrServer server,SolrInputDocument doc){
* server.add(doc);
* server.commit();
* }
* @throws Exception
*/
@Test
public void test2() throws Exception {
//把数据封装为一个document
SolrInputDocument doc = new SolrInputDocument();
doc.setField("id", "1");
doc.setField("name", "crxy1");//这个字段必须在schema.xml文件中定义了,否则会设置失败.这里id和name都已经在schema.xml文件中定义了.
doc.setField("last_modified", new Date());
//把这个文档添加到solr中
httpSolrServer.add(doc);//也会把数据添加到内存中,但是查询不到,因为没有在内存中生成segment,执行软提交的时候才会生成
//把这个添加操作提交
httpSolrServer.commit();//(硬提交)这个提交其实是表示把索引数据直接提交到硬盘中,并且可以保证数据能够查询到
//httpSolrServer.commit(true, true, true);//软提交,数据保存在内存中,并且保证数据可以查询
/**
* 在工作中,不建议没add一条数据,就硬提交一次,这样太消耗性能
* 建议,为了保证实时读取到新增的数据,可以,每add一条数据,就调用一次软提交
* 如果对数据的实时查询要求不是很高,建议在批量添加数据的时候,可以每添加1000条左右调用一次硬提交。
*/
} /**
* 建立索引-2
* 这种工作中用的比较多
* test2中需要自己去封装set...使用比较少...
* @throws Exception
*/
@Test
public void test3() throws Exception {
Person person = new Person();
person.setId("22");
person.setName("heeh22"); httpSolrServer.addBean(person);
httpSolrServer.commit();
} /**
* 删除
* @throws Exception
*/
@Test
public void test4() throws Exception {
//httpSolrServer.deleteById("1");//根据id删除
httpSolrServer.deleteByQuery("id:22");//根据查询条件删除
httpSolrServer.commit(); } /**
*
* @throws Exception
*/
@Test
public void test5() throws Exception {
//组装查询条件
SolrQuery params = new SolrQuery(); //具体查询条件就要拼字符串
params.setQuery("id:1");
//params.setQuery("name:samsung");//params.setQuery()和params.set()是一样的...建议使用setQuery
//params.set("q", "*:*"); //执行查询请求
QueryResponse response = httpSolrServer.query(params);
//从response中获取返回的结果
SolrDocumentList results = response.getResults();
//获取满足条件的数据总条数
long numFound = results.getNumFound();
System.out.println("总数:"+numFound); //当前查询返回document文档的总数,默认最多返回10条,通过rows控制的
//这个获取的总数是有问题的,如果要做分页,获取总页面数,要用results.getNumFound()来获取
System.out.println(results.size());//如果上面params对象使用params.set("q", "*:*"); 打印的是10 for (SolrDocument solrDocument : results) {
//获取文档中的所有字段
Collection<String> fieldNames = solrDocument.getFieldNames();
for (String field : fieldNames) {
//打印字段和对应字段的值
System.out.println(field+":"+solrDocument.get(field));
}
}
} }
Solr中初学Demo的更多相关文章
- 在Solr中配置和使用ansj分词
在上一节[编译Ansj之Solr插件]中介绍如何编译ansj分词在solr(lucene)环境中使用的接口,本章将介绍如何在solr中使用ansj,其步骤主要包括:下载或者编译ansj和nlp-lan ...
- solr与.net系列课程(八)solr中重跑索引的注意事项
solr与.net系列课程(八)solr中重跑索引的注意事项 我们如果在项目中使用solr,那肯定就是把数据库中的数据跑进solr服务器中,solr有两种操作一种是新建索引,一种是增量索引,这里我们来 ...
- solr中重跑索引
solr与.net系列课程(八)solr中重跑索引的注意事项 solr与.net系列课程(八)solr中重跑索引的注意事项 我们如果在项目中使用solr,那肯定就是把数据库中的数据跑进solr服务 ...
- Solr中Field常用属性
FieldType 实例:<fieldType name="text_ik" class="solr.TextField"></fieldTy ...
- Solr 08 - 在Solr Web管理页面中查询索引数据 (Solr中各类查询参数的使用方法)
目录 1 Solr管理页面的查询入口 2 Solr查询输入框简介 3 Solr管理页面的查询方案 1 Solr管理页面的查询入口 选中需要查询的SolrCore, 然后在菜单栏选择[Query]: 2 ...
- Solr 06 - Solr中配置使用IK分词器 (配置schema.xml)
目录 1 配置中文分词器 1.1 准备IK中文分词器 1.2 配置schema.xml文件 1.3 重启Tomcat并测试 2 配置业务域 2.1 准备商品数据 2.2 配置商品业务域 2.3 配置s ...
- Solr中的group与facet的区别
Solr中的group与facet的区别 如果是简单的使用的话,那么Facet与group都可以用来进行数据的聚合查询,但是他们还是有很大的区别的. 首先上facet跟group的操作: Facet的 ...
- solr中Cache综述
一.概述 Solr查询的核心类就是SolrIndexSearcher,每个core通常在同一时刻只由当前的SolrIndexSearcher供上层的handler使用(当切换SolrIndexSear ...
- 如何将数据库中的数据导入到Solr中
要使用solr实现网站中商品搜索,需要将mysql数据库中数据在solr中创建索引. 1.需要在solr的schema.xml文件定义要存储的商品Field. 商品表中的字段为: 配置内容是: < ...
随机推荐
- vs2013修改默认的开发环境
可能会有朋友和我一样,当安装完VS完成之后,没有选择默认的开发模板,在后面添加新项目时,总是不能选择默认的开发语言,下面给出个简单步骤,记录一下以备用. 看图吧 1.工具>导入和导出设置 2.选 ...
- redis的发布订阅模式
概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer { /* Pubsub */ // 字典,键为频道, ...
- document.getElementById和document.querySelector的区别
zepto中的$(".111")出错,jQuery中$(".111")不出错的原因: zepto用document.querySelector实现,jQuery ...
- jquery与ajax应用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Rstudio安装
1.https://www.r-project.org/下载R语言(注意32位还是46位系统). 2.安装R,尽量默认安装路径,安装路径不要有中文. 3.https://www.rstudio.com ...
- Sublime Text3 激活教程
Sublime Text3激活 在使用Sublime时会定期弹出购买提示框,避免出现购买提示,影响工作效率,我们可以使用网上的激活码,虽然有些不厚道,但是工作以后,一定选择购买正版支持一下. 打开Su ...
- CarDAQ-Plus
Overview CarDAQ-Plus is the most validated and accepted J2534 device in the world. It has been on th ...
- rails中的语法
1. erb文件中的语法说明 erb文件中常混合使用Ruby语言和html语言,以下为两种常见的格式 <% 写逻辑脚本(Ruby语法) %> <%= 直接输出变量值或运算结果 %&g ...
- C++ foreach
考虑下面的需求,对vector<int>中的每个元素加1,如何做? void add(int& lhs) // 注意:要修改主调方法中的数据,这里要使用引用 { lhs= lhs ...
- uoj #118. 【UR #8】赴京赶考 水题
#118. [UR #8]赴京赶考 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/118 Description ...