ElasticSearch 基本介绍 | 青训营笔记

73 阅读3分钟

ElasticSearch 基本介绍

1. 简介

ElasticSearch 是一个基于 Lucene 的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于 RESTful web 接口。Elasticsearch 是用 Java 语言开发的,并作为 Apache 许可条款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch 是一个分布式、可扩展、实时的搜索与数据分析引擎。它能从项目一开始就赋予你的数据以搜索、分析和探索的能力。它被用作全文检索、结构化搜索、分析以及这三个功能的组合。

2. 安装

使用 Docker 安装

docker pull elasticsearch:7.6.2

docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2

3. 基本概念

3.1. 索引(index)

索引是 Elasticsearch 中最重要的概念之一,它是一个存储关联到一个特定对象的一起的文档的地方。索引是一个逻辑命名空间,它指向一个或多个物理分片。索引的名字必须全部是小写的,不能以下划线开头,不能包含逗号。

3.2. 类型(type)

在 Elasticsearch 中,类型是索引的一个逻辑分类/分区,它通常用于存储具有相似结构的文档。一个索引可以定义一个或多个类型。一个类型可以定义一个或多个映射。一个映射定义了一个文档的字段,每个字段的类型和它如何被存储和索引。一个类型也可以定义一个或多个分析器。一个分析器定义了如何分析一个字符串以产生一系列的词条。

3.3. 文档(document)

文档是可以被索引的基础信息单元。例如,你可以有一个文档,它代表一个单独的客户。这个文档有一个类型,它定义了这个文档的结构。在这个文档中,你可以定义一个字段,它包含客户的名字,另一个字段,它包含客户的年龄,等等。文档是用 JSON 格式表示的。

4. 基本操作

4.1. 创建索引

curl -X PUT "localhost:9200/customer?pretty"

4.2. 查看索引

curl -X GET "localhost:9200/_cat/indices?v"

4.3. 删除索引

curl -X DELETE "localhost:9200/customer?pretty"

4.4. 创建文档

curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}

4.5. 查看文档

curl -X GET "localhost:9200/customer/_doc/1?pretty"

4.6. 删除文档

curl -X DELETE "localhost:9200/customer/_doc/1?pretty"

4.7. 更新文档

curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Jane Doe" }
}

5. 搜索

5.1. 基本搜索

curl -X GET "localhost:9200/customer/_search?q=*&pretty"

5.2. 指定字段搜索

curl -X GET "localhost:9200/customer/_search?q=last_name:Smith&pretty"

5.3. 指定多个字段搜索

curl -X GET "localhost:9200/customer/_search?q=first_name:John&pretty"

6. Go 操作

6.1. 安装

go get github.com/olivere/elastic/v7

6.2. 创建客户端

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/olivere/elastic/v7"
)

func main() {
    // 创建客户端
    client, err := elastic.NewClient(elastic.SetURL("http://
localhost:9200"))
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("client:", client)
}

6.3. 创建索引

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/olivere/elastic/v7"
)

func main() {
    // 创建客户端
    client, err := elastic.NewClient(elastic.SetURL("http://
localhost:9200"))

    // 创建索引
    _, err = client.CreateIndex("customer").Do(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("create index success")
}

6.4. 插入文档

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/olivere/elastic/v7"
)

func main() {
    // 创建客户端
    client, err := elastic.NewClient(elastic.SetURL("http://
localhost:9200"))

    // 创建索引
    _, err = client.CreateIndex("customer").Do(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("create index success")

    // 插入文档
    type Customer struct {
        Name string `json:"name"`
    }
    customer := Customer{Name: "John Doe"}
    _, err = client.Index().Index("customer").BodyJson(customer).Do(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("insert document success")
}

6.5. 查询文档

package main

import (
    "context"
    "fmt"
    "log"

    "github.com/olivere/elastic/v7"
)

func main() {
    // 创建客户端
    client, err := elastic.NewClient(elastic.SetURL("http://
localhost:9200"))

    // 创建索引
    _, err = client.CreateIndex("customer").Do(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("create index success")

    // 插入文档
    type Customer struct {
        Name string `json:"name"`
    }
    customer := Customer{Name: "John Doe"}
    _, err = client.Index().Index("customer").BodyJson(customer).Do(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("insert document success")

    // 查询文档
    res, err := client.Get().Index("customer").Id("1").Do(context.Background())
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("get document success")
    fmt.Println(res)
}