创建索引

说明:根据id(唯一约束)域来更新Document的内容,如果根据id值搜索不到id域则会执行添加操作,如果找到则更新。

  1. public void testCreateIndex() throws SolrServerException, IOException {
  2. SolrServer solrServer = new HttpSolrServer(urlString);
  3. SolrInputDocument document = new SolrInputDocument();
  4. document.addField("id", "c0001");
  5. document.addField("product_name", "传智java教程");//商品名称
  6. document.addField("product_price", 86.5f);//商品价格
  7. document.addField("product_picture", "382782828.jpg");//商品图片
  8. document.addField("product_description", "这是一本深入浅出讲解java技术的书籍!");//商品描述
  9. document.addField("product_catalog_name", "javabook");//商品分类
  10. UpdateResponse response = solrServer.add(document);
  11. // 提交
  12. solrServer.commit();
  13. }

删除索引

  1. public void testDeleteIndex() throws SolrServerException, IOException {
  2. SolrServer solrServer = new HttpSolrServer(urlString);
  3. //根据id删除
  4. UpdateResponse response = solrServer.deleteById("c0001");
  5. //根据多个id删除
  6. // solrServer.deleteById(ids...);
  7. //自动查询条件删除
  8. // solrServer.deleteByQuery("product_keywords:教程");
  9. // 提交
  10. solrServer.commit();
  11. }

搜索索引

简单搜索

  1. public void testSearch() throws SolrServerException {
  2. SolrServer solr = new HttpSolrServer(urlString);
  3. // 查询对象
  4. SolrQuery query = new SolrQuery();
  5. //设置查询条件,名称“q”是固定的且必须 的
  6. //搜索product_keywords域,product_keywords是复制域包括product_name和product_description
  7. query.set("q", "product_keywords:java教程");
  8. // 请求查询
  9. QueryResponse response = solr.query(query);
  10. // 查询结果
  11. SolrDocumentList docs = response.getResults();
  12. // 查询文档总数
  13. System.out.println("查询文档总数" + docs.getNumFound());
  14. for (SolrDocument doc : docs) {
  15. //商品主键
  16. String id = (String) doc.getFieldValue("id");
  17. //商品名称
  18. String product_name = (String) doc.getFieldValue("product_name");
  19. //商品价格
  20. Float product_price = (Float) doc.getFieldValue("product_price");
  21. //商品图片
  22. String product_picture = (String) doc.getFieldValue("product_picture");
  23. //商品分类
  24. String product_catalog_name = (String) doc.getFieldValue("product_catalog_name");
  25. System.out.println("=============================");
  26. System.out.println(id);
  27. System.out.println(product_name);
  28. System.out.println(product_price);
  29. System.out.println(product_picture);
  30. System.out.println(product_catalog_name);
  31. }
  32. }

组合查询

  1. public void testSearch2() throws SolrServerException {
  2. SolrServer solr = new HttpSolrServer(urlString);
  3. // 查询对象
  4. SolrQuery query = new SolrQuery();
  5. // 搜索product_keywords域,product_keywords是复制域包括product_name和product_description
  6. // 设置商品分类、关键字查询
  7. // query.set("q", "product_keywords:挂钩 AND product_catalog_name:幽默杂货");
  8. query.setQuery("product_keywords:挂钩 AND product_catalog_name:幽默杂货");
  9. // 设置价格范围
  10. query.set("fq", "product_price:[1 TO 20]");
  11. // 查询结果按照价格降序排序
  12. // query.set("sort", "product_price desc");
  13. query.addSort("product_price", ORDER.desc);
  14. // 请求查询
  15. QueryResponse response = solr.query(query);
  16. // 查询结果
  17. SolrDocumentList docs = response.getResults();
  18. // 查询文档总数
  19. System.out.println("查询文档总数" + docs.getNumFound());
  20. for (SolrDocument doc : docs) {
  21. // 商品主键
  22. String id = (String) doc.getFieldValue("id");
  23. // 商品名称
  24. String product_name = (String) doc.getFieldValue("product_name");
  25. // 商品价格
  26. Float product_price = (Float) doc.getFieldValue("product_price");
  27. // 商品图片
  28. String product_picture = (String) doc.getFieldValue("product_picture");
  29. // 商品分类
  30. String product_catalog_name = (String) doc.getFieldValue("product_catalog_name");
  31. System.out.println("=============================");
  32. System.out.println("id=" + id);
  33. System.out.println("product_name=" + product_name);
  34. System.out.println("product_price=" + product_price);
  35. System.out.println("product_picture=" + product_picture);
  36. System.out.println("product_catalog_name=" + product_catalog_name);
  37. }
  38. }

分页、高亮

  1. public void testSearch3() throws SolrServerException {
  2. SolrServer solr = new HttpSolrServer(urlString);
  3. // 查询对象
  4. SolrQuery query = new SolrQuery();
  5. // 设置商品分类、关键字查询
  6. query.setQuery("product_keywords:透明挂钩 ");
  7. // 分页参数
  8. // 每页显示记录数
  9. int pageSize = 2;
  10. // 当前页码
  11. int curPage = 2;
  12. // 开始记录下标
  13. int begin = pageSize * (curPage - 1);
  14. // 起始下标
  15. query.setStart(begin);
  16. // 结束下标
  17. query.setRows(pageSize);
  18. // 设置高亮参数
  19. query.setHighlight(true); // 开启高亮组件
  20. query.addHighlightField("product_name");// 高亮字段
  21. query.setHighlightSimplePre("<span color='red'>");// 前缀标记
  22. query.setHighlightSimplePost("</span>");// 后缀标记
  23. // 请求查询
  24. QueryResponse response = solr.query(query);
  25. // 查询结果
  26. SolrDocumentList docs = response.getResults();
  27. // 查询文档总数
  28. System.out.println("查询文档总数" + docs.getNumFound());
  29. for (SolrDocument doc : docs) {
  30. // 商品主键
  31. String id = (String) doc.getFieldValue("id");
  32. // 商品名称
  33. String product_name = (String) doc.getFieldValue("product_name");
  34. // 商品价格
  35. Float product_price = (Float) doc.getFieldValue("product_price");
  36. // 商品图片
  37. String product_picture = (String) doc.getFieldValue("product_picture");
  38. // 商品分类
  39. String product_catalog_name = (String) doc.getFieldValue("product_catalog_name");
  40. System.out.println("=============================");
  41. System.out.println("id=" + id);
  42. System.out.println("product_name=" + product_name);
  43. System.out.println("product_price=" + product_price);
  44. System.out.println("product_picture=" + product_picture);
  45. System.out.println("product_catalog_name=" + product_catalog_name);
  46. // 高亮信息
  47. if (response.getHighlighting() != null) {
  48. if (response.getHighlighting().get(id) != null) {
  49. Map<String, List<String>> map = response.getHighlighting().get(id);// 取出高亮片段
  50. if (map.get("product_name") != null) {
  51. for (String s : map.get("product_name")) {
  52. System.out.println(s);
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }

转载于:https://www.cnblogs.com/wesly186/p/115607faa5f3f1bb68b4c61385c3c972.html

11.SolrJ索引操作相关推荐

  1. PyTorch 笔记(06)— Tensor 索引操作(index_select、masked_select、non_zero、gather)

    Tensor 支持与 numpy.ndarray 类似的索引操作,如无特殊说明,索引出来的结果与源 tensor 共享内存,即修改一个,另外一个也会跟着改变. In [65]: a = t.arang ...

  2. 【Pandas库】(6) 索引操作--改、查、高级索引

    各位同学好,今天我和大家分享一下Pandas库的索引操作中的修改值.查找值.高级索引. 首先,我们先定义两个变量ps1存放Series数据,pd1存放DataFrame数据,以便后续操作. impor ...

  3. 【Pandas库】(5) 索引操作--增、删

    各位同学好,今天我向大家介绍一下pandas库中的索引操作--增.删. 1. 增加 1.1 对series操作 方法一:在原有数据上增加,改变原有数据. Series名[新标签名] = 新标签名对应的 ...

  4. Elastricsearch 索引操作详解(快速入门、索引管理、映射详解、索引别名)

    一.快速入门 1. 查看集群的健康状况 http://localhost:9200/_cat http://localhost:9200/_cat/health?v 说明:v是用来要求在结果中返回表头 ...

  5. SQL Server死锁诊断--同一行数据在不同索引操作下引起的死锁

    死锁概述 对于数据库中出现的死锁,通俗地解释就是:不同Session(会话)持有一部分资源,并且同时相互排他性地申请对方持有的资源,然后双方都得不到自己想要的资源,从而造成的一种僵持的现象. 当然,在 ...

  6. Pandas索引操作及高级索引——reindex()方法

    文章目录 索引对象 多个数据结构之间共享index类对象 is与==的区别 重置索引--reindex() 索引操作 Series的索引操作 切片 不连续索引 布尔型索引 DataFrame的索引操作 ...

  7. Tensor:索引操作

    索引操作 Tensor支持与numpy.ndarray类似的索引操作,语法上也类似,下面通过一些例子,讲解常用的索引操作.如无特殊说明,索引出来的结果与原tensor共享内存,也即修改一个,另一个会跟 ...

  8. 【Pandas库】(4) 索引操作--重新生成索引

    各位同学好,今天和大家分享一下pandas库的索引操作--重新生成索引. 本文主要介绍如何重新生成Series类型和DataFrame类型的索引. (1)Series类型重新生成索引 方法: 变量 = ...

  9. Mongodb的索引操作

    Mongodb的索引操作 1. 为什么mongdb需要创建索引 加快查询速度 进行数据的去重 2. mongodb创建简单的索引方法 语法:db.集合名.ensureIndex({属性:1}),1表示 ...

最新文章

  1. linux内核SMP负载均衡浅析
  2. 实战:基于OpenCV实现偏斜文档校正
  3. GitHub代码一键转VS Code:只需+1s
  4. 2018年视频云服务市场格局进入整合阶段,阿里云视频云位居市场竞争力领导者的位置...
  5. iOS UILabel加载html点击图片查看大图 附demo
  6. LeetCode 443 String Compression(双指针)
  7. Picasso-源码解析(二)
  8. 指针数组和数组指针和函数指针
  9. 论文浅尝 | 对于知识图谱嵌入表示的几何形状理解
  10. Centos7上kvm虚拟化自定义NAT网络
  11. reports buileder 触发器的写法
  12. 苹果要换Type-C接口?丁磊建议统一充电器接口 工信部回复来了...
  13. 报错注入是什么?一看你就明白了。报错注入原理+步骤+实战案例
  14. SQL Serve权限管理
  15. ZOJ-3953 Intervals,t
  16. P问题、NP问题、NPC问题
  17. android中stagefright和OMXCodec原理分析
  18. 斯坦福计算机科学博士研究方向,美国人工智能专业Top10名校推荐
  19. 计算机主板上的模块安装和拆,更换计算机主板上的内存模块插槽需要多少费用?...
  20. python项目--O2O优惠券线下使用情况数据分析

热门文章

  1. SpringBoot时间戳与MySql数据库记录相差14小时排错
  2. Mac OS X 10.10.3对SSD开启Trim功能
  3. 如何在 Ubuntu 12.04 Server 中安装图形用户界面
  4. 交叉编译和交叉调试环境搭建及使用
  5. O_RDWR, O_CREAT等open函数标志位在哪里定义?
  6. awk按分隔符的不同取出不同的列
  7. 解决ant design vue中的modal弹框样式修改无效问题 修改modal样式无效
  8. [html] 写一个布局,当页面滚动一定高时,导航始终固定在顶部,反之恢复原位
  9. 工作373-前端 import与export区别
  10. [vue] 开发过程中有使用过devtools吗?