前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot检索之整合elasticsearch并使用spring-data操作

springboot检索之整合elasticsearch并使用spring-data操作

作者头像
西西嘛呦
发布2020-08-26 15:58:50
7160
发布2020-08-26 15:58:50
举报

各种过时,版本不兼容,只能暂时将代码放在这先做参考了。

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

Book.java

package com.gong.spingbootes.bean;

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

//这里必须指明索引名字和类型
@Document(indexName = "gong",type = "book")
public class Book {
    private Integer id;
    private String bookName;
    private String author;

    public Book(){}

    public Book(Integer id, String bookName, String author) {
        this.id = id;
        this.bookName = bookName;
        this.author = author;
    }

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

    public Integer getId() {
        return id;
    }

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

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }
}

BookRepository.java

package com.gong.spingbootes.repository;

import com.gong.spingbootes.bean.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;
//第一个泛型是对象类型,第二个泛型是book的主键
public interface BookRepository extends ElasticsearchRepository<Book,Integer>{
   //这里的名字不是随意取的,而是根据查询需求来取的,具体可参考相关文档
    public List<Book> findByBookNameLike(String bookName);
}

application.properties

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=192.168.124.22
#这两玩意过时了

SpringbootEsApplicationTests.java

package com.gong.spingbootes;

import com.gong.spingbootes.bean.Article;
import com.gong.spingbootes.bean.Book;
import com.gong.spingbootes.repository.BookRepository;
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;


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

    @Autowired
    BookRepository bookRepository;

    @Test
    public void contextLoads() {
    }

  //添加索引
    @Test
    public void index() {
        bookRepository.index(new Book(1,"java","德玛"));
    }
  //进行查询
    @Test
    public void search() {
        bookRepository.findByBookNameLike("ja");
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档