elasticsearchTemplate操作es

 

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));

 

上一篇:c-隐藏窗口的PrintWindow和BitBlt


下一篇:opensuse – Netbeans在我的计算机上没有显示