- 浏览: 67892 次
文章分类
最新评论
-
小色帝:
我是天才是打发
Jquery实现的Tabs页 -
小色帝:
小色帝 写道1111而温热
Jquery实现的Tabs页 -
小色帝:
1111而温热
Jquery实现的Tabs页
一、简介
在上篇 SpringBoot 整合 ElasticSearch 文章中,我们详细的介绍了 ElasticSearch 的索引和文档的基本增删改查的操作方法!
本文将重点介绍 ES 的各种高级查询写法和使用。
废话不多说,直接上代码!
二、代码实践
本文采用的SpringBoot版本号是2.1.0.RELEASE,服务端 es 的版本号是6.8.2,客户端采用的是官方推荐的Elastic Java High Level Rest Client版本号是6.4.2,方便与SpringBoot的版本兼容。
es 最大的亮点就是查询非常丰富,可以在上亿的数据里面快速搜索出目标数据,查询如果实现呢?请看下文:
单条件精确查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 单条件精确查询
* @throws IOException
*/
@Test
public void search0() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.termsQuery("name", "赵里"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
多条件精确查询,取并集
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 多条件精确查询,取并集
* @throws IOException
*/
@Test
public void search1() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.termsQuery("name", "张", "陈"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
范围查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 范围查询,包括from、to
* @throws IOException
*/
@Test
public void search2() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.rangeQuery("age").from(20).to(32));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
/**
* 范围查询,不包括from、to
* @throws IOException
*/
@Test
public void search3() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.rangeQuery("age").from(20,false).to(30, false));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
/**
* 范围查询, lt:小于,gt:大于
* @throws IOException
*/
@Test
public void search4() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.rangeQuery("age").lt(30).gt(20));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
模糊查询,支持通配符
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 模糊查询,支持通配符
* @throws IOException
*/
@Test
public void search5() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.wildcardQuery("name","张三"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
不使用通配符的模糊查询,左右匹配
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 不使用通配符的模糊查询,左右匹配
* @throws IOException
*/
@Test
public void search6() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.queryStringQuery("张三").field("name"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
多字段模糊查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 多字段模糊查询
* @throws IOException
*/
@Test
public void search7() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.multiMatchQuery("长", "name", "city"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
多字段模糊查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 分页搜索
* @throws IOException
*/
@Test
public void search8() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.from(0).size(2);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
字段排序
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 排序,字段的类型必须是:integer、double、long或者keyword
* @throws IOException
*/
@Test
public void search9() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.sort("createTime", SortOrder.ASC);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
精确统计筛选文档数
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 精确统计筛选文档数,查询性能有所降低
* @throws IOException
*/
@Test
public void search10() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.trackTotalHits(true);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
设置源字段过滤返回
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
* @throws IOException
*/
@Test
public void search11() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.fetchSource(new String[]{"name","age","city","createTime"},new String[]{});
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
根据 id 精确匹配
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 根据id精确匹配
* @throws IOException
*/
@Test
public void search12() throws IOException {
String[] ids = new String[]{"1","2"};
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.termsQuery("_id", ids));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
matchAllQuery 搜索全部
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* matchAllQuery搜索全部
* @throws IOException
*/
@Test
public void search21() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.matchAllQuery());
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
match 搜索匹配
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* match搜索匹配
* @throws IOException
*/
@Test
public void search22() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.matchQuery("name", "张王"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
bool组合查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* bool组合查询
* @throws IOException
*/
@Test
public void search23() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
boolQueryBuilder.must(QueryBuilders.matchQuery("name", "张王"));
boolQueryBuilder.must(QueryBuilders.rangeQuery("age").lte(30).gte(20));
builder.query(boolQueryBuilder);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
nested 类型嵌套查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* nested类型嵌套查询
* @throws IOException
*/
@Test
public void search24() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件查询
BoolQueryBuilder mainBool=new BoolQueryBuilder();
mainBool.must(QueryBuilders.matchQuery("name", "赵六"));
//nested类型嵌套查询
BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
boolQueryBuilder.must(QueryBuilders.matchQuery("products.brand", "A"));
boolQueryBuilder.must(QueryBuilders.matchQuery("products.title", "巧克力"));
NestedQueryBuilder nested = QueryBuilders.nestedQuery("products",boolQueryBuilder, ScoreMode.None);
mainBool.must(nested);
builder.query(mainBool);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
多条件查询 + 排序 + 分页
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 多条件查询 + 排序 + 分页
* @throws IOException
*/
@Test
public void search29() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
boolQueryBuilder.must(QueryBuilders.matchQuery("name", "张王"));
boolQueryBuilder.must(QueryBuilders.rangeQuery("age").lte(30).gte(20));
builder.query(boolQueryBuilder);
//结果集合分页
builder.from(0).size(2);
//排序
builder.sort("createTime",SortOrder.ASC);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
聚合查询-求和
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 聚合查询 sum
* @throws IOException
*/
@Test
public void search30() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
builder.query(QueryBuilders.matchAllQuery());
//聚合查询
AggregationBuilder aggregation = AggregationBuilders.sum("sum_age").field("age");
builder.aggregation(aggregation);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
聚合查询-求平均值
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 聚合查询 avg
* @throws IOException
*/
@Test
public void search31() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
builder.query(QueryBuilders.matchAllQuery());
//聚合查询
AggregationBuilder aggregation = AggregationBuilders.avg("avg_age").field("age");
builder.aggregation(aggregation);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
聚合查询-计数
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 聚合查询 count
* @throws IOException
*/
@Test
public void search32() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
builder.query(QueryBuilders.matchAllQuery());
//聚合查询
AggregationBuilder aggregation = AggregationBuilders.count("count_age").field("age");
builder.aggregation(aggregation);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
聚合查询-分组
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 聚合查询 分组
* @throws IOException
*/
@Test
public void search33() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
builder.query(QueryBuilders.matchAllQuery());
//聚合查询
AggregationBuilder aggregation = AggregationBuilders.terms("tag_createTime").field("createTime")
.subAggregation(AggregationBuilders.count("count_age").field("age")) //计数
.subAggregation(AggregationBuilders.sum("sum_age").field("age")) //求和
.subAggregation(AggregationBuilders.avg("avg_age").field("age")); //求平均值
builder.aggregation(aggregation);
//不输出原始数据
builder.size(0);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
三、小结
本文主要围绕 SpringBoot 整合 ElasticSearch 进行各种高级查询的介绍,在下篇文章中,我们会重点介绍 es 的性能调优!
https://www.qb5200.com/article/492194.html
在上篇 SpringBoot 整合 ElasticSearch 文章中,我们详细的介绍了 ElasticSearch 的索引和文档的基本增删改查的操作方法!
本文将重点介绍 ES 的各种高级查询写法和使用。
废话不多说,直接上代码!
二、代码实践
本文采用的SpringBoot版本号是2.1.0.RELEASE,服务端 es 的版本号是6.8.2,客户端采用的是官方推荐的Elastic Java High Level Rest Client版本号是6.4.2,方便与SpringBoot的版本兼容。
es 最大的亮点就是查询非常丰富,可以在上亿的数据里面快速搜索出目标数据,查询如果实现呢?请看下文:
单条件精确查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 单条件精确查询
* @throws IOException
*/
@Test
public void search0() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.termsQuery("name", "赵里"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
多条件精确查询,取并集
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 多条件精确查询,取并集
* @throws IOException
*/
@Test
public void search1() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.termsQuery("name", "张", "陈"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
范围查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 范围查询,包括from、to
* @throws IOException
*/
@Test
public void search2() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.rangeQuery("age").from(20).to(32));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
/**
* 范围查询,不包括from、to
* @throws IOException
*/
@Test
public void search3() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.rangeQuery("age").from(20,false).to(30, false));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
/**
* 范围查询, lt:小于,gt:大于
* @throws IOException
*/
@Test
public void search4() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.rangeQuery("age").lt(30).gt(20));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
模糊查询,支持通配符
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 模糊查询,支持通配符
* @throws IOException
*/
@Test
public void search5() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.wildcardQuery("name","张三"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
不使用通配符的模糊查询,左右匹配
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 不使用通配符的模糊查询,左右匹配
* @throws IOException
*/
@Test
public void search6() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.queryStringQuery("张三").field("name"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
多字段模糊查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 多字段模糊查询
* @throws IOException
*/
@Test
public void search7() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.multiMatchQuery("长", "name", "city"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
多字段模糊查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 分页搜索
* @throws IOException
*/
@Test
public void search8() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.from(0).size(2);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
字段排序
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 排序,字段的类型必须是:integer、double、long或者keyword
* @throws IOException
*/
@Test
public void search9() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.sort("createTime", SortOrder.ASC);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
精确统计筛选文档数
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 精确统计筛选文档数,查询性能有所降低
* @throws IOException
*/
@Test
public void search10() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.trackTotalHits(true);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
设置源字段过滤返回
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
* @throws IOException
*/
@Test
public void search11() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.fetchSource(new String[]{"name","age","city","createTime"},new String[]{});
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
根据 id 精确匹配
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 根据id精确匹配
* @throws IOException
*/
@Test
public void search12() throws IOException {
String[] ids = new String[]{"1","2"};
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.termsQuery("_id", ids));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
matchAllQuery 搜索全部
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* matchAllQuery搜索全部
* @throws IOException
*/
@Test
public void search21() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.matchAllQuery());
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
match 搜索匹配
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* match搜索匹配
* @throws IOException
*/
@Test
public void search22() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.matchQuery("name", "张王"));
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
bool组合查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* bool组合查询
* @throws IOException
*/
@Test
public void search23() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
boolQueryBuilder.must(QueryBuilders.matchQuery("name", "张王"));
boolQueryBuilder.must(QueryBuilders.rangeQuery("age").lte(30).gte(20));
builder.query(boolQueryBuilder);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
nested 类型嵌套查询
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* nested类型嵌套查询
* @throws IOException
*/
@Test
public void search24() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件查询
BoolQueryBuilder mainBool=new BoolQueryBuilder();
mainBool.must(QueryBuilders.matchQuery("name", "赵六"));
//nested类型嵌套查询
BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
boolQueryBuilder.must(QueryBuilders.matchQuery("products.brand", "A"));
boolQueryBuilder.must(QueryBuilders.matchQuery("products.title", "巧克力"));
NestedQueryBuilder nested = QueryBuilders.nestedQuery("products",boolQueryBuilder, ScoreMode.None);
mainBool.must(nested);
builder.query(mainBool);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
多条件查询 + 排序 + 分页
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 多条件查询 + 排序 + 分页
* @throws IOException
*/
@Test
public void search29() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();
boolQueryBuilder.must(QueryBuilders.matchQuery("name", "张王"));
boolQueryBuilder.must(QueryBuilders.rangeQuery("age").lte(30).gte(20));
builder.query(boolQueryBuilder);
//结果集合分页
builder.from(0).size(2);
//排序
builder.sort("createTime",SortOrder.ASC);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
聚合查询-求和
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 聚合查询 sum
* @throws IOException
*/
@Test
public void search30() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
builder.query(QueryBuilders.matchAllQuery());
//聚合查询
AggregationBuilder aggregation = AggregationBuilders.sum("sum_age").field("age");
builder.aggregation(aggregation);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
聚合查询-求平均值
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 聚合查询 avg
* @throws IOException
*/
@Test
public void search31() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
builder.query(QueryBuilders.matchAllQuery());
//聚合查询
AggregationBuilder aggregation = AggregationBuilders.avg("avg_age").field("age");
builder.aggregation(aggregation);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
聚合查询-计数
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 聚合查询 count
* @throws IOException
*/
@Test
public void search32() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
builder.query(QueryBuilders.matchAllQuery());
//聚合查询
AggregationBuilder aggregation = AggregationBuilders.count("count_age").field("age");
builder.aggregation(aggregation);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
聚合查询-分组
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = ElasticSearchApplication.class)
public class SearchJunit {
@Autowired
private RestHighLevelClient client;
/**
* 聚合查询 分组
* @throws IOException
*/
@Test
public void search33() throws IOException {
// 创建请求
SearchSourceBuilder builder = new SearchSourceBuilder();
//条件搜索
builder.query(QueryBuilders.matchAllQuery());
//聚合查询
AggregationBuilder aggregation = AggregationBuilders.terms("tag_createTime").field("createTime")
.subAggregation(AggregationBuilders.count("count_age").field("age")) //计数
.subAggregation(AggregationBuilders.sum("sum_age").field("age")) //求和
.subAggregation(AggregationBuilders.avg("avg_age").field("age")); //求平均值
builder.aggregation(aggregation);
//不输出原始数据
builder.size(0);
//搜索
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("cs_index");
searchRequest.types("_doc");
searchRequest.source(builder);
// 执行请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
// 解析查询结果
System.out.println(response.toString());
}
}
三、小结
本文主要围绕 SpringBoot 整合 ElasticSearch 进行各种高级查询的介绍,在下篇文章中,我们会重点介绍 es 的性能调优!
https://www.qb5200.com/article/492194.html
相关推荐
SpringBoot整合Elasticsearch是现代Java开发中常见的一项任务,因为SpringBoot的便捷性和Elasticsearch的高效搜索能力,使得这种结合在数据检索、分析和日志存储等领域广泛应用。本项目"springboot-elasticsearch-...
springboot整合elasticsearch7,进行数据同步。elasticsearch相关度查询、排序。高亮显示;自动补全等功能。代码仅供参考,代码中有具体的注释,可以根据代码及注释内容,对自己项目架构及业务进行修改、整合。
springboot 2.0.2集成elasticsearch5.5.1,并使用集群模式,亲测可用!!!
在本项目中,我们主要探讨的是如何将...通过学习和实践这个"SpringBoot整合Elasticsearch完整源码",你可以了解到如何在Spring Boot应用中无缝集成Elasticsearch,从而利用其强大的搜索和分析能力来提升应用的功能。
本实例涵盖ES中的各类操作,如索引操作、CRUD操作、批处理、结果排序、分页查询、检索查询、关键字查询、高亮显示、逻辑查询、过滤查询、分组查询等等。并且已经过生产环境验证,各位可放心使用。
SpringBoot与Elasticsearch(ES)的整合是现代Java应用中常见的数据存储和检索解决方案。Elasticsearch是一款高性能、分布式、全文搜索引擎,广泛应用于日志分析、实时数据分析、搜索服务等领域。SpringBoot作为轻量...
通过以上步骤,你可以轻松地在Spring Boot应用中整合Elasticsearch,实现高效的全文搜索、数据分析和存储功能。随着项目的深入,还可以探索更高级的功能,如聚合分析、地理位置搜索、多租户支持等,进一步提升应用的...
springboot1.5.9整合elasticsearch2.3.5,elasticsearch跟springboot整合需要注意版本兼容,我这里整合的都是比较旧的版本。附带elasticsearch数据库。有问题随时留言。
SpringBoot整合Elasticsearch5.5.0是一个常见的任务,特别是在构建高性能、可扩展的搜索和分析应用中。本文将详细介绍如何将SpringBoot与Elasticsearch5.5.0进行集成,并提供一个简单的查询示例。 一、SpringBoot...
springboot 整合 elasticsearch demo(内含增删改查)以及基本es操作 效果图在这里 :https://blog.csdn.net/Japhet_jiu/article/details/105581997
这个"SpringBoot整合Elasticsearch完整源码"提供了实现这一目标的详细步骤和代码实例。以下是对相关知识点的详细说明: 1. **Spring Boot**: Spring Boot是由Pivotal Team创建的,旨在简化Spring应用的初始搭建...
SpringBoot整合Elasticsearch-图片,自己复习备用
在本项目中,我们主要探讨如何使用SpringBoot与Elasticsearch(ES)集成,以便查询存储在ES中的日志内容。Elasticsearch是一种强大的、分布式、实时的搜索和分析引擎,常用于处理大量数据,特别是在日志管理和分析...
springboot整合ElasticSearch,实现高性能搜索引擎
本实例涵盖ES中的各类操作,如索引操作、CRUD操作、批处理、结果排序、分页查询、检索查询、关键字查询、高亮显示、逻辑查询、过滤查询、分组查询等等。并且已经过生产环境验证,各位可放心使用。如有不对之处欢迎...
首先,让我们深入了解SpringBoot整合Elasticsearch的基本步骤: 1. **添加依赖**:在SpringBoot项目中,你需要在`pom.xml`或`build.gradle`文件中添加Elasticsearch和Spring Data Elasticsearch的相关依赖。确保...
springboot整合elasticsearch的实践代码,包括数据的增删改查案例
SpringBoot整合Elasticsearch7.2.0的实现方法 在本篇文章中,我们将详细介绍如何将 Spring Boot 与 Elasticsearch 7.2.0 整合的实现方法。该方法主要通过示例代码进行详细的介绍,对大家的学习或者工作具有一定的...