类比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("----------案件批量插入成功一轮------------");
-
}
-
}