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. 商品表中的字段为: 配置内容是: < ...
随机推荐
- 前端MVC学习总结(三)——AngularJS服务、路由、内置API、jQueryLite
一.服务 AngularJS功能最基本的组件之一是服务(Service).服务为你的应用提供基于任务的功能.服务可以被视为重复使用的执行一个或多个相关任务的代码块. AngularJS服务是单例对象, ...
- android UI开源库
. ActionBarSherlock ActionBarSherlock是一个独立的Android设计库,可以让Android 2.x的系统也能使用ActionBar.此 外,ActionBarSh ...
- arm_cm4.c关于kinetis的修改
/***********************************************************************/ /* * Initialize the NVIC t ...
- PostgreSQL下,对汉字按拼音排序
参考学习此文: http://blog.163.com/digoal@126/blog/static/163877040201173003547236/ 建库 postgres=# \l List o ...
- 从零开始学android开发-项目打包发布
右键项目 选择[android tools]-[export signed application package] 点击[next] 如果没有keystore可以选择[create new keys ...
- Ruby Scripting - Array
A literal array is created by putting squarebrackets around a comma-separated list of elements eg: # ...
- Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp
C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...
- iOS开发——动画编程Swift篇&(一)UIView基本动画
UIView基本动画 // MARK: - UIView动画 ------------------------------------- // MARK: - UIView动画-淡入 @IBActio ...
- 自增锁ID复用问题
mysql> select * from pp; +----+------+ | id | name | +----+------+ | xx | | xx | | xx | | xx | | ...
- smartPtr指针的实现
编写一个智能指针类.智能指针是一种数据类型,一般用模板实现,模拟指针行为的同时还提供自动来及回收机制.它会自动记录SmartPointer<T*>对象的引用计数,一旦T类型对象的引用计数为 ...