栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

springboot整合ElasticSearch

springboot整合ElasticSearch

默认支持两种技术和es交互

1.jest的方式 1.1前提

这里有个大的前提,就是springboot的版本不能太高。必须是1.5版本的。因为2.x版本的springboot已经没有这个jest的自动配置了。

 
        org.springframework.boot
        spring-boot-starter-parent
        1.5.12.RELEASE
         
    
1.2pom引入依赖

    io.searchbox
    jest
    5.3.4


1.3设置es请求地址

在application.properties配置文件中设置

spring.elasticsearch.jest.uris=http://120.78.152.93:9200/
1.4测试 1.4.1插入一个对象到es中

新建一个Article类,其中 @JestId是设置主键的意思。

@Data
public class Article {
    @JestId
    private  Integer id;
    private  String title;
    private  String desc;
    private String context;
}

 @Autowired
    JestClient jestClient;
    @Test
    public void testEs(){
        Article article=new Article();
        article.setId(1);
        article.setDesc("hello es");
        article.setContext("第一个es");

        //构建一个索引
        Index build = new Index.Builder(article).index("jf3q").type("news").build();
        try {
            //执行
            jestClient.execute(build);
        }catch (IOException e){
            e.printStackTrace();
        }

    }

![image.png](https://img-blog.csdnimg.cn/img_convert/c2bbda028647ba666ee3ba2c91b93b48.png#clientId=u89df12b6-0cea-4&from=paste&height=116&id=u465c6d55&margin=[object Object]&name=image.png&originHeight=116&originWidth=910&originalType=binary&ratio=1&size=16209&status=done&style=none&taskId=u0b5c387a-eaee-4e75-8a7d-5a796c283f1&width=910)

1.4.2全文搜索
 // 测试搜索
    @Test
    public void testSearch(){
        //查询表达式
        String json="{n" +
                "    "query" : {n" +
                "        "match_phrase" : {n" +
                "            "desc" : "hello"n" +
                "        }n" +
                "    }n" +
                "}";
        //构建搜索功能
    Search search = new Search.Builder(json).addIndex("jf3q").addType("news").build();
   try {
       SearchResult result = jestClient.execute(search);
       System.out.println(result.getJsonString());//打印json字符串
   }catch (Exception e){
       e.printStackTrace();
   }
2.springdata ElasticSeach的方式

这里注意下springboot要和es的版本匹配才行。

2.1pom引入依赖

我这里用的springboot版本是的1.5.12,然后我的elasticsearch的版本是2.4.6的。springdata es版本是2.1.11

 
        org.springframework.boot
        spring-boot-starter-parent
        1.5.12.RELEASE
         
    
 
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
2.2配置es的地址

在application.properties配置文件中设置

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=120.78.152.93:9300
2.3两种方式操作es

参考文档:https://github.com/spring-projects/spring-data-elasticsearch

  1. elasticsearchTemplate 操作es
  2. 编写一个ElasticsearchRepository 的子接口来操作ES:
    1. 先新建一个BookRepository
public interface BookRepository extends ElasticsearchRepository {
     //自定义方法
    public List findByBooeNameLike(String bookName);
}

  1. 新建Book实体
@document(indexName="jf3q",type = "book")
@Data
public class Book {
    private Integer id;
    private String booeName;
    private String author;
}

  1. 测试类
    @Autowired
    BookRepository bookRepository;
    @Test
    public void test(){
//         Book book=new Book();
//         book.setAuthor("杰凡IT");
//         book.setId(1);
//         book.setBooeName("有偿问答");
//         bookRepository.index(book);
		 for (Book book : bookRepository.findByBooeNameLike("问")) {
            System.out.println(book);
        }
     }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/439209.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号