SpringBoot2.X之ElasticSearch的开发使用

背景:

          Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。(简单来说就是一种数据存储的方式,不过可以实现高级的搜索功能)

开发设计的步骤:

一:环境的搭建

在官网上进行下载相关的Elasticsearch版本服务,下载后直接解压就可以使用。方法如下:

1.官方网址:(由于我们使用的是SpringBoot2.x版本所以这里选择的Elasticsearch版本也是偏高的版本7.8.x)

https://www.elastic.co/cn/downloads/past-releases#elasticsearch

2. 开发文档的使用说明:(可以根据文档进行PostMan模拟使用)

https://www.elastic.co/guide/cn/elasticsearch/guide/current/_search_lite.html

3. SpringBoot的环境搭建(网上大多说是针对1.x版本的开发文档,这里使用2.X版本)

相关的配置:(uris表示访问地址,cluster-name表示集群的名字,cluster-nodes表示集群节点服务地址)

server.port=8088
spring.elasticsearch.jest.uris=http://localhost:9200
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=localhost:9300

 4. Book的bean对象声明:(其中@Document是专门针对elasticsearch专门的配置,可以直接配置index和type的类型。)

//@Document是针对elasticsearch专门的配置,可以直接配置index和type的类型。
@Document(indexName = "lhd",type = "book")
public class Book implements Serializable {
    private Integer id;
    private String name;
    private String content;

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Book{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}

 二.具体的相关测试

1. SpringBoot对于ElasticSearch的开发使用主要涉及到两种方式。

第一种:就是采用JEST方式进行相关的测试,使用的时候需要导入相关的工具包(io.searchbox.client.JestClient)

a.进行添加数据到其中索引为gcglhd,类型为book。插入的值为book对象类型。

  @Autowired
    JestClient jestClient;
    @Test
    public void contextLoads() {
        System.out.println("Hello 开始进行加载!");
        
        //给ES中索引(保存)一个文档
        Book book = new Book();
        book.setId(1);
        book.setName("lhd");
        book.setContent("Hello World");
//构建一个索引功能
        Index build = new Index.Builder(book).index("gcglhd").type("book").build();

        try {
            //执行
            jestClient.execute(build);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

注意:

开发过程中有个注意事项,那就是如果声明的对象有id属性,例如Book,在创建对象过程中如果给id属性赋值,就会默认的被作为索引对象的直接值,例如 http://localhost:9200/gcglhd/book/_search是查看gcglhd索引中book类型的所有对象数据,http://localhost:9200/gcglhd/book/1就是上面存储的数据对象。如果没有进行id值设定将会产生一个随机值,

[{"_index":"lhd","_type":"book","_id":"QLs1s3MB6Aly3uJLkyHJ","_score":1.0,"_source":{"id":null,"name":"西游屠龙记","content":null}},
{"id":1,"name":"西游记","content":"中国!"}}]

 这个时候如果进行查看西游屠龙的话只能通过http://localhost:9200/gcglhd/book/QLs1s3MB6Aly3uJLkyHJ进行查看。

b. 进行查看已经存储的数据

 @Test
    public void search() {
        String json="{\n" +
                "    \"query\" : {\n" +
                "        \"match_phrase\" : {\n" +
                "            \"content\" : \"Hello\"\n" +
                "        }\n" +
                "    }\n" +
                "}";
        //构建搜索功能
        Search build = new Search.Builder(json).addIndex("gcglhd").addType("book").build();

        try {
            SearchResult execute = jestClient.execute(build);
            System.out.println(execute.getJsonString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 第二种: 通过继承ElasticsearchRepository接口实现对Elasticsearch的相关操作。

首先写仓库接口,如BookRepository:(该方法的操作类似于对mysql的操作,只需要进行方法的声明,不需要实现就可以实现该方法)

/**
 * 类似于,mybatis的使用,先进行继承ElasticsearchRepository,然后就可以使用里面的所有的方法。
 */
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
    public List<Book> findByNameLike(String bookName);
}

 接着就是针对BookRepository的测试,这里是进行添加数据操作

    @Autowired
    BookRepository bookRepository;
    @Test
    public void test03(){
        System.out.println("当前进入SpringData ElasticSearch的使用方法!");
        Book book = new Book();
        book.setId(1);
        book.setName("西游记");
        book.setContent("中国!");

        Book book1 = new Book();
        book1.setId(2);
        book1.setName("西游降魔");

        Book book2 = new Book();
        book2.setName("西游屠龙记");
        bookRepository.index(book);
        bookRepository.index(book1);
        bookRepository.index(book2);
    }

 最后就是实现我在接口中声明的函数:

    @Test
    public void test04(){
        System.out.println("当前进入SpringData ElasticSearch的使用方法!");

        List<Book> books = bookRepository.findByNameLike("西游");
        for (Book book : books) {
            System.out.println(book);
        }

    }

 注意点:针对不同的SpringBoot版本和ElasticSearch版本是不同的情况,所以还请通过简单地测试后进行相关的后续开发。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值