Solrj实现增删改查

淡淡的烟草味﹌ 2022-05-27 04:27 393阅读 0赞

一、准备

solrhome/collection1/conf/schema.xml 配置文件中定义相关的字段。

  1. <!-- name:该字段的名称。 type:fieldType 字段对应的名称。 "text_ik" 是我自定义的一个字段类型,并配置了中文分词器。 -->
  2. <field name="product_name" type="text_ik" indexed="true" stored="true"/>
  3. <field name="product_price" type="float" indexed="true" stored="true"/>
  4. <field name="product_description" type="text_ik" indexed="true" stored="false" />
  5. <field name="product_catalog_name" type="string" indexed="true" stored="true" />
  6. <!-- 当从 "product_keywords" 字段搜索时,会搜索 "product_name" 和 "product_description"字段。 -->
  7. <field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
  8. <copyField source="product_name" dest="product_keywords"/>
  9. <copyField source="product_description" dest="product_keywords"/>

这里通过新建 Maven 项目完成测试,在pom.xml中添加对应的依赖:

  1. <!-- 这里使用 7.2.1 的版本 -->
  2. <dependency>
  3. <groupId>org.apache.solr</groupId>
  4. <artifactId>solr-solrj</artifactId>
  5. <version>7.2.1</version>
  6. </dependency>
  7. <!-- 添加 Junit 测试的依赖 -->
  8. <dependency>
  9. <groupId>junit</groupId>
  10. <artifactId>junit</artifactId>
  11. <version>4.11</version>
  12. <scope>test</scope>
  13. </dependency>

二、测试增删改

增删改相对来说比较简单,这里放在一起进行测试,Solr 中没有提供专门的修改方法,会根据 id 在文档中查找,如果没有找到就是添加,找到了就会覆盖原来的数据,即修改。

2.1新增与修改

编写 Java 代码:

  1. @Test
  2. public void indexCreateTest() throws Exception {
  3. // 创建和Solr 服务端的连接,并指定 Solr 实例是 "collection1"
  4. SolrClient client = new HttpSolrClient
  5. .Builder("http://192.168.248.136:8080/solr/collection1").build();
  6. // 创建 Solr 文档对象
  7. SolrInputDocument document = new SolrInputDocument();
  8. // 向文档对象中添加指定字段对应的字段值,字段必须先定义后使用,且必须要有 id 字段
  9. // 如果是修改,只需要固定 id 字段的值,修改其他的字段即可
  10. document.addField("id", "a1");
  11. document.addField("product_catalog_name", "幽默杂货");
  12. document.addField("product_price", "20");
  13. document.addField("product_name", "小王子");
  14. // 将文档添加到 client 对象中
  15. client.add(document);
  16. // 提交
  17. client.commit();
  18. }

在浏览器端进行验证:
70

2.2删除

编写 Java 代码:

  1. @Test
  2. public void indexDelTest() throws Exception {
  3. SolrClient client = new HttpSolrClient
  4. .Builder("http://192.168.248.136:8080/solr/collection1").build();
  5. // 根据 id 删除
  6. // client.deleteById("a1");
  7. // 根据查询删除, *:* 表示删除所有
  8. client.deleteByQuery("product_name:小王子");
  9. client.commit();
  10. }

这里就不在浏览器验证了。

三、测试查询

相对于增删改操作,往往需要我们做的是查询操作。查询操作也相对比较复杂,有关查询的设置,可以在浏览器端查看,只要在代码中设置即可。

查询高亮的结果比较麻烦,这里将浏览器端的查询结果贴出来,希望能帮助大家更好理解高亮数据的获取方式。需要高亮显示的字段是:product_name,查询关键字是:小王子
这里写图片描述

编写 Java 代码:

  1. @Test
  2. public void indexSearchTest() throws Exception {
  3. SolrClient client = new HttpSolrClient
  4. .Builder("http://192.168.248.136:8080/solr/collection1").build();
  5. // 创建查询对象
  6. SolrQuery query = new SolrQuery();
  7. // 设置默认搜索字段,如果不指定搜索的字段,则从默认字段中搜索
  8. query.set("df", "product_keywords");
  9. // 指定搜索域与搜索的关键字
  10. query.setQuery("product_name:小王子");
  11. // 如果不定搜索域,则从默认搜索域中搜索,如下
  12. //query.setQuery("手机");
  13. // 设置搜索的过滤器,只搜索"product_price" 在 10-20 之间的
  14. query.addFilterQuery("product_price:[15 TO 30]");
  15. // 设置起始的条数,默认是 0
  16. query.setStart(0);
  17. // 设置查询的条数,默认是 10
  18. query.setRows(5);
  19. // 设置高亮
  20. query.setHighlight(true);
  21. // 设置显示高亮的字段
  22. query.addHighlightField("product_name");
  23. // 设置高亮字段值的前缀
  24. query.setHighlightSimplePre("<span style=\"color:red\">");
  25. // 设置高亮字段值的后缀
  26. query.setHighlightSimplePost("</span>");
  27. // 获得查询结果的响应对象
  28. QueryResponse response = client.query(query);
  29. // 从响应对象中获得结果集对象
  30. SolrDocumentList list = response.getResults();
  31. System.out.println("查询到的总记录数:" + list.getNumFound());
  32. // 遍历结果集
  33. for (SolrDocument document : list){
  34. System.out.println("product_price : " + document.get("product_price"));
  35. System.out.println("product_name : " + document.get("product_name"));
  36. // 从响应对象中获得高亮,并处理
  37. Map<String, Map<String, List<String>>> map = response.getHighlighting();
  38. List<String> lightList = map.get(document.get("id")).get("product_name");
  39. if(lightList != null && lightList.size() > 0) {
  40. System.out.println("high lighting product_name : " + lightList.get(0));
  41. }
  42. System.out.println("==================================");
  43. }
  44. }

查询结果输出:
这里写图片描述

(完)

发表评论

表情:
评论列表 (有 0 条评论,393人围观)

还没有评论,来说两句吧...

相关阅读