estemplate 导入MySQL_elasticsearchTemplate操作es

ElasticsearchTemplate是spring对java api的封装

maven依赖:

org.springframework.data

spring-data-elasticsearch

3.1.8.RELEASE

spring bean配置 spring-es.xml

class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">

autowired注入实例:

@Autowiredprivate ElasticsearchTemplate elasticsearchTemplate;

分页查询demo:

BoolQueryBuilder boolQuery =QueryBuilders.boolQuery();if (params.get("uuid") != null && StringUtils.isNotBlank(params.get("uuid").toString())) {

boolQuery.must(QueryBuilders.matchPhraseQuery("article_id", params.get("uuid")));

}if (params.get("subject_id") != null && StringUtils.isNotBlank(params.get("subject_id").toString())) {

boolQuery.must(QueryBuilders.matchPhraseQuery("subject_id", params.get("subject_id")));

}if (params.get("title_like") != null && StringUtils.isNotBlank(params.get("title_like").toString())) {

boolQuery.must(QueryBuilders.matchPhraseQuery("title", params.get("title_like")));

}if (params.get("content_like") != null && StringUtils.isNotBlank(params.get("content_like").toString())) {

boolQuery.must(QueryBuilders.matchPhraseQuery("content", params.get("content_like")));

}

Pageable pageable= new PageRequest(Integer.parseInt(String.valueOf(params.get("start"))), Integer.parseInt(String.valueOf(params.get("limit"))),new Sort(Sort.Direction.DESC, "update_time"));

NativeSearchQuery searchQuery= newNativeSearchQueryBuilder()

.withIndices(centerIndexName)

.withTypes(centerArticleTypeName)

.withQuery(boolQuery)

.withPageable(pageable)

.build();

List maps = elasticsearchTemplate.queryForList(searchQuery, Map.class);

System.out.println(JSON.toJSONString(maps));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值