zoukankan      html  css  js  c++  java
  • 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));
  • 相关阅读:
    【转】日本留学——修士申请注意事项
    【转】日本留学读研究生和修士有什么区别?申请误区有哪些
    【转】为什么说学一门小语种,就能打开新世界的大门?
    【转】TED:两年一门外语--她总结了学外语的秘诀
    【转】为什么一定要学一门外语?
    【转】学完标准日本语初级至高级,可以过日语n1吗?
    【转】去日本语言学校前,日语应该达到几级呢?
    Cordova学习
    敏捷开发实录(二)
    Mac端博客发布工具推荐
  • 原文地址:https://www.cnblogs.com/chenmz1995/p/10995307.html
Copyright © 2011-2022 走看看