<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
spring:
elasticsearch:
rest:
uris: 192.168.1.101:9200
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
/**
* @author Wang
* @Date 2022/8/27
*/
@Service
@Slf4j
@RequiredArgsConstructor
public class IotDataLogServiceImpl implements IotDataLogService {
/**
* ① ElasticsearchRestTemple是ElasticsearchOperations的子类的子类
* ② 在ES7.x以下的版本使用的是ElasticsearchTemple,7.x以上版本已弃用ElasticsearchTemple,使用ElasticsearchRestTemple替代
*/
final RestHighLevelClient highLevelClient;
private static final String IOT_DATA_LOG = "iot_data_log";
private static final String IOT_SIMULATOR_LOG = "iot_simulator_log";
@Override
public TableDataInfo searchV2(IotDataLogRequest iotDataLogRequest) {
TableDataInfo tableDataInfo = new TableDataInfo();
SearchRequest searchRequest = new SearchRequest(IOT_DATA_LOG);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
if (StringUtils.isNotEmpty(iotDataLogRequest.getDeviceUid())) {
boolBuilder.must(QueryBuilders.termQuery("deviceUid", iotDataLogRequest.getDeviceUid()));
}
if (StringUtils.isNotEmpty(iotDataLogRequest.getProductUid())) {
boolBuilder.must(QueryBuilders.termQuery("productUid", iotDataLogRequest.getProductUid()));
}
if (StringUtils.isNotEmpty(iotDataLogRequest.getTopic())) {
boolBuilder.must(QueryBuilders.termQuery("topic", iotDataLogRequest.getTopic()));
}
// createTime > start and createTime < end
if (Objects.nonNull(iotDataLogRequest.getStart()) && Objects.nonNull(iotDataLogRequest.getEnd())) {
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("createTime")
.from(iotDataLogRequest.getStart())
.to(iotDataLogRequest.getEnd());
boolBuilder.must(rangeQueryBuilder);
}
sourceBuilder.from(iotDataLogRequest.getPageNum() * iotDataLogRequest.getPageSize());
sourceBuilder.size(iotDataLogRequest.getPageSize());
sourceBuilder.sort(new FieldSortBuilder("createTime").order(SortOrder.DESC));
sourceBuilder.query(boolBuilder);
searchRequest.source(sourceBuilder);
try {
SearchResponse response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
org.elasticsearch.search.SearchHits hits = Objects.requireNonNull(response).getHits();
TotalHits totalHits = hits.getTotalHits();
long total = Objects.nonNull(totalHits) ? totalHits.value : 0;
List<IotDataLog> list = Lists.newArrayList();
for (org.elasticsearch.search.SearchHit hit : hits.getHits()) {
IotDataLog data = JSONUtil.toBean(hit.getSourceAsString(), IotDataLog.class);
list.add(data);
}
tableDataInfo.setRows(list);
tableDataInfo.setTotal(total);
tableDataInfo.setCode(HttpStatus.OK.value());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return tableDataInfo;
}
@Override
public TableDataInfo searchSimulator(IotDataLogRequest iotDataLogRequest) {
TableDataInfo tableDataInfo = new TableDataInfo();
SearchRequest searchRequest = new SearchRequest(IOT_SIMULATOR_LOG);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
// and deviceUid=deviceUid
if (StringUtils.isNotEmpty(iotDataLogRequest.getDeviceUid())) {
boolBuilder.must(QueryBuilders.termQuery("deviceUid", iotDataLogRequest.getDeviceUid()));
}
// and simulatorUid=simulatorUid
if (StringUtils.isNotEmpty(iotDataLogRequest.getSimulatorUid())) {
boolBuilder.must(QueryBuilders.termQuery("simulatorUid", iotDataLogRequest.getSimulatorUid()));
}
// and topic=topic
if (StringUtils.isNotEmpty(iotDataLogRequest.getTopic())) {
boolBuilder.must(QueryBuilders.termQuery("topic", iotDataLogRequest.getTopic()));
}
// and (source = source or to = to)
if (StringUtils.isNotEmpty(iotDataLogRequest.getSource()) && StringUtils.isNotEmpty(iotDataLogRequest.getTo())) {
boolBuilder.should(QueryBuilders.matchQuery("source", iotDataLogRequest.getSource()))
.should(QueryBuilders.matchQuery("to", iotDataLogRequest.getTo()));
// 至少匹配一个条件
boolBuilder.minimumShouldMatch(1);
}
sourceBuilder.from(iotDataLogRequest.getPageNum() * iotDataLogRequest.getPageSize());
sourceBuilder.size(iotDataLogRequest.getPageSize());
sourceBuilder.sort(new FieldSortBuilder("createTime").order(SortOrder.DESC));
sourceBuilder.query(boolBuilder);
searchRequest.source(sourceBuilder);
try {
SearchResponse response = highLevelClient.search(searchRequest, RequestOptions.DEFAULT);
log.debug(searchRequest.source().toString());
org.elasticsearch.search.SearchHits hits = Objects.requireNonNull(response).getHits();
TotalHits totalHits = hits.getTotalHits();
long total = Objects.nonNull(totalHits) ? totalHits.value : 0;
List<IotSimulatorLog> list = Lists.newArrayList();
for (org.elasticsearch.search.SearchHit hit : hits.getHits()) {
IotSimulatorLog data = JSONUtil.toBean(hit.getSourceAsString(), IotSimulatorLog.class);
list.add(data);
}
tableDataInfo.setRows(list);
tableDataInfo.setTotal(total);
tableDataInfo.setCode(HttpStatus.OK.value());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return tableDataInfo;
}
}
select * from person where name = '张无忌';
es api
@Autowired
private RestHighLevelClient client;
@Test
public void queryTerm() throws IOException {
// 根据索引创建查询请求
SearchRequest searchRequest = new SearchRequest("person");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.termQuery("name.keyword", "张无忌"));
System.out.println("searchSourceBuilder=====================" + searchSourceBuilder);
searchRequest.source(searchSourceBuilder);
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(JSONObject.toJSON(response));
}
sql
select * from persons where sect in('明教','武当派');
es api
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.termsQuery("sect.keyword", Arrays.asList("明教", "武当派")));
sql
select * from pesons where age between 18 and 22;
es api
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.rangeQuery("age").gte(10).lte(30));
sql
select * from persons where sect like '武当%';
es api
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.prefixQuery("sect.keyword","武当"));
sql
select * from persons where name like '张%忌';
es api
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.wildcardQuery("sect.keyword","张*忌"));
sql
select * from persons where sex = '女' and sect = '明教';
es api
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("sex", "女"))
.must(QueryBuilders.termQuery("sect.keyword", "明教"))
);
所有 must 语句必须匹配,所有 must_not 语句都必须不匹配,但有多少 should 语句应该匹配呢?默认情况下,没有 should 语句是必须匹配的,只有一个例外:那就是当没有 must 语句的时候,至少有一个 should 语句必须匹配。
我们可以通过 minimum_should_match 参数控制需要匹配的 should 语句的数量,它既可以是一个绝对的数字,又可以是个百分比:
简单实现
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("sex", "女"))
.should(QueryBuilders.termQuery("address.word", "峨眉山"))
.should(QueryBuilders.termQuery("sect.keyword", "明教"))
.minimumShouldMatch(1)
);
复杂sql
select
*
from
persons
where
sex = '女'
and
age between 30 and 40
and
sect != '明教'
and
(address = '峨眉山' OR skill = '暗器')
复杂 es api
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("sex", "女"))
.must(QueryBuilders.rangeQuery("age").gte(30).lte(40))
.mustNot(QueryBuilders.termQuery("sect.keyword", "明教"))
.should(QueryBuilders.termQuery("address.keyword", "峨眉山"))
.should(QueryBuilders.rangeQuery("power.keyword").gte(50).lte(80))
.minimumShouldMatch(1); // 设置should至少需要满足几个条件
// 将BoolQueryBuilder构建到SearchSourceBuilder中
searchSourceBuilder.query(boolQueryBuilder);
query和filter的区别:query查询的时候,会先比较查询条件,然后计算分值,最后返回文档结果;而filter是先判断是否满足查询条件,如果不满足会缓存查询结果(记录该文档不满足结果),满足的话,就直接缓存结果,filter不会对结果进行评分,能够提高查询效率。
filter的使用方式比较多样,下面用几个例子演示一下。
简单实现
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.boolQuery()
.filter(QueryBuilders.termQuery("sex", "男"))
);
方式二,和must、must_not同级,相当于子查询:
sql
select * from (select * from persons where sect = '明教')) a where sex = '女';
es api
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("sect.keyword", "明教"))
.filter(QueryBuilders.termQuery("sex", "女"))
);
方式三,将must、must_not置于filter下,这种方式是最常用的:
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// 构建查询语句
searchSourceBuilder.query(QueryBuilders.boolQuery()
.filter(QueryBuilders.boolQuery()
.must(QueryBuilders.termQuery("sect.keyword", "明教"))
.must(QueryBuilders.rangeQuery("age").gte(20).lte(35))
.mustNot(QueryBuilders.termQuery("sex.keyword", "女")))
);
参考链接
https://z.itpub.net/article/detail/A7B79869961FB96969AADEA98959D9FC