pom

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-data-jpa</artifactId>
  8. </dependency>
  9. <!--test-->
  10. <dependency>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-test</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-data-solr</artifactId>
  17. </dependency>

代码:

  1. package com.example.solrdemo.controller;
  2. import com.example.solrdemo.pojo.TbItem;
  3. import com.example.solrdemo.pojo.TbItemDesc;
  4. import com.example.solrdemo.repository.TbItemDescRepository;
  5. import com.example.solrdemo.repository.TbItemRepository;
  6. import org.apache.solr.client.solrj.SolrClient;
  7. import org.apache.solr.client.solrj.SolrQuery;
  8. import org.apache.solr.client.solrj.SolrServerException;
  9. import org.apache.solr.client.solrj.response.QueryResponse;
  10. import org.apache.solr.common.SolrDocument;
  11. import org.apache.solr.common.SolrDocumentList;
  12. import org.apache.solr.common.SolrInputDocument;
  13. import org.junit.Test;
  14. import org.junit.runner.RunWith;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.boot.test.context.SpringBootTest;
  17. import org.springframework.test.context.junit4.SpringRunner;
  18. import org.springframework.web.bind.annotation.RequestMapping;
  19. import java.io.IOException;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.UUID;
  23. @SpringBootTest
  24. @RunWith(SpringRunner.class)
  25. public class SolrControllerTest {
  26. @Autowired
  27. private SolrClient client;
  28. @Autowired
  29. TbItemRepository repository;
  30. @Autowired
  31. TbItemDescRepository tbItemDescRepository;
  32. //录入数据
  33. @Test
  34. public void luru() throws IOException, SolrServerException {
  35. List<TbItem> list = repository.findAll();
  36. for (TbItem item:list){
  37. SolrInputDocument doc = new SolrInputDocument();
  38. doc.setField("id", item.getId());
  39. doc.setField("item_title", item.getTitle());
  40. doc.setField("item_sell_point", item.getSellPoint());
  41. doc.setField("item_price", item.getPrice());
  42. doc.setField("item_image", item.getImage());
  43. doc.setField("item_category", item.getCid());
  44. TbItemDesc des = tbItemDescRepository.findById(item.getId()).get();
  45. if (des!=null&&des.getItemDesc()!=null){
  46. doc.setField("item_desc",des.getItemDesc() );
  47. }
  48. client.add("collection1", doc);
  49. client.commit("collection1");
  50. System.out.println(item);
  51. }
  52. }
  53. //增加
  54. @Test
  55. public void teAdd() {
  56. try {
  57. SolrInputDocument doc = new SolrInputDocument();
  58. doc.setField("id", "test001");
  59. doc.setField("item_title", "测试商品");
  60. doc.setField("item_price", "123213");
  61. client.add("collection1", doc);
  62. //client.commit();
  63. client.commit("collection1");
  64. } catch (Exception e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. //查询
  69. @Test
  70. public void getById() throws IOException, SolrServerException {
  71. SolrDocument document = client.getById("collection1", "test001");
  72. System.out.println(document);
  73. }
  74. @Test
  75. public void slelectParm() throws IOException, SolrServerException {
  76. SolrQuery params = new SolrQuery();
  77. //查询条件, 这里的 q 对应 下面图片标红的地方
  78. params.set("q", "魔音"); //匹配title含有手机的
  79. //过滤条件
  80. //params.set("fq", "item_price:[100 TO 100000]");
  81. //排序
  82. // params.addSort("item_price", SolrQuery.ORDER.asc);
  83. //分页
  84. params.setStart(0);
  85. params.setRows(20);
  86. //默认域
  87. //params.set("df", "item_title");//搜索匹配的对象,值匹配item_title存在的关键字
  88. params.set("df", "item_keywords");//使用复制域做搜索对象,可以匹配多个对象
  89. //只查询指定域
  90. params.set("fl", "id,item_title,item_price,item_sell_point,item_desc");//搜索返回的数据项
  91. //高亮
  92. //打开开关
  93. params.setHighlight(true);
  94. //指定高亮域
  95. params.addHighlightField("item_title");
  96. //设置前缀
  97. params.setHighlightSimplePre("<span style='color:red'>");
  98. //设置后缀
  99. params.setHighlightSimplePost("</span>");
  100. QueryResponse queryResponse = client.query(params);
  101. SolrDocumentList results = queryResponse.getResults();
  102. long numFound = results.getNumFound();
  103. System.out.println(numFound);
  104. Map<String, Map<String, List<String>>> highlight = queryResponse.getHighlighting();//获取高亮显示的结果, 高亮显示的结果和查询结果是分开放的
  105. for (SolrDocument result : results) {
  106. System.out.println(result.get("id"));
  107. System.out.println(result.get("item_title"));
  108. System.out.println(result.get("item_price"));
  109. Map<String, List<String>> map = highlight.get(result.get("id"));
  110. List<String> list = map.get("item_title");
  111. System.out.println(list.get(0));
  112. System.out.println("------------------");
  113. System.out.println();
  114. }
  115. }
  116. /* /**
  117. * 根据id删除索引
  118. * @param id
  119. * @return
  120. */
  121. public String delete(String id) {
  122. try {
  123. client.deleteById("collection1",id);
  124. client.commit("collection1");
  125. return id;
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. }
  129. return "error";
  130. }
  131. /**
  132. * 删除所有的索引
  133. * @return
  134. */
  135. public String deleteAll(){
  136. try {
  137. client.deleteByQuery("collection1","*:*");
  138. client.commit("collection1");
  139. return "success";
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. }
  143. return "error";
  144. }
  145. }

sorl的域配置:

  1. <field name="item_title" type="text_ik" indexed="true" stored="true"/>
  2. <field name="item_sell_point" type="text_ik" indexed="true" stored="true"/>
  3. <field name="item_price" type="text_ik" indexed="true" stored="true"/>
  4. <field name="item_image" type="text_ik" indexed="true" stored="true"/>
  5. <field name="item_category" type="text_ik" indexed="true" stored="true"/>
  6. <field name="item_desc" type="text_ik" indexed="true" stored="false"/>
  7. <field name="item_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>
  8. <copyField source="item_title" dest="item_keywords"/>
  9. <copyField source="item_sell_point" dest="item_keywords"/>
  10. <copyField source="item_category" dest="item_keywords"/>
  11. <copyField source="item_desc" dest="item_keywords"/>