ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Elasticsearch6.6.x 版本的学习(一)es 安装 和 JestClient 进行操作Elasticsearch6.6.x

2021-09-19 17:32:32  阅读:260  来源: 互联网

标签:String people Elasticsearch6.6 JestClient result 分片 new es


目录

一张图展示我们要学的东西

在这里插入图片描述

Elasticsearch和solr区别

在这里插入图片描述

ES下载安装

前提是你要本地安装jdk1.8以上的版本
Elasticsearch 7.6.1学习(一)下载windows 版本的es,下载可视化界面,使用kibana

在这里插入图片描述

ES head 插件的安装

他是一个web的前段工具,需要有node环境。
在这里插入图片描述
在这里插入图片描述
打开这个app.js

在这里插入图片描述
将这个改为es的地址

cmd里面进入这个目录

npm  run  start

在这里插入图片描述
以上就启动了

在这里插入图片描述

以上就启动这个了

ES 内置的REST接口

在这里插入图片描述

利用Kibana 对es 进行crud

Kibana 是一个可视化工具,需要配置这个工具要连接的es的地址

在这里插入图片描述
默认是localhost ,如果是要操作服务器上面的es,那么就要改这个里面的地址了

在这里插入图片描述

核心概念

前提

es是有集群的,比如这个集群里面有3个,那么3个es是在不同的服务器上面。一个es 里面可以创建很多的索引,就是索引库,就相当于数据库,创建的时候会定义分片的个数。分为主分片和父分片。

举个例子,集群有3个,在不同的服务器上面。你创建一个索引库,这个索引里面有2个主分片,每一个主分片有1个父分片。那么这个索引是在3个服务器上面都有的,并且每一个主分片和他对应的父分片在不同的服务器上面,也就是每一个服务器上面的es里面保存的数据都是一样的,只是分片不一样。

cluster

在这里插入图片描述

Shards

在这里插入图片描述

Replicas

在这里插入图片描述

Gateway

在这里插入图片描述

JestClient 进行操作Elasticsearch6.6.x

导入依赖

 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>



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



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

配置文件

在这里插入图片描述

spring.elasticsearch.jest.uris=http://127.0.0.1:9200
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.cluster-name=my-application

写实体类

在这里插入图片描述

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class people {

    private Integer id;

    private String name;

    private String phone;
}

写业务层,实现crud

创建索引库

@RestController
public class IndexController {

导入依赖,就可以直接使用
    @Autowired
    private JestClient jestClient;

    @GetMapping("createIndex")
    public String createIndex(String indexName) throws Exception{
        CreateIndex createIndex = new CreateIndex.Builder(indexName).build();

        System.out.println(createIndex);
        JestResult result = jestClient.execute(createIndex);
        return result.getJsonString();
    }

删除索引库

    @GetMapping("deleteIndex")
    public String deleteIndex(String indexName) throws Exception{
        DeleteIndex deleteIndex = new DeleteIndex.Builder(indexName).build();
        JestResult result = jestClient.execute(deleteIndex);
        return result.getJsonString();
    }

新增文档数据

  @GetMapping("creatDoc")
    public String creatDoc() throws Exception{

        people people = new people();
        people.setPhone("ddd");
        people.setName("iii");
        people.setId(55);
        Index.Builder builder = new Index.Builder(people);
        Index index = builder.index("people").type("peopledoc").build();
        JestResult result = jestClient.execute(index);
        return result.getJsonString();
    }

删除文档数据

 @GetMapping("deleteDoc")
    public String deleteDoc(String id) throws Exception{
        Delete index = new Delete.Builder(id).index("people").type("peopledoc").build();
        JestResult result = jestClient.execute(index);
        return result.getJsonString();
    }

查询文档数据

 @GetMapping("search")
    public String search(String Keyword) throws Exception{
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();


        searchSourceBuilder.query(new MultiMatchQueryBuilder(Keyword, "name","phone"));


        System.out.println(searchSourceBuilder.toString());


        SearchResult result = jestClient.execute(new Search.Builder(searchSourceBuilder.toString())
                .addIndex("people")
                .addType("peopledoc")
                .build());



        List<SearchResult.Hit<people, Void>> hits = result.getHits(people.class);


        for(SearchResult.Hit<people, Void> hit:  hits){
            people source = hit.source;

            System.out.println(source.getId());
            System.out.println(source.getPhone());
            System.out.println(source.getName());
            System.out.println("==============");
        }




        return result.getJsonString();
    }

标签:String,people,Elasticsearch6.6,JestClient,result,分片,new,es
来源: https://blog.csdn.net/python113/article/details/120379517

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有