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. }
posted @ 2017-03-10 21:37  Wesly186  阅读(167)  评论(0编辑  收藏  举报