11.SolrJ索引操作

创建索引

说明:根据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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/417182.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

common lisp 学习第四天 变量、宏

//变量//引入变量&#xff1a;变量作用域括号内(let ((x 10) (y 20) z)...)引入变量列表中的变量(let* ((x 10) (y ( x 10))...)//全局变量之前有没有值都可以赋值(defparameter *count*变量名0值"doc描述")变量未定以才可以赋值&#xff0c;也可以不给定值(defvar *…

前端学习(2310):数据请求和json-server

app.js import React from react;import ./App.css; import Home from ./components/Home.js import World from "./components/World"; function App() {return (<div className"App">你好<World/></div>); }export default App;worl…

对Linux课程内容的建议,Linux课程笔记 Day01 课程内容总结(示例代码)

系统安装&#xff1a;引导项简单介绍&#xff1a;在“boot:”提示后&#xff1a;直接回车(Enter)——图形界面安装模式linux text——字符界面安装模式linux askmethod——提示用户选择安装方法(例如&#xff1a;nfs、ftp、http远程安装)linux rescue——救援模式&#xff0c;…

CommonJs、AMD、CMD模块化规范

/*** CommonJS 模块化规范* CommonJS规范加载模块是同步的&#xff0c;也就是说&#xff0c;只有加载完成&#xff0c;才能执行后面的操作*//*-------Node.js遵循Commonjs规范---------*///写法1.var exportsmodule.exports;exports.name"leyi";exports.fnfunction()…

前端学习(2311):react中处理跨域问题

proxy:{"/api":{target:"http://www.weather.com.cn/data/cityinfo/101320101.html",changeOrigin:true,"pathRewrite":{"^/api":"/"}}}

mysql显示行号

显示行号set intIndex 0;select (intIndex : intIndex 1) as RowNum ,* from table;

linux密码stdin怎么用,如何使ssh接收来自stdin的密码

根据这篇文章&#xff0c;您可以执行以下操作&#xff1a;创建一个使用SSH_ASKPASS打开ssh会话的命令(在man ssh上查找SSH_ASKPASS )$ cat > ssh_session <export SSH_ASKPASS"/path/to/script_returning_pass"setsid ssh "your_user""your_hos…

Dev Express Report 学习总结(五)在分组中使用聚集表达式AggregateExpression

聚集表达式AggregateExpression主要包括几种&#xff1a;Avg(),Count(),Exists(),Max(),Min(),Single()和Sum()。其中对于Sum()&#xff0c;在我看来主要有两种用法&#xff0c;一种是Group时的合计&#xff0c;另一种是整个页面某个列的值的合计。但是对于Count(),由于以前对D…

RavenDb中的Task异步应用.Net4

internal partial class RavenService : ServiceBase{ private RavenDbServer server; private Task startTask; public RavenService() { InitializeComponent(); } protected override void OnStart(string[] args) { //单独开启一个线程启动服务 startTask Task.Factory…

linux 线程间传送消息,Linux 多线程同步-消息队列

消息队列是消息的链表&#xff0c;存放在内核中并有消息队列标示符标示。msgget用于创建一个新队列或打开一个现存的队列。msgsnd将新消息加入到消息队列中&#xff1b;每个消息包括一个long型的type&#xff1b;和消息缓存&#xff1b;msgrcv用于从队列中取出消息&#xff1b;…

RTF文件格式

RTF1.8白皮书 http://www.microsoft.com/downloads/details.aspx?FamilyIDac57de32-17f0-4b46-9e4e-467ef9bc5540&displaylangen //标签 name为自定义标签名称{\*\bkmkstart name} //文本头部\rtf1RTF版本\ansi字符集\ansicpg936简体中文//字体表{\fonttbl{\f0字体0\fmode…

linux6.8安装图形桌面,图形/文本界面安装CentOS 6.8系统详解

2. anaconda的工作过程前面提到&#xff0c;使用anaconda安装CentOS系统有两种方式&#xff0c;默认使用的是图形界面(GUI)安装&#xff0c;要求主机内存至少有512MB内存。而使用基于文本配置接口(TUI)来安装CentOS则需要显示指定&#xff0c;指定方式可以是在菜单界面按ESC键&…

C#位图算法

在处理表格合并等问题时&#xff0c;可以考虑采用位图算法实现二维存储对象的处理通过Map.Set设置点值或Map.Fill填充区域。判断各点值时通过Map.Get获取扩展Point属性可以改成3维或更多维实现多维存储对象的处理/// <summary>/// 二维图形算法/// 王洪岐 121226/// <…

从零开始学JavaWeb

引言 记得上学时,有位导师说过一句很经典的话:"编程语言只是工具,最重要的是掌握思想。" 笔者一直主要从事.net领域的开发工作。随着工作阅历的丰富&#xff0c;越来越深刻的理解当年导师说的那句话的意义。 "他山之石,可以攻玉",相互借鉴,然后为我所用,无…

嵌入式linux截屏代码,嵌入式linux截屏程序

基于网上一个代码改的。源地址&#xff1a;http://blog.csdn.net/z1179675084/article/details/14645359// 注意&#xff0c;由于fb_bpp 16的情况没用到&#xff0c;以下修改后的代码只调整了fb_bpp不为16的情况#include #include #include #include #include #include #inclu…

MongoDB分布式(分片存储)部署

分别开启一个Config和两个ShardD:\mongodb1\bin\mongod --configsvr --dbpath D:\mongodb1\db\ --port 27020D:\mongodb2\bin\mongod --dbpath D:\mongodb2\db\ --port 27021D:\mongodb3\bin\mongod --dbpath D:\mongodb3\db\ --port 27022启动mongos(Sharding controller分片控…

(转)找回vss超级管理员密码

原文&#xff1a;http://www.cnblogs.com/446557021/archive/2011/01/05/1926213.html 如果忘记了VSS管理员密码&#xff0c;打开vss数据库所在的文件夹&#xff0c;打开data目录&#xff0c;找到um.dat文件&#xff0c;用编辑器打开编辑它&#xff0c;从offset 80的55 55 开始…

MongoDB文件操作(支持大于4M数据)

// MongoDB连接串&#xff0c;以[mongodb://]开头。这里&#xff0c;我们连接的是本机的服务string connectionString "mongodb://localhost";// 连接到一个MongoServer上MongoServer server MongoServer.Create(connectionString);// 打开数据库testdbMongoDataba…

linux 压缩排除某个文件夹,linux tar压缩排除 某类型文件 某个文件夹

排除某类型文件测试 生成10个.log文件再生成10个.txt文件touch {1..10}.logtouch {1..10}.txt打包并排除 log结尾的文件tar -zcvf 1.tar –exclude*.log . 结论tar -tvf 1.tar rwxr-xr-x root/root 0 2018-10-26 02:45 ./-rw-r–r– root/root 0 2018-1…