idea下springboot 集成EasticSearch gradle

在集成之前,先下好EasticSearch,地址:点击打开链接

然后解压,运行对应bin的文件,看到如下即表示ok


输入localhost:9200,看到如下就可以了


先看下项目结构


1.新建一个项目


我是gradle构建的,maven都行的。


勾选


找不到就直接搜,然后直接next


接着还要加入jna依赖(如果gradle出错就多更新几次)


2.建一个domain包,创建一个Blog类

package com.example.springboot_es.domain;

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

import java.io.Serializable;

@Document(indexName = "blog", type = "blog")
/**
 * 注意:
 * 1.blog要实现 Serializable
 * 2.IDString
 */
public class Blog implements Serializable {

       @Id //2.表示为主键
    private String id;
       private String title;  //标题
    private String summary; //标签
    private String content;   //内容

    public Blog(){

    }

    public Blog(String title, String summary, String content) {
        this.title = title;
        this.summary = summary;
        this.content = content;
    }

    //省略getter()setter()

    @Override
    public String toString() {
        return "EsBlog{" +
                "id='" + id + '\'' +
                ", title='" + title + '\'' +
                ", summary='" + summary + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

3.建立一个BlogRepository接口

package com.example.springboot_es.repository;

import com.example.springboot_es.domain.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**
 * ES资源库接口
 * ElasticsearchRepository类似jpa,帮我们实现了很多方法
 * StringID的类型
 */
public interface BlogRepository extends ElasticsearchRepository<Blog, String > {

    /**
     * 分页查询博客:
     * Distinct为去除重复的数据
     * Containing关键字为表示包含
     * 就是titlesummarycontent包含关键字就返回内容
     * 返回的是分页的
     */
    Page<Blog> findByContent(String content, Pageable pageable);
    Page<Blog> findByTitle(String title, Pageable pageable);
    Page<Blog> findBySummary(String summary, Pageable pageable);

    Page<Blog> findDistinctByContentContainingOrSummaryContainingOrTitleContaining(String title, String summary, String content, Pageable pageable);
}

4.在application.properties配置:

#  elasticsearch服务地址,默认端口为9300
spring.data.elasticsearch.cluster-nodes=localhost:9300
#节点名字,默认elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearch  

5.在包名下建一个配置类MyConfig,位置可以看上面的图

package com.example.springboot_es;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration //配置注解
public class Myconfig {

    @Bean
    public TransportClient client() throws UnknownHostException {
        InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300); //这里为estcp端口9300,而不是http端口9200

        // my-application为默认的 ,可以在es文件夹下的config->elasticsearch.yml内修改
    Settings settings = Settings.builder().put("cluster.name", "my-application").build();


        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddresses(node);

        return client;
    }

}

6.建立一个测试类BlogTest,在test文件夹下

package com.example.springboot_es;

import com.example.springboot_es.domain.Blog;
import com.example.springboot_es.repository.BlogRepository;
import org.junit.After;
import org.junit.Assert;
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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class BlogTest {

    @Autowired
    private BlogRepository blogRepository;

    @After
    public void init(){

        //先清除所有数据
        blogRepository.deleteAll();

        //添加数据
    blogRepository.save(new Blog("登鹳雀楼", "王之涣", "白日依山尽,黄河入海流。欲穷千里目,更上一层楼。"));
        blogRepository.save(new Blog("静夜思", "李白", "床前明月光,疑是地上霜。举头望明月,低头思故乡。"));
        blogRepository.save(new Blog("江雪", "柳宗元", "千山鸟飞绝,万径人踪灭。孤舟蓑笠翁,独钓寒江雪。"));
        blogRepository.save(new Blog("九月九日忆山东兄弟", "王维", "独在异乡为异客,每逢佳节倍思亲。遥知兄弟登高处,遍插茱萸少一人。"));
        blogRepository.save(new Blog("登楼", "杜甫", "花近高楼伤客心,万方多难此登临。锦江春色来天地,玉垒浮云变古今。北极朝廷终不改,西山寇盗莫相侵。可怜后主还祠庙,日暮聊为《梁甫吟》。"));

    }

    @Test
    public void test(){
        /**
         * Pageable Spring Data库中定义的一个接口,该接口是所有分页相关信息的一个抽象,通过该接口,我们可以得到和分页相关所有信息(例如pageNumberpageSize等),这样,Jpa就能够通过pageable参数来得到一个带分页信息的Sql语句。
         * 0表示从第0条开始
         * 20表示每页最多20条数据
         */
        Pageable pageable = new PageRequest(0, 20);
//        String title = "";
//        String summary = "";
//        String content = "";
//        Page<Blog> page = blogRepository.findDistinctByContentContainingOrSummaryContainingOrTitleContaining(content, content, content, pageable);
        Page<Blog> page = blogRepository.findAll(pageable);
        System.out.println("------------------------------------------------------------------");

        System.out.println("count=" + page.getTotalElements());

        for (Blog blog : page.getContent()) {
            System.out.println(blog.toString());
        }

        System.out.println("------------------------------------------------------------------");
        Assert.assertEquals(page.getTotalElements(), 5);

    }
}

运行测试,看到结果就成功了,然后也可以测试其他的方法。。。


-------------------------------------------------------------------

如果

在配置过程中遇到如下错误,由于没有配置类,即MyConfig,如果还报错,重新运行下es


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值