【ElasticSearch實戰】——ES常用查詢條件與mysql對比
類比mysql,我們需要=、>、>=、<、<= 、or、and、in、like、count、sum、group by、order by、limit
在高版本的ES裡面使用了boolquery替換了filter
1、等於號=
-
QueryBuilders.termQuery("isDel",1));
-
//termQuery相當於=,相當於isDel=1
2、大於小於號
-
QueryBuilders.rangeQuery("overdueStartTime").lte(startDate.getTime())
-
//rangeQuery
-
//lte <=
-
//lt <
-
//gte >=
-
//gt >
3、or、and
-
boolQueryBuilder.must(QueryBuilders.termQuery("isDel",req.getIs_del()));
-
//must相當於and
-
caseStatusQuery.should(QueryBuilders.termQuery("caseStatus",s));
-
//should相當於or
4、in
-
QueryBuilders.termsQuery("clientCompanyId",req.getClientCompanyIdList())
-
//termQuery相當於in,可以傳遞list
5、like
-
QueryBuilders.wildcardQuery("phone1", "*" + req.getTelephone() + "*")
-
//wildcard 正則匹配
-
QueryBuilders.matchPhraseQuery("debtorName", req.getDebtor_name())
-
//matchPhrase 不會分詞,直接匹配有完整相關短語的記錄
6、sum&&count
-
AggregationBuilder debtTotalSum = AggregationBuilders.sum(DEBT_TOTAL_SUM_KEY).field("debtTotal");
-
AggregationBuilder debtTotalCount = AggregationBuilders.count(DEBT_TOTAL_COUNT_KEY).field("debtTotal");
7、order by and limit
-
SearchResponse searchResponse = client.prepareSearch(ElasticSearchUtil.getIndexName())
-
.setTypes(ElasticSearchUtil.TYPE_NAME)
-
.setQuery(boolQueryBuilder)
-
//指定查詢欄位
-
.addStoredField("id")
-
.addSort(order, sortOrder)
-
//分頁
-
.setFrom((currentPage - 1) * limit).setSize(limit)
-
.execute()
-
.actionGet();
8、批量插入
-
public void batchInsert(List<CaseTable> list,Client client) {
-
LOGGER.info("---------案件批量插入索引開始--------");
-
BulkRequestBuilder bulkRequest = client.prepareBulk();
-
long start = System.currentTimeMillis();
-
BulkRequestBuilder delRequest = client.prepareBulk();
-
for (CaseTable caseTable :list){
-
IndexRequestBuilder ir = client.prepareIndex()
-
.setIndex(ElasticSearchUtil.INDEX_NAME_CASE)
-
.setType(ElasticSearchUtil.TYPE_NAME_CASE)
-
.setId(caseTable.getId().toString())
-
.setSource(JSON.toJSONString(caseTable));
-
bulkRequest.add(ir);
-
start = System.currentTimeMillis();
-
BulkResponse bulkResponse = bulkRequest.execute().actionGet();
-
LOGGER.info("案件批量插入迴圈所用時間 search time:{}ms", System.currentTimeMillis() - start);
-
if (!bulkResponse.hasFailures()) {
-
LOGGER.info("----------案件批量插入成功一輪------------");
-
}
-
}