当前位置:   article > 正文

es使用java来查询文档(基于es7.8)_java查询es文档

java查询es文档

注意,索引的字段mapping是如下类型,如果index是FALSE的话,会导致条件查询失败:

先引入pom依赖:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.elasticsearch</groupId>
  4. <artifactId>elasticsearch</artifactId>
  5. <version>7.8.0</version>
  6. </dependency>
  7. <!-- elasticsearch的客户端 -->
  8. <dependency>
  9. <groupId>org.elasticsearch.client</groupId>
  10. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  11. <version>7.8.0</version>
  12. </dependency>
  13. <!-- elasticsearch依赖2.x的log4j -->
  14. <dependency>
  15. <groupId>org.apache.logging.log4j</groupId>
  16. <artifactId>log4j-api</artifactId>
  17. <version>2.8.2</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.apache.logging.log4j</groupId>
  21. <artifactId>log4j-core</artifactId>
  22. <version>2.8.2</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>com.fasterxml.jackson.core</groupId>
  26. <artifactId>jackson-databind</artifactId>
  27. <version>2.9.9</version>
  28. </dependency>
  29. <!-- junit单元测试 -->
  30. <dependency>
  31. <groupId>junit</groupId>
  32. <artifactId>junit</artifactId>
  33. <version>4.12</version>
  34. </dependency>
  35. </dependencies>

创建User类:

  1. public class User {
  2. private String name;
  3. private String sex;
  4. private Integer age;
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public String getSex() {
  12. return sex;
  13. }
  14. public void setSex(String sex) {
  15. this.sex = sex;
  16. }
  17. public Integer getAge() {
  18. return age;
  19. }
  20. public void setAge(Integer age) {
  21. this.age = age;
  22. }
  23. }

然后在名为user的索引里,插入多条数据,在main方法里进行测试:

  1. import org.apache.http.HttpHost;
  2. import org.elasticsearch.action.bulk.BulkRequest;
  3. import org.elasticsearch.action.bulk.BulkResponse;
  4. import org.elasticsearch.action.index.IndexRequest;
  5. import org.elasticsearch.client.RequestOptions;
  6. import org.elasticsearch.client.RestClient;
  7. import org.elasticsearch.client.RestHighLevelClient;
  8. import org.elasticsearch.common.xcontent.XContentType;
  9. public class ESTest_Doc_Insert_Batch {
  10. public static void main(String[] args) throws Exception {
  11. RestHighLevelClient esClient = new RestHighLevelClient(
  12. RestClient.builder(new HttpHost("localhost", 9200, "http"))
  13. );
  14. // 批量插入数据
  15. BulkRequest request = new BulkRequest();
  16. request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan", "age",30,"sex","男"));
  17. request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi", "age",30,"sex","女"));
  18. request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu", "age",40,"sex","男"));
  19. request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "wangwu1", "age",40,"sex","女"));
  20. request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "wangwu2", "age",50,"sex","男"));
  21. request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "wangwu3", "age",50,"sex","男"));
  22. request.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON, "name", "wangwu44", "age",60,"sex","男"));
  23. request.add(new IndexRequest().index("user").id("1008").source(XContentType.JSON, "name", "wangwu555", "age",60,"sex","男"));
  24. request.add(new IndexRequest().index("user").id("1009").source(XContentType.JSON, "name", "wangwu66666", "age",60,"sex","男"));
  25. BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
  26. System.out.println("====>"+response.getTook());
  27. System.out.println("====>"+response.getItems());
  28. esClient.close();
  29. }
  30. }

然后进行测试:

  1. import org.apache.http.HttpHost;
  2. import org.elasticsearch.action.search.SearchRequest;
  3. import org.elasticsearch.action.search.SearchResponse;
  4. import org.elasticsearch.client.RequestOptions;
  5. import org.elasticsearch.client.RestClient;
  6. import org.elasticsearch.client.RestHighLevelClient;
  7. import org.elasticsearch.common.unit.Fuzziness;
  8. import org.elasticsearch.index.query.QueryBuilders;
  9. import org.elasticsearch.search.SearchHit;
  10. import org.elasticsearch.search.SearchHits;
  11. import org.elasticsearch.search.builder.SearchSourceBuilder;
  12. public class ESTest_Doc_Query {
  13. public static void main(String[] args) throws Exception {
  14. RestHighLevelClient esClient = new RestHighLevelClient(
  15. RestClient.builder(new HttpHost("localhost", 9200, "http"))
  16. );
  17. // 1. 查询索引中全部的数据
  18. // SearchRequest request = new SearchRequest();
  19. // request.indices("user");
  20. //
  21. // request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
  22. //
  23. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  24. //
  25. // SearchHits hits = response.getHits();
  26. //
  27. // System.out.println(hits.getTotalHits());
  28. // System.out.println(response.getTook());
  29. //
  30. // for ( SearchHit hit : hits ) {
  31. // System.out.println(hit.getSourceAsString());
  32. // }
  33. // 2. 条件查询 : termQuery
  34. // SearchRequest request = new SearchRequest();
  35. // request.indices("user");
  36. //
  37. // request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age", 40)));
  38. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  39. //
  40. // SearchHits hits = response.getHits();
  41. //
  42. // System.out.println(hits.getTotalHits());
  43. // System.out.println(response.getTook());
  44. //
  45. // for ( SearchHit hit : hits ) {
  46. // System.out.println(hit.getSourceAsString());
  47. // }
  48. // 3. 分页查询
  49. // SearchRequest request = new SearchRequest();
  50. // request.indices("user");
  51. //
  52. // SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
  53. // // (当前页码-1)*每页显示数据条数
  54. // builder.from(2);
  55. // builder.size(2);
  56. // request.source(builder);
  57. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  58. //
  59. // SearchHits hits = response.getHits();
  60. //
  61. // System.out.println(hits.getTotalHits());
  62. // System.out.println(response.getTook());
  63. //
  64. // for ( SearchHit hit : hits ) {
  65. // System.out.println(hit.getSourceAsString());
  66. // }
  67. // // 4. 查询排序
  68. // SearchRequest request = new SearchRequest();
  69. // request.indices("user");
  70. //
  71. // SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
  72. // //
  73. // builder.sort("age", SortOrder.DESC);
  74. //
  75. // request.source(builder);
  76. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  77. //
  78. // SearchHits hits = response.getHits();
  79. //
  80. // System.out.println(hits.getTotalHits());
  81. // System.out.println(response.getTook());
  82. //
  83. // for ( SearchHit hit : hits ) {
  84. // System.out.println(hit.getSourceAsString());
  85. // }
  86. // // 5. 过滤字段
  87. // SearchRequest request = new SearchRequest();
  88. // request.indices("user");
  89. //
  90. // SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
  91. // //
  92. // String[] excludes = {"age"};
  93. // String[] includes = {};
  94. // builder.fetchSource(includes, excludes);
  95. //
  96. // request.source(builder);
  97. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  98. //
  99. // SearchHits hits = response.getHits();
  100. //
  101. // System.out.println(hits.getTotalHits());
  102. // System.out.println(response.getTook());
  103. //
  104. // for ( SearchHit hit : hits ) {
  105. // System.out.println(hit.getSourceAsString());
  106. // }
  107. // // 6. 组合查询
  108. // SearchRequest request = new SearchRequest();
  109. // request.indices("user");
  110. //
  111. // SearchSourceBuilder builder = new SearchSourceBuilder();
  112. // BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  113. //
  114. // //boolQueryBuilder.must(QueryBuilders.matchQuery("age", 30));
  115. // //boolQueryBuilder.must(QueryBuilders.matchQuery("sex", "男"));
  116. // //boolQueryBuilder.mustNot(QueryBuilders.matchQuery("sex", "男"));
  117. // boolQueryBuilder.should(QueryBuilders.matchQuery("age", 30));
  118. // boolQueryBuilder.should(QueryBuilders.matchQuery("age", 40));
  119. //
  120. // builder.query(boolQueryBuilder);
  121. //
  122. // request.source(builder);
  123. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  124. //
  125. // SearchHits hits = response.getHits();
  126. //
  127. // System.out.println(hits.getTotalHits());
  128. // System.out.println(response.getTook());
  129. //
  130. // for ( SearchHit hit : hits ) {
  131. // System.out.println(hit.getSourceAsString());
  132. // }
  133. // // 7. 范围查询
  134. // SearchRequest request = new SearchRequest();
  135. // request.indices("user");
  136. //
  137. // SearchSourceBuilder builder = new SearchSourceBuilder();
  138. // RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
  139. //
  140. // rangeQuery.gte(30);
  141. // rangeQuery.lt(50);
  142. //
  143. // builder.query(rangeQuery);
  144. //
  145. // request.source(builder);
  146. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  147. //
  148. // SearchHits hits = response.getHits();
  149. //
  150. // System.out.println(hits.getTotalHits());
  151. // System.out.println(response.getTook());
  152. //
  153. // for ( SearchHit hit : hits ) {
  154. // System.out.println(hit.getSourceAsString());
  155. // }
  156. // 8. 模糊查询
  157. SearchRequest request = new SearchRequest();
  158. request.indices("user");
  159. SearchSourceBuilder builder = new SearchSourceBuilder();
  160. //这里的Fuzziness.TWO是查询结果的字符串长度偏差,可以设置成Fuzziness.ONE看一下效果
  161. builder.query(QueryBuilders.fuzzyQuery("name", "wangwu").fuzziness(Fuzziness.TWO));
  162. request.source(builder);
  163. SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  164. SearchHits hits = response.getHits();
  165. System.out.println(hits.getTotalHits());
  166. System.out.println(response.getTook());
  167. for ( SearchHit hit : hits ) {
  168. System.out.println(hit.getSourceAsString());
  169. }
  170. // // 9. 高亮查询
  171. // SearchRequest request = new SearchRequest();
  172. // request.indices("user");
  173. //
  174. // SearchSourceBuilder builder = new SearchSourceBuilder();
  175. // TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "zhangsan");
  176. //
  177. // HighlightBuilder highlightBuilder = new HighlightBuilder();
  178. // highlightBuilder.preTags("<font color='red'>");
  179. // highlightBuilder.postTags("</font>");
  180. // highlightBuilder.field("name");
  181. //
  182. // builder.highlighter(highlightBuilder);
  183. // builder.query(termsQueryBuilder);
  184. //
  185. // request.source(builder);
  186. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  187. //
  188. // SearchHits hits = response.getHits();
  189. //
  190. // System.out.println(hits.getTotalHits());
  191. // System.out.println(response.getTook());
  192. //
  193. // for ( SearchHit hit : hits ) {
  194. // System.out.println(hit.getSourceAsString());
  195. // }
  196. // // 10. 聚合查询
  197. // SearchRequest request = new SearchRequest();
  198. // request.indices("user");
  199. //
  200. // SearchSourceBuilder builder = new SearchSourceBuilder();
  201. //
  202. // AggregationBuilder aggregationBuilder = AggregationBuilders.max("maxAge").field("age");
  203. // builder.aggregation(aggregationBuilder);
  204. //
  205. // request.source(builder);
  206. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  207. //
  208. // SearchHits hits = response.getHits();
  209. //
  210. // System.out.println(hits.getTotalHits());
  211. // System.out.println(response.getTook());
  212. //
  213. // for ( SearchHit hit : hits ) {
  214. // System.out.println(hit.getSourceAsString());
  215. // }
  216. // 11. 分组查询
  217. // SearchRequest request = new SearchRequest();
  218. // request.indices("user");
  219. //
  220. // SearchSourceBuilder builder = new SearchSourceBuilder();
  221. //
  222. // AggregationBuilder aggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");
  223. // builder.aggregation(aggregationBuilder);
  224. //
  225. // request.source(builder);
  226. // SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
  227. //
  228. // SearchHits hits = response.getHits();
  229. //
  230. // System.out.println(hits.getTotalHits());
  231. // System.out.println(response.getTook());
  232. //
  233. // for ( SearchHit hit : hits ) {
  234. // System.out.println(hit.getSourceAsString());
  235. // }
  236. esClient.close();
  237. }
  238. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/68311
推荐阅读
相关标签
  

闽ICP备14008679号