1 //向solr索引库中添加索引
2 public void addDoc() throws Exception {
3 //创建solr客户端的对象
4 HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core");
5 //创建文档对象
6 SolrInputDocument sd = new SolrInputDocument();
7
8 sd.addField("id", "001");
9 sd.addField("product_name", "solr教程");
10 sd.addField("product_catalog_name", "IT技术");
11 sd.addField("product_description", "很厉害的技术");
12 sd.addField("product_price", 299);
13 sd.addField("product_picture", "001.jpg");
14 //把文档加入服务器
15 client.add(sd);
16 client.commit();
17 }

删除文档

1 public void delDoc() throws Exception {
2 //创建solr客户端的对象
3 HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core");
4 client.deleteById("001");
5 // client.deleteByQuery("product_name:青蛙"); 根据查询删除
6 client.commit();
7 }

查询

public void queryDoc() throws Exception {
//创建solr客户端的对象
HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core");
//创建solr的查询对象
SolrQuery sq = new SolrQuery();
//设置查询条件
sq.set("q", "product_name:青蛙");
sq.set("fq", "product_price:[10 TO 100]");
sq.addSort("product_price",ORDER.asc);
//查询
QueryResponse qr = client.query(sq);
//获得查询结果
SolrDocumentList results = qr.getResults();
//获得查询的记录数
long numFound = results.getNumFound();
System.out.println("查询的记录数是 " + numFound);
for (SolrDocument sd : results) {
//获得文档域
String id = (String) sd.getFieldValue("id");
String product_name = (String) sd.getFieldValue("product_name");
String product_catalog_name = (String) sd.getFieldValue("product_catalog_name");
String product_description = (String) sd.getFieldValue("product_description");
double product_price = (double) sd.getFieldValue("product_price");
System.out.println("--------------");
System.out.println("id " + id);
System.out.println("商品名字 " + product_name);
System.out.println("商品类别名字 " + product_catalog_name);
System.out.println("商品描述 " + product_description);
System.out.println("商品价格 " + product_price);
}
}

带高亮的查询

 1 public void queryDoc1() throws Exception {
2 //创建solr客户端的对象
3 HttpSolrClient client = new HttpSolrClient("http://localhost:8080/solr/core");
4 //创建solr的查询对象
5 SolrQuery sq = new SolrQuery();
6 //设置查询条件
7 sq.set("q", "product_name:青蛙");
8 sq.set("fq", "product_price:[10 TO 100]");
9 sq.addSort("product_price",ORDER.asc);
10 //开启高亮
11 sq.setHighlight(true);
12 sq.addHighlightField("product_name");
13 sq.setHighlightSimplePre("<b>");
14 sq.setHighlightSimplePost("</b>");
15 //查询
16 QueryResponse qr = client.query(sq);
17 //获得查询结果
18 SolrDocumentList results = qr.getResults();
19 //获得查询的记录数
20 long numFound = results.getNumFound();
21 System.out.println("查询的记录数是 " + numFound);
22 for (SolrDocument sd : results) {
23 //获得文档域
24 String id = (String) sd.getFieldValue("id");
25 String product_name = (String) sd.getFieldValue("product_name");
26 String product_catalog_name = (String) sd.getFieldValue("product_catalog_name");
27 String product_description = (String) sd.getFieldValue("product_description");
28 double product_price = (double) sd.getFieldValue("product_price");
29 System.out.println("--------------");
30 System.out.println("id " + id);
31 System.out.println("商品名字 " + product_name);
32 System.out.println("商品类别名字 " + product_catalog_name);
33 System.out.println("商品描述 " + product_description);
34 System.out.println("商品价格 " + product_price);
35 //获得高亮的结构体
36 Map<String, Map<String, List<String>>> highlighting = qr.getHighlighting();
37 if(highlighting != null){
38 //根据id来获得某一个域的高亮的内容
39 Map<String, List<String>> map = highlighting.get(id);
40 //根据具体的域来获得高亮内容
41 List<String> list = map.get("product_name");
42 if(list != null && list.size() > 0){
43 //打印高亮内容
44 for (String str : list) {
45 System.out.println(str);
46 }
47 }
48 }
49 }
50 }

SolrJ使用的更多相关文章

  1. 我与solr(四)--solrJ

    SolrJ索引库: solr提供的一个客户端操作框架,在文件/solr6.2/dist下面可以找到该jar包solrj.jar以及相关jar包,可以使用maven添加. java使用solrJ如下: ...

  2. Solrj和Solr DIH索引效率对比分析

    测试软件环境: 1.16G windows7 x64  32core cpu . 2.jdk 1.7  tomcat 6.x  solr 4.8 数据库软件环境: 1.16G windows7 x64 ...

  3. Solr JAVA客户端SolrJ 4.9使用示例教程

    http://my.oschina.net/cloudcoder/blog/305024 简介 SolrJ是操作Solr的JAVA客户端,它提供了增加.修改.删除.查询Solr索引的JAVA接口.So ...

  4. [solr] - SolrJ增删查

    使用SolrJ进行对Solr的增.删.查功能. 参考引用: http://wiki.apache.org/solr/Solrj Eclipse中新建一个项目:TestSolr 其中SorlJ的Lib包 ...

  5. 【solr】java整合solr5.0之solrj的使用

    1.首先导入solrj需要的的架包 2.需要注意的是低版本是solr是使用SolrServer进行URL实例的,5.0之后已经使用SolrClient替代这个类了,在添加之后首先我们需要根据schem ...

  6. Solr5.3.1 SolrJ查询索引结果

    通过SolrJ获取Solr检索结果 1.通过SolrParams的方式提交查询参数 SolrClient solr = new HttpSolrClient("http://localhos ...

  7. 使用solrj操作solr索引库

    (solrj)初次使用solr的开发人员总是很郁闷,不知道如何去操作solr索引库,以为只能用<五分钟solr4.5教程(搭建.运行)>中讲到的用xml文件的形式提交数据到索引库,其实没有 ...

  8. Solr使用初探——SolrJ的使用

    二.SolrJ的使用 SolrJ覆盖了solr的全部功能,下面将自己在实际开发中所使用的程序粘贴出来并适当加以解释,由于本人比较菜,代码书写不是那么的精练,还请见谅. 1.  创建solrserver ...

  9. 使用solrj进行DIH操作

    背景说明:在一个项目中需要将Mongodb中的数据导入到solr中完成搜索.在solr中Mysql数据库有对应的DIH包,可以通过配置sql语句完成数据的导入.Mongodb下也有开源的工具用来实现数 ...

  10. 使用solrj操作solr索引库,solr是lucene服务器

    客户端开发 Solrj 客户端开发 Solrj Solr是搭建好的lucene服务器 当然不可能完全满足一般的业务需求 可能 要针对各种的架构和业务调整 这里就需要用到Solrj了 Solrj是Sol ...

随机推荐

  1. 整站死链接检测与查询工具 Xenu(可以用来制作sitemap)

    http://www.wocaoseo.com/thread-286-1-1.html 很多新手朋友们都会去找一些工具来检查网站死链接,这里给大家分享一款非常好用的检查网站死链接的工具xenu,大家可 ...

  2. 跳转语句 break 和 continue

    break跳出循环体,结束本次循环. continue结束本次循环. for(var i=0; i<5; i++){ if(i == 3) break; document.write(" ...

  3. 使用jQuery设置和获取css样式

  4. 用android studio多渠道打包

    1. 官方教程 https://developer.android.com/studio/build/build-variants.html 2. 设置Build Types参数 打开 Project ...

  5. Unity资源引用问题

    前几天做项目时,遇到一个奇怪的问题: 从一处复制了一个预制体,预制体上面还附有一个材质球,材质球上关联着另一张贴图. 将所有关联的东西,均Copy两份,然后关联成一个新的预制体,最后用项目内的读取内存 ...

  6. vue中饼状图的使用

    图形构建子组件 <template> <div> <div id="myChart" :style="echartStyle"&g ...

  7. C005:计算多项式的值

    程序: #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { float x; do{ printf("E ...

  8. C#编辑GridView的Thead

    背景 有这样一个需求,需要更改GridView的Thead,即表头.不只是多行表头,而是任意的内容,可能是一段文字,也可能是一个图片,综合网上的一些资料,大致整理出一些做法. 内容 大致有两种方法 第 ...

  9. 如何使用JSTL获取并显示数据

    首先在×××controller里查询数据,并绑定,代码如下: /** * 显示所有租借信息 默认进入这个方法 * * @param resp * @param req * @param manage ...

  10. Java实现获取命令行中的指定数据

    构造一个ping的命令类这个类中可以设置需要ping的目标域名类提供方法public void exec();方法执行完毕后可以读取ping的次数,ping的成功回应包个数ping的丢包个数,ping ...