SolrJ管理Demo git地址:https://github.com/UserFengFeng/Solr-.git

1.SolrJ添加索引文档对象

solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,Solrj通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务,如下图(最新版本solr自己已经有自己的服务,可忽略tomcat):

import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.common.SolrInputDocument;import java.io.IOException;public class SolrMain {public static void main(String[] args) throws IOException, SolrServerException {addDoc();}/** 向solr索引库中添加索引库的文档* */public static void addDoc() throws IOException, SolrServerException {String solrUrl = "http://localhost:8983/solr/demoInstance";//创建solr客户端对象HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(6000).build();// 创建文档对象SolrInputDocument sd = new SolrInputDocument();sd.addField("id", "c0001");sd.addField("product_name", "solrDemo");sd.addField("product_catalog_name", "IT技术");sd.addField("product_description", "拓新教育的每一个课程都是用心在做。");sd.addField("product_price", 299);sd.addField("product_picture", "111.jpg");// 把文档加入服务器client.add(sd);// 提交client.commit();}
}

2.Solrj删除索引

public static void main(String[] args) throws IOException, SolrServerException {// 根据id删除索引deleteDoc();
}public static void deleteDoc() throws IOException, SolrServerException {String solrUrl = "http://localhost:8983/solr/demoInstance";//创建solr客户端对象HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(6000).build();// 根据id删除// client.deleteById("c0001");// 删除查询到的所有域client.deleteByQuery("product_name:青蛙");client.commit();
}

3.SolrJ条件查询

public static void main(String[] args) throws IOException, SolrServerException {// 条件查询queryCondition();
}
public static void queryCondition() throws IOException, SolrServerException {String solrUrl = "http://localhost:8983/solr/demoInstance";//创建solr客户端对象HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(6000).build();// 创建solr查询对象SolrQuery sq = new SolrQuery();// 设置查询条件sq.set("q", "product_name:青蛙");// 查询QueryResponse query = client.query(sq);// 获得查询结果SolrDocumentList results = query.getResults();// 获得查询的记录数long numFound = results.getNumFound();System.out.println("记录数:" + numFound);for (SolrDocument sd : results) {// 获得文档域String id = (String) sd.getFieldValue("id");String product_catalog_name = (String) sd.getFieldValue("product_catalog_name");Double product_price = (Double) sd.getFieldValue("product_price");String product_name = (String) sd.getFieldValue("product_name");String product_picture = (String) sd.getFieldValue("product_picture");System.out.println("id" + id);System.out.println("描述" + product_catalog_name);System.out.println("单价" + product_price);System.out.println("商品名称" + product_name);System.out.println("商品图片" + product_picture);System.out.println("------------------------");}
}

结果:

4.solr条件过滤排序分页查询

public static void main(String[] args) throws IOException, SolrServerException {/**  价格区间查询*  排序查询*  分页查询* */queryCondition2();
}// 价格区间查询
public static void queryCondition2() throws IOException, SolrServerException {String solrUrl = "http://localhost:8983/solr/demoInstance";//创建solr客户端对象HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(6000).build();// 创建solr查询对象SolrQuery sq = new SolrQuery();// 设置查询条件sq.set("q", "product_name:青蛙");// 设置过滤条件sq.set("fq", "product_price: [10 TO 100]");// 添加排序//sq.addSort("product_price", SolrQuery.ORDER.asc);sq.addSort("product_price", SolrQuery.ORDER.desc);// 分页查询sq.setStart(0);// 设置每一页的记录数sq.setRows(10);// 查询QueryResponse query = client.query(sq);// 获得查询结果SolrDocumentList results = query.getResults();// 获得查询的记录数long numFound = results.getNumFound();System.out.println("记录数:" + numFound);for (SolrDocument sd : results) {// 获得文档域String id = (String) sd.getFieldValue("id");String product_catalog_name = (String) sd.getFieldValue("product_catalog_name");Double product_price = (Double) sd.getFieldValue("product_price");String product_name = (String) sd.getFieldValue("product_name");String product_picture = (String) sd.getFieldValue("product_picture");System.out.println("id" + id);System.out.println("描述" + product_catalog_name);System.out.println("单价" + product_price);System.out.println("商品名称" + product_name);System.out.println("商品图片" + product_picture);System.out.println("------------------------");}
}

5.solr高亮查询

public static void main(String[] args) throws IOException, SolrServerException {// 高亮查询queryFl();
}public static void queryFl() throws IOException, SolrServerException {String solrUrl = "http://localhost:8983/solr/demoInstance";//创建solr客户端对象HttpSolrClient client = new HttpSolrClient.Builder(solrUrl).withConnectionTimeout(10000).withSocketTimeout(6000).build();// 创建solr查询对象SolrQuery sq = new SolrQuery();// 设置查询条件sq.set("q", "product_name:青蛙 AND product_keywords:吸盘");// 开启高亮(也可对多个域进行高亮)sq.setHighlight(true);sq.addHighlightField("product_name");sq.setHighlightSimplePre("<h1>");sq.setHighlightSimplePost("</h2>");// 查询QueryResponse query = client.query(sq);// 获得查询结果SolrDocumentList results = query.getResults();// 获得查询的记录数long numFound = results.getNumFound();System.out.println("记录数:" + numFound);for (SolrDocument sd : results) {// 获得文档域String id = (String) sd.getFieldValue("id");String product_catalog_name = (String) sd.getFieldValue("product_catalog_name");Double product_price = (Double) sd.getFieldValue("product_price");String product_name = (String) sd.getFieldValue("product_name");String product_picture = (String) sd.getFieldValue("product_picture");//System.out.println("id" + id);//System.out.println("描述" + product_catalog_name);//System.out.println("单价" + product_price);//System.out.println("商品名称" + product_name);//System.out.println("商品图片" + product_picture);System.out.println("------------------------");// 获得高亮的结构体Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();if (highlighting != null) {// 根据id获取每一个域的内容Map<String, List<String>> map = highlighting.get(id);// 根据具体的域来获得高亮内容(多个高亮域的获取下方代码复制换掉域名称即可)List<String> list = map.get("product_name");if (list != null && list.size() > 0) {// 打印高亮内容for (String s : list) {System.out.println("高亮内容" + s);}}}}
}

打印结果:

SolrJ管理solr相关推荐

  1. Solr快速入门第七讲——使用SolrJ管理索引库

    什么是SolrJ? SolrJ是访问Solr服务的Java客户端,提供索引(这里指的就是创建索引.更新索引以及删除索引)和搜索(这里指的是查询索引)的请求方法,SolrJ通常嵌入在业务系统中,通过So ...

  2. 通过SolrJ 4.9管理Solr core

    应用场景 使用solr时,有时需要通过程序动态的加载配置文件,如修改了solrconfig.xml.schema.xml,需要通过重新加载core来达到重新加载配置文件的目的. 另外,针对索引分类,也 ...

  3. Windows系统环境下Solr之Java实战(三)使用solrJ管理索引库

    https://www.cnblogs.com/zhuxiaojie/p/5764680.html https://www.cnblogs.com/xieyupeng/p/9317158.html 转 ...

  4. SolrJ查询Solr数据

    2019独角兽企业重金招聘Python工程师标准>>> 1.首先,我比较懒.我把 apache-solr-3.6.1/dist 目录下所有的包导入到了工程中,除了war包. 2.下面 ...

  5. SolrJ管理SolrCloud

    添加文档 操作步骤 第一步:把solrJ相关的jar包添加到工程中 第二步:创建一个SolrServer对象 需要使用CloudSolrServer子类 构造方法的参数是zookeeper的地址列表 ...

  6. 在java中使用solrj对solr进行CRUD

    如果想要知道如何安装solr,集成IKAnalyzer中文分词器,批量导入数据库数据,java使用参照以下本博主博文: 安装solr https://blog.csdn.net/u013294097/ ...

  7. Solr(二)-Solrj操作Solr

    一.Solrj实现索引库数据的更新 solrj的版本要和solr的版本对应. solrj是一款java操作solr的工具jar包. spring也可以很方便的整合solrj. @Test public ...

  8. 通过Http管理Solr Core,实现索引的分类

    使用的版本为Solr 4.9 查看所有core的状态 使用样例 http://172.168.63.233:8983/solr/admin/cores?action=STATUS 查看某一个core的 ...

  9. SSM商城项目(八)

    1.   学习计划 1.solr集群搭建 2.使用solrj管理solr集群 3.把搜索功能切换到集群版 4.添加商品同步到索引库 2.   什么是SolrCloud SolrCloud(solr 云 ...

最新文章

  1. 23设计模式简介笔记
  2. vivo的android是什么手机图片,vivo iQOO配置好不好 vivo iQOO手机参数和外观图赏
  3. 都是月饼惹的祸 124盒月饼太甜太温柔(结尾有彩蛋)
  4. python中列表字典和字符串的相互转化
  5. LeetCode 1190. 反转每对括号间的子串(栈)
  6. python颜色相关系数_python相关系数 - osc_w6qmyr6s的个人空间 - OSCHINA - 中文开源技术交流社区...
  7. 第一个程序03 - 零基础入门学习汇编语言22
  8. java 工作池_Java线程池的工作原理,好处和注意事项
  9. Spring —— 静态成员的注入
  10. VMWare mac os x 优化神器 beamoff
  11. graphpad细胞增殖曲线_Graphpad 作图教程 | 这份超详细的生存曲线绘制指南,科研新手一看就会!...
  12. VS中如何添加报表控件
  13. 氨基酸三字母转一个字母
  14. 星际争霸1,如何有效提高apm和hotkey
  15. 前端不错的相关网站和论坛
  16. 特征值比对代码/计算相似度代码
  17. 技术总结--android篇(四)--工具类总结
  18. 何谓OTA(Over-the-air programming)?
  19. python leetcode 387. First Unique Character in a String
  20. Godaddy子域名转向外部IP地址设置

热门文章

  1. OS X自带数码测色计的使用
  2. python 百度翻译 有道翻译
  3. Linux下使用百度云盘
  4. Android Studio 安卓手机上实现火柴人动画(Java源代码—Python)
  5. Thread 设置 IsBackground true false 的 运行差别
  6. 【数制转换】-八进制转换为二进制
  7. text-shadow和文字颜色渐变冲突问题
  8. Three.js常见光源类型
  9. c语言能对16进制数比较大小,C语言中的二进制数、八进制数和十六进制数
  10. 【ubuntu】Ubuntu中Android SDK下载跟配置