JavaSpringBootElasticsearch
这里使用的SpringBoot版本号是2.1.0.RELEASE,服务端 es 的版本号是6.8.2,客户端采用的是官方推荐的Elastic Java High Level Rest Client版本号是6.4.2,方便与SpringBoot的版本兼容。
es 最大的亮点就是查询非常丰富,可以在上亿的数据里面快速搜索出目标数据,查询如果实现呢?请看下面的内容

单条件精确查询

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 单条件精确查询
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search0() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .query(QueryBuilders.termsQuery("name", "赵里"));
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

多条件精确查询,取并集

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 多条件精确查询,取并集
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search1() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .query(QueryBuilders.termsQuery("name", "张", "陈"));
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

范围查询

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 范围查询,包括from、to
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search2() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .query(QueryBuilders.rangeQuery("age").from(20).to(32));
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. /**
  26. * 范围查询,不包括from、to
  27. * @throws IOException
  28. */
  29. @Test
  30. public void search3() throws IOException {
  31. // 创建请求
  32. SearchSourceBuilder builder = new SearchSourceBuilder()
  33. .query(QueryBuilders.rangeQuery("age").from(20,false).to(30, false));
  34. //搜索
  35. SearchRequest searchRequest = new SearchRequest();
  36. searchRequest.indices("cs_index");
  37. searchRequest.types("_doc");
  38. searchRequest.source(builder);
  39. // 执行请求
  40. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  41. // 解析查询结果
  42. System.out.println(response.toString());
  43. }
  44. /**
  45. * 范围查询, lt:小于,gt:大于
  46. * @throws IOException
  47. */
  48. @Test
  49. public void search4() throws IOException {
  50. // 创建请求
  51. SearchSourceBuilder builder = new SearchSourceBuilder()
  52. .query(QueryBuilders.rangeQuery("age").lt(30).gt(20));
  53. //搜索
  54. SearchRequest searchRequest = new SearchRequest();
  55. searchRequest.indices("cs_index");
  56. searchRequest.types("_doc");
  57. searchRequest.source(builder);
  58. // 执行请求
  59. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  60. // 解析查询结果
  61. System.out.println(response.toString());
  62. }
  63. }

模糊查询,支持通配符

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 模糊查询,支持通配符
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search5() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .query(QueryBuilders.wildcardQuery("name","张三"));
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

不使用通配符的模糊查询,左右匹配

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 不使用通配符的模糊查询,左右匹配
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search6() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .query(QueryBuilders.queryStringQuery("张三").field("name"));
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

多字段模糊查询

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 多字段模糊查询
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search7() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .query(QueryBuilders.multiMatchQuery("长", "name", "city"));
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

多字段模糊查询

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 分页搜索
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search8() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .from(0).size(2);
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

字段排序

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 排序,字段的类型必须是:integer、double、long或者keyword
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search9() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .sort("createTime", SortOrder.ASC);
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

精确统计筛选文档数

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 精确统计筛选文档数,查询性能有所降低
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search10() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .trackTotalHits(true);
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

设置源字段过滤返回

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search11() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .fetchSource(new String[]{"name","age","city","createTime"},new String[]{});
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

根据 id 精确匹配

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 根据id精确匹配
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search12() throws IOException {
  12. String[] ids = new String[]{"1","2"};
  13. // 创建请求
  14. SearchSourceBuilder builder = new SearchSourceBuilder()
  15. .query(QueryBuilders.termsQuery("_id", ids));
  16. //搜索
  17. SearchRequest searchRequest = new SearchRequest();
  18. searchRequest.indices("cs_index");
  19. searchRequest.types("_doc");
  20. searchRequest.source(builder);
  21. // 执行请求
  22. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  23. // 解析查询结果
  24. System.out.println(response.toString());
  25. }
  26. }

matchAllQuery 搜索全部

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * matchAllQuery搜索全部
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search21() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .query(QueryBuilders.matchAllQuery());
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

match 搜索匹配

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * match搜索匹配
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search22() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder()
  14. .query(QueryBuilders.matchQuery("name", "张王"));
  15. //搜索
  16. SearchRequest searchRequest = new SearchRequest();
  17. searchRequest.indices("cs_index");
  18. searchRequest.types("_doc");
  19. searchRequest.source(builder);
  20. // 执行请求
  21. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  22. // 解析查询结果
  23. System.out.println(response.toString());
  24. }
  25. }

bool组合查询

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * bool组合查询
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search23() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder();
  14. BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
  15. boolQueryBuilder.must(QueryBuilders.matchQuery("name", "张王"));
  16. boolQueryBuilder.must(QueryBuilders.rangeQuery("age").lte(30).gte(20));
  17. builder.query(boolQueryBuilder);
  18. //搜索
  19. SearchRequest searchRequest = new SearchRequest();
  20. searchRequest.indices("cs_index");
  21. searchRequest.types("_doc");
  22. searchRequest.source(builder);
  23. // 执行请求
  24. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  25. // 解析查询结果
  26. System.out.println(response.toString());
  27. }
  28. }

nested 类型嵌套查询

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * nested类型嵌套查询
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search24() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder();
  14. //条件查询
  15. BoolQueryBuilder mainBool=new BoolQueryBuilder();
  16. mainBool.must(QueryBuilders.matchQuery("name", "赵六"));
  17. //nested类型嵌套查询
  18. BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
  19. boolQueryBuilder.must(QueryBuilders.matchQuery("products.brand", "A"));
  20. boolQueryBuilder.must(QueryBuilders.matchQuery("products.title", "巧克力"));
  21. NestedQueryBuilder nested = QueryBuilders.nestedQuery("products",boolQueryBuilder, ScoreMode.None);
  22. mainBool.must(nested);
  23. builder.query(mainBool);
  24. //搜索
  25. SearchRequest searchRequest = new SearchRequest();
  26. searchRequest.indices("cs_index");
  27. searchRequest.types("_doc");
  28. searchRequest.source(builder);
  29. // 执行请求
  30. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  31. // 解析查询结果
  32. System.out.println(response.toString());
  33. }
  34. }

多条件查询 + 排序 + 分页

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 多条件查询 + 排序 + 分页
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search29() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder();
  14. //条件搜索
  15. BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
  16. boolQueryBuilder.must(QueryBuilders.matchQuery("name", "张王"));
  17. boolQueryBuilder.must(QueryBuilders.rangeQuery("age").lte(30).gte(20));
  18. builder.query(boolQueryBuilder);
  19. //结果集合分页
  20. builder.from(0).size(2);
  21. //排序
  22. builder.sort("createTime",SortOrder.ASC);
  23. //搜索
  24. SearchRequest searchRequest = new SearchRequest();
  25. searchRequest.indices("cs_index");
  26. searchRequest.types("_doc");
  27. searchRequest.source(builder);
  28. // 执行请求
  29. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  30. // 解析查询结果
  31. System.out.println(response.toString());
  32. }
  33. }

聚合查询-求和

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 聚合查询 sum
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search30() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder();
  14. //条件搜索
  15. builder.query(QueryBuilders.matchAllQuery());
  16. //聚合查询
  17. AggregationBuilder aggregation = AggregationBuilders.sum("sum_age").field("age");
  18. builder.aggregation(aggregation);
  19. //搜索
  20. SearchRequest searchRequest = new SearchRequest();
  21. searchRequest.indices("cs_index");
  22. searchRequest.types("_doc");
  23. searchRequest.source(builder);
  24. // 执行请求
  25. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  26. // 解析查询结果
  27. System.out.println(response.toString());
  28. }
  29. }

聚合查询-求平均值

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 聚合查询 avg
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search31() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder();
  14. //条件搜索
  15. builder.query(QueryBuilders.matchAllQuery());
  16. //聚合查询
  17. AggregationBuilder aggregation = AggregationBuilders.avg("avg_age").field("age");
  18. builder.aggregation(aggregation);
  19. //搜索
  20. SearchRequest searchRequest = new SearchRequest();
  21. searchRequest.indices("cs_index");
  22. searchRequest.types("_doc");
  23. searchRequest.source(builder);
  24. // 执行请求
  25. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  26. // 解析查询结果
  27. System.out.println(response.toString());
  28. }
  29. }

聚合查询-计数

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 聚合查询 count
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search32() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder();
  14. //条件搜索
  15. builder.query(QueryBuilders.matchAllQuery());
  16. //聚合查询
  17. AggregationBuilder aggregation = AggregationBuilders.count("count_age").field("age");
  18. builder.aggregation(aggregation);
  19. //搜索
  20. SearchRequest searchRequest = new SearchRequest();
  21. searchRequest.indices("cs_index");
  22. searchRequest.types("_doc");
  23. searchRequest.source(builder);
  24. // 执行请求
  25. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  26. // 解析查询结果
  27. System.out.println(response.toString());
  28. }
  29. }

聚合查询-分组

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @SpringBootTest(classes = ElasticSearchApplication.class)
  3. public class SearchJunit {
  4. @Autowired
  5. private RestHighLevelClient client;
  6. /**
  7. * 聚合查询 分组
  8. * @throws IOException
  9. */
  10. @Test
  11. public void search33() throws IOException {
  12. // 创建请求
  13. SearchSourceBuilder builder = new SearchSourceBuilder();
  14. //条件搜索
  15. builder.query(QueryBuilders.matchAllQuery());
  16. //聚合查询
  17. AggregationBuilder aggregation = AggregationBuilders.terms("tag_createTime").field("createTime")
  18. .subAggregation(AggregationBuilders.count("count_age").field("age")) //计数
  19. .subAggregation(AggregationBuilders.sum("sum_age").field("age")) //求和
  20. .subAggregation(AggregationBuilders.avg("avg_age").field("age")); //求平均值
  21. builder.aggregation(aggregation);
  22. //不输出原始数据
  23. builder.size(0);
  24. //搜索
  25. SearchRequest searchRequest = new SearchRequest();
  26. searchRequest.indices("cs_index");
  27. searchRequest.types("_doc");
  28. searchRequest.source(builder);
  29. // 执行请求
  30. SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
  31. // 解析查询结果
  32. System.out.println(response.toString());
  33. }
  34. }