Solr的常用操作

来源:互联网 发布:eclipse 测试java程序 编辑:程序博客网 时间:2024/06/03 18:16
  1. import java.io.IOException;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.apache.solr.client.solrj.SolrQuery;
  5. import org.apache.solr.client.solrj.SolrServerException;
  6. import org.apache.solr.client.solrj.impl.HttpSolrServer;
  7. import org.apache.solr.client.solrj.response.QueryResponse;
  8. import org.apache.solr.common.SolrInputDocument;
  9. import org.junit.Test;
  10. import cn.itcast.solr.domain.Article;
  11. public class SolrjTest {
  12. /**
  13. * 根据SolrInputDocument添加数据或者更新数据
  14. * @throws SolrServerException
  15. * @throws IOException
  16. */
  17. @Test
  18. public void testCreateOrUpdateBySolrInputDocument() throws SolrServerException, IOException{
  19. // 连接服务
  20. String baseURL = "http://localhost:8080/solr/article/";
  21. HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
  22. // 创建SolrInputDocument
  23. SolrInputDocument solrInputDocument = new SolrInputDocument();
  24. solrInputDocument.addField("id", 1106327947);
  25. solrInputDocument.addField("title", "macbook");
  26. solrInputDocument.addField("content", "256G");
  27. // 添加数据
  28. httpSolrServer.add(solrInputDocument);
  29. httpSolrServer.commit();
  30. }
  31. /**
  32. * 通过注解添加或更新数据
  33. * @throws SolrServerException
  34. * @throws IOException
  35. */
  36. @Test
  37. public void testCreateOrUpdateByBean() throws SolrServerException, IOException{
  38. // 连接服务
  39. String baseURL = "http://localhost:8080/solr/article/";
  40. HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
  41. // Article自己写的一个对象
  42. Article article = new Article();
  43. article.setId("1106327948");
  44. article.setTitle("oppoe");
  45. article.setContent("充电五分钟通话两小时");
  46. // 添加数据
  47. httpSolrServer.addBean(article);
  48. httpSolrServer.commit();
  49. }
  50. /**
  51. * 根据SolrQuery查询
  52. * @throws SolrServerException
  53. * @throws IOException
  54. */
  55. @Test
  56. public void testQueryBySolrQuery() throws SolrServerException, IOException{
  57. // 连接服务
  58. String baseURL = "http://localhost:8080/solr/article/";
  59. HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
  60. //SolrParams params = new SolrQuery("三星"); //这样写也ok
  61. //SolrQuery params = new SolrQuery("三星"); // SolrQuery可设置其他条件,这是默认字段的值
  62. SolrQuery params =new SolrQuery();
  63. params.set(("q", "id:1106327948"); // 设置制定字段的值
  64. params.set("fl", "title");
  65. QueryResponse queryResponse = httpSolrServer.query(params);
  66. List<Article> articles = queryResponse.getBeans(Article.class);
  67. System.out.println(articles);
  68. }
  69. /**
  70. * 对结果高亮显示
  71. * @throws SolrServerException
  72. * @throws IOException
  73. */
  74. @Test
  75. public void testQueryByHL() throws SolrServerException, IOException{
  76. // 连接服务
  77. String baseURL = "http://localhost:8080/solr/article/";
  78. HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
  79. //SolrParams params = new SolrQuery("三星");
  80. SolrQuery params = new SolrQuery("三星"); // SolrQuery可设置其他条件
  81. params.addHighlightField("title");
  82. params.setHighlight(true); // 开启高亮
  83. params.setHighlightSimplePre("<em>");
  84. params.setHighlightSimplePost("</em>");
  85. QueryResponse queryResponse = httpSolrServer.query(params);
  86. Map<String, Map<String, List<String>>> map = queryResponse.getHighlighting();
  87. System.out.println(map);
  88. }
  89. /**
  90. * 根据id删除数据
  91. * @throws SolrServerException
  92. * @throws IOException
  93. */
  94. @Test
  95. public void testDeleteById() throws SolrServerException, IOException{
  96. // 连接服务
  97. String baseURL = "http://localhost:8080/solr/article/";
  98. HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
  99. httpSolrServer.deleteById("1105271766");
  100. httpSolrServer.commit();
  101. }
  102. /**
  103. * 先查后删
  104. * @throws SolrServerException
  105. * @throws IOException
  106. */
  107. @Test
  108. public void testDeleteByQuery() throws SolrServerException, IOException{
  109. // 连接服务
  110. String baseURL = "http://localhost:8080/solr/article/";
  111. HttpSolrServer httpSolrServer = new HttpSolrServer(baseURL); // solr服务的地址
  112. httpSolrServer.deleteByQuery("title:全高清");
  113. httpSolrServer.commit();
  114. }
  115. }
0 0