SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作

一、概述

1.1 DB ~ ES

SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作
在这里插入图片描述

1.2 安装准备

JDK1.8 以上 ElasticSearch下载地址:ElasticSearch下载地址elasticsearch-head下载地址:elasticsearch-headkibana下载地址:kibana下载地址IK分词器插件:IK分词器插件以上版本要一致SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作

二、Win安装ES使用

解压即可 注意事项

2.1 jvm.options

SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作默认启动大小为 IG运行内存
根据自级的电脑可以配置
如果是虚拟机请配置小一点
如果阿里云服务器配置低的话也需要配置小一点

2.2 启动

bin 下的  elasticsearch.bat 双击即可

2.3 访问

127.0.0.1:9200

三、win安装可视化插件 Head

下载地址:elasticsearch-head

3.1 安装好node.js环境

安装依赖

cnpm install 
npm run start

访问:127.0.0.1:9100 访问不了………………….

3.2 解决跨域问题

elasticsearch.yml

// 添加
http.cors.enabled: true
http.cors.allow-origin: "*"

3.3 重新启动elasticsearch

SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作
在这里插入图片描述

四、安装可视化平台 Kibana

4.1 简介

ELK是Elasticsearch. Logstash. Kibana三大开源框架首字母大写简称。市面上也被成为Elastic Stack。其中Elasticsearch是- 一个基于Lucene、分布式、通过Restful方式进行交互的近实时搜索平台框架。像类似百度、谷歌这种大数据全文搜索引擎的场景都可以使用Elasticsearch作为底层支持框架,可见Elasticsearch提供的搜索能力确实强大,市面上很多时候我们简称Elasticsearch为es. Logstash是ELK的中央数据流引擎,用于从不同日标(文件/数据存储/MQ )收集的不同格式数据,经过过滤后支持输出到不同目的 地(文件/MQ/redis/elasticsearch/kafka等)。Kibana可以将elasticsearch的数据通过友好的页面展示出来 ,提供实时分析的功 能。市面上很多开发只要提到ELK能够一致说出它是一 个日志分析架构技术栈总称 ,但实际上ELK不仅仅适用于日志分析,它还可以支持其它任何数据分析和收集的场景,日志分析和收集只是更具有代表性。并非唯一性。

4.2 win下载安装kibana

kibana下载地址:kibana下载地址启动

bin 下的 kibana.bat 双击使用

访问:

127.0.0.1:5601
SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作
在这里插入图片描述

4.3 修改汉化Kinana

SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作
在这里插入图片描述
i18n.locale: "zh-CN"

4.4 重新启动Kibana即可

SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作
在这里插入图片描述

五、安装IK分词器插件

5.1 下载

IK分词器插件:IK分词器插件

5.2 集成到ES

打开ES的plugins的目录,进行解压SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作重启ES

5.3 kibana测试

SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作
在这里插入图片描述

5.4 REST操作

method url地址 描述
PUT localhost:9200/索引名称/类型名称/文档id 创建文档(指定文档id )
POST localhost:9200/索引名称/类型名称 创建文档(随机文档id )
POST localhost:9200/索引名称/类型名称/文档id/_ update 修改文档
DELETE localhost:9200/索引名称/类型名称/文档id 删除文档
GET localhost:9200/索引名称/类型名称/文档id 查询文档通过文档id
POST localhost:9200/索引名称/类型名称/ search 查询所有文档

5.4.1 PUT创建索引

// put /索引名/类型名/文档ID   {请求体}
PUT /test1/type1/1
{
 "name""古力娜扎",
 "age":26
}

// 索引字段类型设置
PUT /test2
{
 "mappings":{
  "properties":{
   "name":{
    "type":"text"
   },
   "age":{
    "type":"long"
   }
  }
 }
}

5.4.2 GET获取索引信息

GET test2

5.4.3 POST修改数据

//1. 直接put 相同的ID  版本号增加
//2. update
UPDATE /test1/type1/1/_update
{
 "doc"{
  "name""古力娜扎",
  "age":30
 }
}

5.4.4 DELETE删除索引

DELETE test1

六、SpringBoot集成ES

SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作
在这里插入图片描述

6.1 添加依赖

但是springboot默认的ES版本依赖和自己的ES不一致SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作修改SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作

<dependency>
 <groupId>org.elasticsearch.client</groupId>
 <artificatId>elasticsearch-rest-high-level-client</artificatId>
 <version>7.6.2</version>
</dependency>

6.2 代码测试

ElasticSearchConfig.java


public class ElasticSearchConfig{
 @Bean
 public RestHighLevelClient restHighLevelClient (){
  RestHighLevelClient client = new RestHighLevelClient(
   RestClient.builder(
    new HttpHost("localhost",9200,"http")));
  return client;
 }
}

增删改查—–测试类


@Autowired
private RestHighLevelClient client;

// 1.创建索引
void testCreateIndex(){
 CreateIndexRequest request = new CreateIndexRequest("test_index");
 CreateIndexResponse response = client.indices().creatre(request,RequestOptions.DEFAULT);
 
 System.out.println(response);

// 2.获取索引信息
void testGetIndex(){
 GetIndexRequest request = new GetIndexRequest("test_index");
 boolean b = client.indicies.exists(request,RequestOptions.DEFAULT);
 
    System.out.println(b);
}

// 3. 删除索引
void testDeleteIndex(){
 DeleteIndexRequest request = new DeleteIndexRequest("test_index");
 AcknowledgeResponse delete = client.indicies.delete(request,RequestOptions.DEFAULT);
 
    System.out.println(delete.isAcknowledge());
}

}

6.3 文档创建【增删改查】

创建实体类

@Component
@AllArgsConstrator
@NoArgsConstrator
public class User{
 private String name;
 private int age;
}

测试类

// 4.添加文档
void testAddDoc(){
 User user = new User("古力娜扎",26);
 IndexRequest request = new IndexRequest("test_index");
 // 规则 put /test_index/_doc/1
 request.id("1");
 request.timeout(TimeValue.timeValueSeconds(1));
 request.tomeout("1s");

 request.source(JSON.toJSONString(user),XContentType.JSON);
 // 发送
 IndexResponse response = client.index(request,RequestOptions.DEFAULT);
 System.out.println(response.toString());
 System.out.println(response.status());
}

// 5.获取文档
void testGetDoc(){
 GetRequest request = new GetRequest("test_index","1");
 request.fetchSourceContext(new FetchSourceContext(false));
 request.storeFields("_none_");
 // 是否存在???
 boolean b = client.exists(request,RequestOptions.DEFAULT);
 System.out.println(b);

 // 获取
 GetResponse response = client.get(request,RequestOptions.DEFAULT);
 System.out.println(response.getSourceAsString());
 System.out.println(response);
}

// 6. 更新文档
void testUpdateDoc(){
 UpdateRequest updateRequest = new UpdateRequest("test_index",1);
 updateRequest.timeout("1s");
 User user = new User("迪丽热巴"28);
 updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
 UpdateResponse response = client.update(request,RequestOptions.DEFAULT);
 System.out.println(response);
 System.out.println(response.status());
}

// 7.删除文档
void testDeleteDoc(){
 DeleteRequest request = new DeleteRequest("test_index",1);
 updateRequest.timeout("1s");
 DeleteResponse response = client.delete(request,RequestOptions.DEFAULT);
 System.out.println(response);
 System.out.println(response.status());
}

// 8.批量插入数据
void testBulkRequest(){
 BulkRequest bulkRequest = new BulkRequest();
 bulkRequest.timeout("15s");
 ArrayList<User> userList = new ArrayList<>();
 userList.add(new User("古力娜扎1号",26));
 userList.add(new User("古力娜扎2号",27));
 userList.add(new User("古力娜扎3号",28));
 userList.add(new User("古力娜扎4号",29));
 userList.add(new User("古力娜扎5号",30));
 userList.add(new User("古力娜扎6号",31));
 userList.add(new User("古力娜扎7号",32));

 for(int i = 0;i<=userList.size();i++){
  bulkRequest.add(
   new IndexRequest("test.index").
   id(""+(i+1)).
   source(JSON.toJSONString(userList.get(i)),XContentType.JSON));
 }
 // 批量插入
 BulkResponse response = client.bulk(bulkRequest,RequestOptions.DEFAULT);
 System.out.println(bulkRequest.hasFailures());
}


// 9.条件搜索
void testSearch(){
 SearchRequest searchRequest = new SearchRequest("test_index");
 // 构造条件
 SearchSourceBuilder builder = new   SearchSourceBuilder();
 // 精确匹配
 //TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name","古力娜扎");
 
 // 匹配全部 matchAllQuery();
 builder.query(termQueryBuilder);
 // 分页
 builder.from();
 builder.size();
 builder.timeout("60s");
 searchRequest.source(builder);
 SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
 System.out.println(JSON.toJSONString(response.getHits()));
 
 //遍历出来
 for(SearchHit docFields : searchRequest.getHits().getHits()){
  System.out.println(docFields.getSourceAsMap());
 }
}


原文始发于微信公众号(Coding路人王):SpringBoot集成ElasticSearch 7.6【新版本】及安装使用操作

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/147801.html

(0)
小半的头像小半

相关推荐

发表回复

登录后才能评论
极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!