Elasticsearch入门,超详细

80 阅读2分钟

ES

一. 初始elasticsearch

1.什么是elasticsearch
  • 一个开源的分布式搜索引擎,可以用来实现搜索、日志统计、分析、系统监控

  • 什么是elastic stack(ELK)

  • 是以elasticsearch为核心的技术栈,包括beats、Logstash、kibana、elasticsearch

  • 什么是lucene

  • 是Apache的开源搜索引擎类库,提供了搜索引擎的核心API

2. 倒排索引

在这里插入图片描述

3. es和mysql概念对比

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

4. 安装es、安装kibana、安装IK分词器、IK分词器的拓展和停用词典

二、操作索引库

1. mapping属性

在这里插入图片描述

2. 创建索引库

在192.168.198.138:5601网址中找到左边菜单dev_tools

 # 创建索引库
 PUT /heima
{
 "mappings": {
   "properties": {
     "info": {
       "type": "text",
       "analyzer": "ik_smart"
     },
     "email": {
       "type": "keyword",
       "index": false
     },
     "name": {
       "type": "object",
       "properties": {
         "firstName": {
           "type": "keyword"
         },
         "lastName": {
           "type": "keyword"
         }
       }
     }
   }
 }
}
3. 查询、删除、修改索引库
# 查询
GET /heima

# 修改索引库,添加新字段
PUT /heima/_mapping
{
  "properties":{
    "age":{
      "type": "long"
    }
  }
}

# 删除
DELETE /heima

三、文档操作

1. 新增、查询、删除
# 插入文档
POST /heima/_doc/1
{
  "info": "黑马程序员Java讲师",
  "email": "zy@itcast.cn",
  "name": {
    "firstName": "云",
    "lastName": "赵"
  }
}

# 查询文档
GET heima/_doc/1

 # 删除文档
DELETE /heima/_doc/1
2. 修改文档
# 全量修改文档
PUT /heima/_doc/1
{
  "info": "黑马程序员Java讲师",
  "email": "ZhaoYun@itcast.cn",
  "name": {
    "firstName": "云",
    "lastName": "赵"
  }
}

# 局部修改文档字段
POST /heima/_update/1 
{
  "doc": {
    "email": "ZYUN@ITACS"
  }
}

四、RestClient操作索引库

1. 导入数据库,demo
2. hotel数据结构分析
3. 初始化RestClient

在这里插入图片描述

4. 创建索引库

在这里插入图片描述

5. 删除和判断索引库
    @Test
    void testDeleteHoteIndex() throws IOException {
        // 1.创建Request对象
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        // 2.发送请求
        client.indices().delete(request, RequestOptions.DEFAULT);
    }

    @Test
    void testExistsHotelIndex() throws IOException {
        // 1.创建Request对象
        GetIndexRequest request = new GetIndexRequest("hotel");
        // 2.发送请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        // 3.输出
        System.err.println(exists ? "索引库已经存在" : "索引库不存在!");
    }

五、RestClient操作文档

1. 新增文档
   @Test
    void testAddDocument() throws IOException {
        // 根据id查询酒店数据
        Hotel hotel = hotelService.getById(38812L);
        // 转换为文档类型
        HotelDoc hotelDoc = new HotelDoc(hotel);

        // 1.准备Request对象
        IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
        // 2.准备Json文档
        request.source(JSON.toJSONString(hotelDoc),XContentType.JSON);
        // 3.发送请求
        client.index(request,RequestOptions.DEFAULT);
    }
2. 查询文档
    /**
     * 查询文档
     */
    @Test
    void testGetDocumentById() throws IOException {
        // 1.创建request对象
        GetRequest request = new GetRequest("hotel", "38812");
        // 2.发送请求,得到结果
        GetResponse response = client.get(request, RequestOptions.DEFAULT);
        // 3.解析结果
        String json = response.getSourceAsString();

        HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
        System.out.println(hotelDoc);
    }
3. 更新文档
 /**
     * 更新文档
     */
    @Test
    void testUpdateDocument() throws IOException {
        // 1.创建request对象
        UpdateRequest request = new UpdateRequest("hotel", "38812");
        // 2.准备请求参数
        request.doc(
                "price","952",
                "starName","四钻"
        );
        // 3.发送请求
        client.update(request, RequestOptions.DEFAULT);
    }
4. 删除文档
    /**
     * 删除文档
     */
    @Test
    void testDeleteDocument() throws IOException {
        // 1.创建request对象
        DeleteRequest request = new DeleteRequest("hotel", "38812");
        // 2.发送请求
        client.delete(request,RequestOptions.DEFAULT);
    }

5. 批量导入文档
    /**
     * 批量导入数据
     */
    @Test
    void testBulkRequest() throws IOException {
        // 批量查询酒店数据
        List<Hotel> hotels = hotelService.list();

        for (Hotel hotel : hotels) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
        }

        BulkRequest request = new BulkRequest();

        for (Hotel hotel : hotels) {
            HotelDoc hotelDoc = new HotelDoc(hotel);
            request.add(new IndexRequest("hotel")
                    .id(hotelDoc.getId().toString())
                    .source(JSON.toJSONString(hotelDoc), XContentType.JSON));
        }

        client.bulk(request,RequestOptions.DEFAULT);

    }