码迷,mamicode.com
首页 > 其他好文 > 详细

elasticsearchTemplate操作es

时间:2019-06-09 23:43:42      阅读:1708      评论:0      收藏:0      [点我收藏+]

标签:vat   encoding   location   wired   string   framework   http   release   dir   

 

ElasticsearchTemplate是spring对java api的封装

 

maven依赖:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>3.1.8.RELEASE</version>
</dependency>

 

spring bean配置 spring-es.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <elasticsearch:transport-client id="client"
                                    cluster-nodes="192.168.71.137:9300"
                                    cluster-name="large-data-transfer-platform"/>
    <bean name="elasticsearchTemplate"
          class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg ref="client"/>
    </bean>
</beans>

 

autowired注入实例:

 

@Autowired
private 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 = new NativeSearchQueryBuilder()
                .withIndices(centerIndexName)
                .withTypes(centerArticleTypeName)
                .withQuery(boolQuery)
                .withPageable(pageable)
                .build();
        List<Map> maps = elasticsearchTemplate.queryForList(searchQuery, Map.class);
        System.out.println(JSON.toJSONString(maps));

 

elasticsearchTemplate操作es

标签:vat   encoding   location   wired   string   framework   http   release   dir   

原文地址:https://www.cnblogs.com/chenmz1995/p/10995307.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!