Solr——Java应用
Solr有一个客户端SolrJ
创建一个Java Project
引入Jar包

添加test类
package com.solr.test; import java.io.IOException;
import java.util.List; import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
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 com.solr.entity.Person; public class SolrJTest {
//指定solr服务器的地址
private final static String SOLR_URL = "http://localhost:8080/solr/"; /**
* 创建SolrServer对象
*
* 该对象有两个可以使用,都是线程安全的
* 1、CommonsHttpSolrServer:启动web服务器使用的,通过http请求的
* 2、 EmbeddedSolrServer:内嵌式的,导入solr的jar包就可以使用了
* 3、solr 4.0之后好像添加了不少东西,其中CommonsHttpSolrServer这个类改名为HttpSolrClient
*
* @return
*/
public HttpSolrClient createSolrServer(){
HttpSolrClient solr = null;
solr = new HttpSolrClient(SOLR_URL);
return solr;
} /**
* 往索引库添加文档
* @throws IOException
* @throws SolrServerException
*/
public void addDoc() throws SolrServerException, IOException{
//构造一篇文档
SolrInputDocument document = new SolrInputDocument();
//往doc中添加字段,在客户端这边添加的字段必须在服务端中有过定义
document.addField("id", "8");
document.addField("name", "周新星");
document.addField("description", "一个灰常牛逼的军事家");
//获得一个solr服务端的请求,去提交 ,选择具体的某一个solr core
HttpSolrClient solr = new HttpSolrClient(SOLR_URL + "my_core");
solr.add(document);
solr.commit();
solr.close();
} /**
* 根据id从索引库删除文档
*/
public void deleteDocumentById() throws Exception {
//选择具体的某一个solr core
HttpSolrClient server = new HttpSolrClient(SOLR_URL+"my_core");
//删除文档
server.deleteById("8");
//删除所有的索引
//solr.deleteByQuery("*:*");
//提交修改
server.commit();
server.close();
} /**
* 查询
* @throws Exception
*/
public void querySolr() throws Exception{
HttpSolrClient solrServer = new HttpSolrClient(SOLR_URL+"my_core/");
SolrQuery query = new SolrQuery();
//下面设置solr查询参数
//query.set("q", "*:*");// 参数q 查询所有
query.set("q","周星驰");//相关查询,比如某条数据某个字段含有周、星、驰三个字 将会查询出来 ,这个作用适用于联想查询 //参数fq, 给query增加过滤查询条件
query.addFilterQuery("id:[0 TO 4]");//id为0-4 //给query增加布尔过滤条件
//query.addFilterQuery("description:演员"); //description字段中含有“演员”两字的数据 //参数df,给query设置默认搜索域
query.set("df", "name"); //参数sort,设置返回结果的排序规则
query.setSort("id",SolrQuery.ORDER.desc); //设置分页参数
query.setStart(0);
query.setRows(10);//每一页多少值 //参数hl,设置高亮
query.setHighlight(true);
//设置高亮的字段
query.addHighlightField("name");
//设置高亮的样式
query.setHighlightSimplePre("<font color='red'>");
query.setHighlightSimplePost("</font>"); //获取查询结果
QueryResponse response = solrServer.query(query);
//两种结果获取:得到文档集合或者实体对象 //查询得到文档的集合
SolrDocumentList solrDocumentList = response.getResults();
System.out.println("通过文档集合获取查询的结果");
System.out.println("查询结果的总数量:" + solrDocumentList.getNumFound());
//遍历列表
for (SolrDocument doc : solrDocumentList) {
System.out.println("id:"+doc.get("id")+" name:"+doc.get("name")+" description:"+doc.get("description"));
} //得到实体对象
List<Person> tmpLists = response.getBeans(Person.class);
if(tmpLists!=null && tmpLists.size()>0){
System.out.println("通过文档集合获取查询的结果");
for(Person per:tmpLists){
System.out.println("id:"+per.getId()+" name:"+per.getName()+" description:"+per.getDescription());
}
}
} public static void main(String[] args) throws Exception {
SolrJTest solr = new SolrJTest();
//solr.createSolrServer();
solr.addDoc();
//solr.deleteDocumentById();
//solr.querySolr();
}
}
查询结果:
通过文档集合获取查询的结果
查询结果的总数量:3
id:3 name:周节能 description:台湾著名歌手,号称音乐天王
id:2 name:周润发 description:香港著名演员
id:1 name:周星驰 description:香港著名喜剧演员
Solr——Java应用的更多相关文章
- Solr JAVA客户端SolrJ 4.9使用示例教程
http://my.oschina.net/cloudcoder/blog/305024 简介 SolrJ是操作Solr的JAVA客户端,它提供了增加.修改.删除.查询Solr索引的JAVA接口.So ...
- solr java demo 基础入门
<!--solr的maven依赖--> <dependencies> <dependency> <groupId>org.apache.solr&l ...
- solr java代码
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- Solr JAVA客户端SolrJ的使用
一.Solrj简介 SolrJ是操作Solr的JAVA客户端,它提供了增加.修改.删除.查询Solr索引的JAVA接口.SolrJ针对 Solr提供了Rest 的HTTP接口进行了封装, SolrJ底 ...
- solr java api 使用solrj操作zookeeper集群中的solrCloud中的数据
1 导入相关的pom依赖 <dependencies> <dependency> <groupId>org.apache.solr</groupId> ...
- Solr java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect to this MySQL server
在用solr从mysql导入数据的时候,因为linux和本机的数据库不在同一个ip段上, 又因为本地的mysql没有设置远程其它ip可以访问所以就报了如下错误 解决办法: 在mysql任意可以输入查询 ...
- 【solr】java整合solr5.0之solrj的使用
1.首先导入solrj需要的的架包 2.需要注意的是低版本是solr是使用SolrServer进行URL实例的,5.0之后已经使用SolrClient替代这个类了,在添加之后首先我们需要根据schem ...
- solr 安装
1:solr简介 solr是一个开源的搜索引擎,是对lucene做了封装,对外提供类似于webservice接口, 可以使用http请求的方式对solr进行操作. lucene.solr.elasti ...
- solr4.3 java.lang.NumberFormatException
solr java.lang.NumberFormatException 现象:定时每天全量,每隔5分钟增量DIH从mysql导入数据 solr4j返回加过Id列表,一天偶然出现 java.lang. ...
随机推荐
- 【ARM-Linux开发】Linux的SOCKET编程详解
Linux的SOCKET编程详解 1. 网络中进程之间如何通信 进 程通信的概念最初来源于单机系统.由于每个进程都在自己的地址范围内运行,为保证两个相互通信的进 程之间既互不干扰又协调一致工作,操作系 ...
- Django 框架学习 ---- 安装
这里引用了源码方式安装: 1.git clone https://github.com/django/django.git 2.cd django/ 3.python setup.py install ...
- 10.hive安装
上传hive安装包并解压 给hive设置一个软链接 给hive配置环境变量 sudo vim /etc/profile #hive export HIVE_HOME=/opt/modules/hive ...
- [转帖] ./demoCA/newcerts: No such file or directory openssl 生成证书时问题的解决.
接上面一篇blog 发现openssl 生成server.crt 时有问题. 找了一个网站处理了一下: http://blog.sina.com.cn/s/blog_49f8dc400100tznt. ...
- 【转帖】.NET的一点历史故事:作者的一些感想
.NET的一点历史故事:作者的一些感想 https://mp.weixin.qq.com/s?__biz=MzAwNTMxMzg1MA==&mid=2654068684&idx=2&a ...
- 区间DP(入门)括号匹配
https://www.nitacm.com/problem_show.php?pid=8314 思路:类似于https://blog.csdn.net/MIKASA3/article/details ...
- python中全局global和局部nonlocal命名空间
python中全局global和局部nonlocal命名空间 局部名称空间对全局名称空间的变量可以引用,但是无法改变. count = 1 def func1(): count = 2 print(c ...
- Boot-crm管理系统开发教程(二)
ps:昨天将管理员登录的功能完成了,并完美的解决跳过登录从而进入管理界面的bug,今天我们将实现"查询用户"功能. ①在po包中创建Customer类,并编写相关变量和添加set/ ...
- 如何使用Jedis操作redis
public class JredisTest { private static Jedis jedis = new Jedis("localhost", 6379); publi ...
- 使用spring的test时,当配置文件不在classpath下,而在WEB-INF下
@ContextConfiguration(locations = {"file:src/main/webapp/WEB-INF/applicationContext.xml"})