Elasticsearch 搜索引擎 安装简介 和springboot 例子

下载地址https://www.elastic.co/cn/downloads/past-releases/elasticsearch-6-8-4
检索
我们的应用经常添加检索功能,开源的ElasticSearch是目前全文搜索引擎的首选它可以快速的储存,收缩和分析海量的数据。springboot通过整合Spring Data ElasticSearch为我们提供了非常便捷的检索功能和支持

Elasticsearch是一个分布式的搜索服务 ,提供了Restful Api,底层基于Lucene,采用了shard(分片) 的凡是保证数据库的安全并且提供了resharding的功能,github等大型站点也是采用了ElasticSearch作为搜索服务的

Elasticsearch

官方文档https://www.elastic.co/guide/cn/elasticsearch/guide/current/running-elasticsearch.html
是一个面向文档的储存对象这就意味这它存储的是一个对象或者是一个文档
json
elasticearch使用json为文档的序列化格式(也就是把你储存的对象转换为Json存入elasticearch)Json序列化被大多数编程语言支持,并且已经成为了NoSQL领域的标准格式,简单,简介,易于阅读

Es集群储存规则

一个集群里边包含多个索引 一个索引里面拥有多个类型 类型里面由储存这个该类型的对象也就是文档
对照msql的话 就是 索引= 数据库
类型= 表
文档 = 表里面的数据

基本使用(基于Rest风格的)

索引一个员工(也就是添加一个员工)只能是put请求

在这里插入图片描述
响应体
在这里插入图片描述

检索一个员工(查找一个员工 只能用get请求)

url一样只是只能供get请求

在这里插入图片描述

更新一个员工使用put请求加入修改数据就行但是版

删除一个员工改为delete请求就行

检索一个员工是否存在使用head请求这个请求没有响应体如果有返回状态码为200ok没有就是404

springboot整合Elasticsearch

springboot默认支持两种技术来和es互交
1.
jest(默认不生效 需要导入工具包) 配置类JestAutoConfiguration
2.
springdata ElasticSearch 【可能版本不合适】解决办法有两个 1升级版本 2 安装对应的es 对应文档地址:https://github.com/spring-projects/spring-data-elasticsearch

错误

Error:Abnormal build process termination:
"C:\Program Files\Java\jdk1.8.0_121\bin\java" -Xmx700m -Djava.awt.headless=true -Djava.endorsed.dirs=\"\" -Djdt.compiler.useSingleThread=true -Dpreload.project.path=D:/ide/workspace/niuwan-root -Dpreload.config.path=C:/Users/relieved/.IntelliJIdea2017.3/config/options -Dcompile.parallel=false -Drebuild.on.dependency.change=true -Djava.net.preferIPv4Stack=true -Dio.netty.initialSeedUniquifier=-900763217256069649 -Dfile.encoding=GBK -Djps.file.types.component.name=FileTypeManager -Duser.language=zh -Duser.country=CN -Didea.paths.selector=IntelliJIdea2017.3 "-Didea.home.path=D:\Program Files\JetBrains\IntelliJ IDEA 2017.3.1" -Didea.config.path=C:\Users\relieved\.IntelliJIdea2017.3\config -Didea...
报这个错误关闭防火墙就行了

使用jest操作elasticsearch
@JestId 这个注解标注在实体类上来表示这个字段为id
导入依赖

<dependency>
   <groupId>io.searchbox</groupId>
   <artifactId>jest</artifactId>
   <version>5.3.3</version>
</dependency>

配置配置文件

#指定主机连接地址 默认为本地 默认端口
spring.elasticsearch.rest.uris=http://localhost:9200

添加例子

Article articel = new Article();
articel.setId(1);
articel.setTitle("好消息");
articel.setAuthor("张三");
articel.setContent("helloword");
//把article 对象储存到  jiatp这个索引里面 类型是news 由于我们自定了id 所有这里就没有外挂id .build 索引构建完成
Index index = new Index.Builder(articel).index("abc").type("news").build();
    // 发送
jestClient.execute(index);

查询例子

//查询表达式
String json ="{\n" +
      "    \"query\" : {\n" +
      "        \"match\" : {\n" +
      "            \"content\" : \"hello\"\n" +
      "        }\n" +
      "    }\n" +
      "}";
//更多操作:https://github.com/searchbox-io/Jest/tree/master/jest
//构建搜索功能 查询atguigu 下面的 news
Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();


//执行
try {
   SearchResult result = jestClient.execute(search);
   System.out.println(result.getJsonString());
} catch (IOException e) {
   e.printStackTrace();
}

}

springdata ElasticSearch操作ElasticSearch

@Document(indexName = “atguigu”,type = “book”) 指定索引 指定存储类型 加在类上
官方文档地址:https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
依赖

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

配置文件

#配置节点名称
spring.data.elasticsearch.cluster-name=elasticsearch
#配置主机地址
spring.data.elasticsearch.cluster-nodes=118.24.44.169:9301

这个和上面操作有很大的不同 那个就是我们需要就和Bestmybatis 继承一个接口 接口里面有定义好的方法我们可以可以自定以一个方法 比如模糊查询List findByBookNameLike
我们只需要传入参数让就会自动的给我们寻找 使用 ctrl +f12查看接口方法

public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
    //参照
    // https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
   public List<Book> findByBookNameLike(String bookName);
}

添加一个索引 book为我们自定义类

  Book book = new Book();
      book.setId(1);
      book.setBookName("西游记"); 
      book.setAuthor("吴承恩");
      bookRepository.index(book);

使用自定义方法查询

 list<Book> list = bookRepository.findByBookNameLike("游")

猜你喜欢

转载自blog.csdn.net/weixin_43979902/article/details/119849959