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. 商品表中的字段为: 配置内容是: < ...
随机推荐
- android 工具类之图片加工
/** * 图片加工厂 * * @author way * */ public class ImageUtil { /** * 通过路径获取输入流 * * @param path * 路径 * @re ...
- Flex SuperTabNavigator Tab标签图片不显示或图片显示不完全
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="ht ...
- 关于以DataTable形式批量写入数据的案例
void IDataAccess.CommandDataTable(DataTable dt, string ProcedureName, System.Data.Common.DbParameter ...
- STM32 + RT Thread OS 学习笔记[三]
RTGUI 据说RTGUI是多线程的,因此与RT-Thread OS的耦合度较高,有可能要访问RT-Thread的线程控制块.如果要移植到其它OS,估计难度较大.目前还处于Alpha状态,最终将会包含 ...
- 2015UESTC 暑假集训总结
day1: 考微观经济学去了…… day2: 一开始就看了看一道题目最短的B题,拍了半小时交了上去wa了 感觉自己一定是自己想错了,于是去拍大家都过的A题,十分钟拍完交上去就A了 然后B题写了一发暴力 ...
- JS的加载方式---同步和异步
同步加载及异步加载,只有这两种方式. 动态加载是异步加载的方式之一. ajax加载也是异步加载.
- [CSS] Introduction to CSS Columns
Learn how to use CSS columns to quickly lay out fluid columns that are responsive, degrade gracefull ...
- input text框和 checkbox 连带被选中的情况
<html> <head></head> <body> <ul> <li><input type="checkb ...
- c++笔试题两道,求解当中一道
1.Implement a functionthat prints the numbers from 1 to 100.But for multiples of three(3) print &quo ...
- 9款风格华丽的jQuery/CSS3插件
今天向大家分享9款效果相当不错的jQuery/CSS3插件,不多说,直接来看看这些插件吧. 1.jQuery动画下拉菜单Smart Menu 这是一款基于jQuery的动画下拉菜单,子菜单外观比较时尚 ...