SpringBoot集成ES

SpringBoot集成ES概述

一,依赖

 
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
    

    
        UTF-8
        1.7
        1.7
        1.7.21
        1.2.16
        2.6
        3.3.2
        1.2.47
    

    
        
            nexus-aliyun
            Nexus aliyun
            http://maven.aliyun.com/nexus/content/groups/public
            
                true
            
            
                true
            
        
    


    
        
            org.springframework.boot
            spring-boot-starter-web
            2.0.4.RELEASE
        

        
            com.alibaba
            fastjson
            ${fastjson.version}
        

        
            org.slf4j
            slf4j-api
            ${slf4j.version}
        

        
            com.google.collections
            google-collections
            1.0
        
        
            commons-lang
            commons-lang
            ${commons-lang.version}
        
        
            org.springframework.data
            spring-data-elasticsearch
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

二,配置文件

server.port=9401
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300

 

 

三,源码

1,entity包

package com.tangdou.es2.entity;


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

import java.io.Serializable;

@Document(indexName = "creative_index", type = "creative", shards = 5, replicas = 1, indexStoreType = "fs", refreshInterval = "-1")
public class CreativeIndex implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    private Integer id;
    @Field
    private Integer position_id;
    @Field
    private String creative_id;
    @Field
    private Integer subscribe_id;
    @Field
    private Integer channel_id;
    @Field
    private Integer campaign_id;
    @Field
    private Long user_id;
    @Field
    private String title;
    @Field
    private String describe;
    @Field
    private String target_url;
    @Field
    private String open_url;
    @Field
    private String pic_url;
    @Field
    private Integer show_time;
    @Field
    private String video_url;
    @Field
    private Integer video_duration;
    @Field
    private String appinfo;
    @Field
    private String appid;
    @Field
    private String vid;
    @Field
    private Integer action;
    @Field
    private Integer state;
    @Field
    private Integer ad_type;
    @Field
    private Integer creative_type;
    @Field
    private Integer position_type;
    @Field
    private Integer local_industry;
    @Field
    private String refuse_reason;
    @Field
    private Integer country_id;
    @Field
    private Integer city_id;
    @Field
    private Integer province_id;


    public Integer getPosition_id() {
        return position_id;
    }

    public void setPosition_id(Integer position_id) {
        this.position_id = position_id;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCreative_id() {
        return creative_id;
    }

    public void setCreative_id(String creative_id) {
        this.creative_id = creative_id;
    }

    public Integer getSubscribe_id() {
        return subscribe_id;
    }

    public void setSubscribe_id(Integer subscribe_id) {
        this.subscribe_id = subscribe_id;
    }

    public Integer getChannel_id() {
        return channel_id;
    }

    public void setChannel_id(Integer channel_id) {
        this.channel_id = channel_id;
    }

    public Integer getCampaign_id() {
        return campaign_id;
    }

    public void setCampaign_id(Integer campaign_id) {
        this.campaign_id = campaign_id;
    }

    public Long getUser_id() {
        return user_id;
    }

    public void setUser_id(Long user_id) {
        this.user_id = user_id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public String getTarget_url() {
        return target_url;
    }

    public void setTarget_url(String target_url) {
        this.target_url = target_url;
    }

    public String getOpen_url() {
        return open_url;
    }

    public void setOpen_url(String open_url) {
        this.open_url = open_url;
    }

    public String getPic_url() {
        return pic_url;
    }

    public void setPic_url(String pic_url) {
        this.pic_url = pic_url;
    }

    public Integer getShow_time() {
        return show_time;
    }

    public void setShow_time(Integer show_time) {
        this.show_time = show_time;
    }

    public String getVideo_url() {
        return video_url;
    }

    public void setVideo_url(String video_url) {
        this.video_url = video_url;
    }

    public Integer getVideo_duration() {
        return video_duration;
    }

    public void setVideo_duration(Integer video_duration) {
        this.video_duration = video_duration;
    }

    public String getAppinfo() {
        return appinfo;
    }

    public void setAppinfo(String appinfo) {
        this.appinfo = appinfo;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getVid() {
        return vid;
    }

    public void setVid(String vid) {
        this.vid = vid;
    }

    public Integer getAction() {
        return action;
    }

    public void setAction(Integer action) {
        this.action = action;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public Integer getAd_type() {
        return ad_type;
    }

    public void setAd_type(Integer ad_type) {
        this.ad_type = ad_type;
    }

    public Integer getCreative_type() {
        return creative_type;
    }

    public void setCreative_type(Integer creative_type) {
        this.creative_type = creative_type;
    }

    public Integer getPosition_type() {
        return position_type;
    }

    public void setPosition_type(Integer position_type) {
        this.position_type = position_type;
    }

    public Integer getLocal_industry() {
        return local_industry;
    }

    public void setLocal_industry(Integer local_industry) {
        this.local_industry = local_industry;
    }

    public String getRefuse_reason() {
        return refuse_reason;
    }

    public void setRefuse_reason(String refuse_reason) {
        this.refuse_reason = refuse_reason;
    }

    public Integer getCountry_id() {
        return country_id;
    }

    public void setCountry_id(Integer country_id) {
        this.country_id = country_id;
    }

    public Integer getCity_id() {
        return city_id;
    }

    public void setCity_id(Integer city_id) {
        this.city_id = city_id;
    }

    public Integer getProvince_id() {
        return province_id;
    }

    public void setProvince_id(Integer province_id) {
        this.province_id = province_id;
    }
}

2,repository

package com.tangdou.es2.repository;

import com.tangdou.es2.entity.CreativeIndex;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface CreativeIndexRepository extends ElasticsearchRepository {
}

3,service

package com.tangdou.es2.service;

import com.tangdou.es2.entity.CreativeIndex;

import java.util.List;

public interface CreativeIndexService {

    CreativeIndex getOne(Integer id);

    List getOneT();

    List getAll();
}


package com.tangdou.es2.service.impl;

import com.google.common.collect.Lists;
import com.tangdou.es2.entity.CreativeIndex;
import com.tangdou.es2.repository.CreativeIndexRepository;
import com.tangdou.es2.service.CreativeIndexService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CreativeIndexServiceImpl implements CreativeIndexService {

    @Autowired
    private CreativeIndexRepository creativeIndexRepository;

    @Override
    public CreativeIndex getOne(Integer id) {
        CreativeIndex creativeIndexOptional = creativeIndexRepository.findById(id).get();
        System.out.println(creativeIndexOptional.getTitle());
        System.out.println(creativeIndexOptional.getAppid());
        return creativeIndexOptional;
    }

    @Override
    public List getOneT() {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termQuery("country_id", "0"));
        boolQueryBuilder.must(QueryBuilders.termQuery("province_id", "0"));
        boolQueryBuilder.must(QueryBuilders.termQuery("city_id", "0"));
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
        FieldSortBuilder sortBuilder = SortBuilders.fieldSort("id").order(SortOrder.DESC);
        nativeSearchQueryBuilder.withSort(sortBuilder);
        NativeSearchQuery query = nativeSearchQueryBuilder.build();
        return Lists.newArrayList(creativeIndexRepository.search(query));
    }

    @Override
    public List getAll() {
        Iterable indexIterable = creativeIndexRepository.findAll();
        return Lists.newArrayList(indexIterable);
    }


}

4,test包

package com.tangdou.es2;

import static org.junit.Assert.assertTrue;

import com.tangdou.es2.entity.CreativeIndex;
import com.tangdou.es2.service.CreativeIndexService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * Unit test for simple App.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class AppTest 
{
    /**
     * Rigorous Test :-)
     */

    @Autowired
    CreativeIndexService creativeIndexService;
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }

    @Test
    public void getOneTTest(){
        List creativeIndices=creativeIndexService.getOneT();
        for (CreativeIndex creativeIndex:creativeIndices) {
            System.out.println(creativeIndex.getAppid());
            System.out.println(creativeIndex.getTitle());
            System.out.println(creativeIndex.getId());
        }
    }
}

 

你可能感兴趣的:(work,summary,elk)