SpringBoot集成Spring-data-Elasticsearch(elasticsearch 6.3.2 和kibana6.3.2)

1.kibana官网的下载地址:https://www.elastic.co/downloads/kibana#ga-release
2.elasticsearch官网下载地址https://www.elastic.co/downloads/elasticsearch
3.kibana的安装
进入config编辑kibana.yml 修改配置,放开以下两个注释
//kibana的端口
1.server.port: 5601
//elasticsearch的访问地址
2.elasticsearch.url: “http://localhost:9200
3.启动
进入bin目录下双击kibana.bat,启动kibana,访问 http://localhost:5601/ 即可访问kibana主页,若elasticsearch连接正常即可看到如下界面
这里写图片描述
4.整合Spring-data–Elasticsearch
这里写图片描述
(1)pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.myself.elk</groupId>
  <artifactId>elk</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.3.RELEASE</version>
  </parent> 

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Dalston.SR1</spring-cloud.version>
  </properties>

  <!-- 注意: 这里必须要添加, 否者各种依赖有问题  加了这段代码:以下有关springcloud,boot的版本号就不需要加了-->
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <!-- 管理依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.M7</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency> 
        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <!-- 使用jetty发布 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <!--添加适用于生产环境的功能,如性能指标和监测等功能。 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>        
         <!-- ES -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
         </dependency>       

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

(2)Owner.java

package com.myself.elk.bean;

import java.io.Serializable;
import java.util.Date;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

import lombok.Data;

 //索引 和类型设置
@Document(indexName = "owner", type = "doc")
@Data
public class Owner implements Serializable {
    private static final long serialVersionUID = 4608332739155548287L;
    //这里的注解一定要加上,否则启动会出错    
     @Id
    private long ownerId;

    /** 社区 */
    private long cmutyId;   

    private String title;

    private String author;
    ......
}

(3)OwnerRepository.java

package com.myself.elk.repository;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import com.myself.elk.bean.Owner;

public interface OwnerRepository extends ElasticsearchRepository<Owner,Long>{

}

(4)OwnerServiceImpl.java

package com.myself.elk.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.InternalMappedTerms;
import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;
import org.elasticsearch.search.aggregations.bucket.terms.LongTerms.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ResultsExtractor;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.myself.elk.bean.Model;
import com.myself.elk.bean.Owner;
import com.myself.elk.repository.OwnerRepository;
import com.myself.elk.service.OwnerService;

@Service
public class OwnerServiceImpl implements OwnerService {

    @Autowired
    private OwnerRepository ownerRepository;

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public void save(Owner owner) {
        ownerRepository.save(owner);
    }

    @Override
    public List<Owner> search(Owner owner) {
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        if(owner!=null){
            if(owner.getCmutyId()>0){
                queryBuilder.must(QueryBuilders.termQuery("cmutyId", owner.getCmutyId()));
            }
            if(owner.getOwnerId()>0){
                queryBuilder.must(QueryBuilders.termQuery("ownerId", owner.getOwnerId()));
            }

            //范围查询
            if(owner.getUidSta()>0){
                queryBuilder.must(QueryBuilders.rangeQuery("uid").gt(owner.getUidSta()).includeLower(true));
            }
            if(owner.getUidEnd()>0){
                queryBuilder.must(QueryBuilders.rangeQuery("uid").lt(owner.getUidEnd()).includeUpper(true));
            }

            if(StringUtils.isNotEmpty(owner.getPhone())){
                //查询前缀
                queryBuilder.must(QueryBuilders.prefixQuery("phone", owner.getPhone()));
            }
            //时间
            if(owner.getStartTime()!=null&&owner.getEndTime()!=null){
                queryBuilder.must(QueryBuilders.rangeQuery("usedTime").gte(owner.getStartTime()).lte(owner.getEndTime()).includeLower(true).includeUpper(true));
            }


        }
        //可输出查看es的查询
        System.err.println(queryBuilder);
        Iterable<Owner> list = ownerRepository.search(queryBuilder);
        List<Owner> li = Lists.newArrayList(list);
        return li;
    }

    @SuppressWarnings("all")
    @Override
    public List<Model> juhe(Owner owner) {
        //新建一个用于存储数据的集合
        List<Model> list=new ArrayList<Model>();
        //1.创建查询条件,也就是QueryBuild
        //QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();//设置查询所有,相当于不设置查询条件
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

        queryBuilder.must(QueryBuilders.termQuery("cmutyid", 112));
        //2.构建查询
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        //2.0 设置QueryBuilder
        nativeSearchQueryBuilder.withQuery(queryBuilder);

        //2.1设置搜索类型,默认值就是QUERY_THEN_FETCH,参考https://blog.csdn.net/wulex/article/details/71081042
        nativeSearchQueryBuilder.withSearchType(SearchType.QUERY_THEN_FETCH);//指定索引的类型,只先从各分片中查询匹配的文档,再重新排序和排名,取前size个文档
        //2.2指定索引库和文档类型
        nativeSearchQueryBuilder.withIndices("owner").withTypes("doc");//指定要查询的索引库的名称和类型,其实就是我们文档@Document中设置的indedName和type
        //2.3重点来了!!!指定聚合函数,本例中以某个字段分组聚合为例(可根据你自己的聚合查询需求设置)
        //该聚合函数解释:计算该字段(假设为cmutyid)在所有文档中的出现频次,并按照降序排名(常用于某个字段的热度排名)
        TermsAggregationBuilder termsAggregation = AggregationBuilders.terms("group_cmutyId").field("cmutyid").order(Terms.Order.count(false));
        nativeSearchQueryBuilder.addAggregation(termsAggregation);
        //2.4构建查询对象
        NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();  
        //3.查询
        Aggregations aggregations = elasticsearchTemplate.query(nativeSearchQuery, new ResultsExtractor<Aggregations>() {
            @Override
            public Aggregations extract(SearchResponse response) {
                return response.getAggregations();
            }
        });
        //转换成map集合
        Map<String, Aggregation> aggregationMap = aggregations.asMap();
        //获得对应的聚合函数的聚合子类,该聚合子类也是个map集合,里面的value就是桶Bucket,我们要获得Bucket
        //StringTerms stringTerms =  aggregationMap.get("group_cmutyId");
        Aggregation stringTerms =  aggregationMap.get("group_cmutyId");
        //获得所有的桶
        List<Bucket> buckets = ((InternalMappedTerms<LongTerms, Bucket>) stringTerms).getBuckets();
        //将集合转换成迭代器遍历桶,当然如果你不删除buckets中的元素,直接foreach遍历就可以了
        /*for (Bucket bucket : buckets)
        {   
            Model model = new Model();
            model.setName(bucket.getKey()+"");
            model.setValue(bucket.getDocCount());
            list.add(model);
        }*/
        Iterator<Bucket> iterator = buckets.iterator();
        while(iterator.hasNext()) {
            Model model = new Model();
            model.setName(iterator.next().getKey()+"");
            model.setValue(iterator.next().getDocCount());
            list.add(model);
        }


        return list;
    }

}

5.OwnerController.java

package com.myself.elk.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.myself.elk.bean.Owner;
import com.myself.elk.service.OwnerService;

@RestController
public class OwnerController {

    @Autowired
    private OwnerService ownerService;

    @RequestMapping("/save")
    public void  save(@RequestBody Owner owner) {
        ownerService.save(owner);
    }

}

6.启动类ElkRunApplication .java

package com.myself.elk;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

@SpringBootApplication
//@EnableElasticsearchRepositories(basePackages = "com.myself.elk.repository")
public class ElkRunApplication 
{
    public static void main( String[] args )
    {
        SpringApplication.run(ElkRunApplication.class, args);
    }
}

(7)application.properties

#端口
server.port=7001
#为服务命名
spring.application.name=elk-service
#访问的根路径
server.servlet.contextpath=/elk-service
#session失效时间
server.session-timeout=30
#编码
server.tomcat.uri-encoding=utf-8
# 取消默认注册服务
eureka.instance.hostname=localhost
# 向注册中心注册服务
eureka.client.register-with-eureka=false
# 检索服务
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#访问超市时间的设置
ribbon.ConnectTimeout=60000
ribbon.ReadTimeout=60000

#节点名字,默认elasticsearch
spring.data.elasticsearch.cluster-name=my-application 
#节点地址,多个节点用逗号隔开
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 
spring.data.elasticsearch.local=true
spring.data.elasticsearch.repositories.enable=true

(8)启动 访问
这里写图片描述
查看看kibana 就有一条记录添加进去咯

kibana的安装可以参考:https://blog.csdn.net/yanxiaobo1991/article/details/77118640
spring-data-elasticsearch 参考:https://blog.csdn.net/topdandan/article/details/81436141

没有更多推荐了,返回首页