1.26W字 22图详解!ElasticSearch 最全详细使用教程(上)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 1.26W字 22图详解!ElasticSearch 最全详细使用教程

本文介绍了ElasticSearch的必备知识:从入门、索引管理到映射详解。

、快速入门1. 查看集群的健康状况http://localhost:9200/_cat

image.png

http://localhost:9200/_cat/health?v

image.png说明:v是用来要求在结果中返回表头


状态值说明


Green - everything is good (cluster is fully functional),即最佳状态

Yellow - all data is available but some replicas are not yet allocated (cluster is fully functional),即数据和集群可用,但是集群的备份有的是坏的

Red - some data is not available for whatever reason (cluster is partially functional),即数据和集群都不可用


查看集群的节点

http://localhost:9200/_cat/?v

image.png2. 查看所有索引http://localhost:9200/_cat/indices?v

image.png3. 创建一个索引创建一个名为 customer 的索引。pretty要求返回一个漂亮的json 结果

PUT /customer?pretty

image.png再查看一下所有索引

http://localhost:9200/_cat/indices?v

image.pngGET /_cat/indices?v

image.png4. 索引一个文档到customer索引中

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

5. 从customer索引中获取指定id的文档

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

6. 查询所有文档

GET /customer/_search?q=*&sort=name:asc&pretty

JSON格式方式

GET /customer/_search
{
  "query": { "match_all": {} },
  "sort": [
    {"name": "asc" }
  ]
}

二、索引管理

image.png1. 创建索引
创建一个名为twitter的索引,设置索引的分片数为3,备份数为2。注意:在ES中创建一个索引类似于在数据库中建立一个数据库(ES6.0之后类似于创建一个表)

PUT twitter
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,
            "number_of_replicas" : 2
        }
    }
}

说明:

默认的分片数是5到1024

默认的备份数是1

索引的名称必须是小写的,不可重名


创建结果:

image.png创建的命令还可以简写为

PUT twitter
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}

2. 创建mapping映射

注意:在ES中创建一个mapping映射类似于在数据库中定义表结构,即表里面有哪些字段、字段是什么类型、字段的默认值等;也类似于solr里面的模式schema的定义

PUT twitter
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,
            "number_of_replicas" : 2
        }
    },
   "mappings" : {
        "type1" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}

3. 创建索引时加入别名定义

PUT twitter
{
    "aliases" : {
        "alias_1" : {},
        "alias_2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        }
    }
}

4. 创建索引时返回的结果说明

image.png

5. Get Index 查看索引的定义信息

GET /twitter,可以一次获取多个索引(以逗号间隔) 获取所有索引 _all 或 用通配符*

image.pngGET /twitter/_settings

image.pngGET /twitter/_mapping

image.png 6. 删除索引

DELETE /twitter

说明:

可以一次删除多个索引(以逗号间隔) 删除所有索引 _all 或 通配符 *

7. 判断索引是否存在

HEAD twitter

image.png

HTTP status code 表示结果 404 不存在 , 200 存在

8. 修改索引的settings信息

索引的设置信息分为静态信息和动态信息两部分。静态信息不可更改,如索引的分片数。动态信息可以修改。

REST 访问端点:
/_settings 更新所有索引的。
{index}/_settings 更新一个或多个索引的settings。

详细的设置项请参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index-modules.html#index-modules-settings

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
4月前
|
存储 自然语言处理 网络协议
【elastic search】下载安装、使用教程
【elastic search】下载安装、使用教程
61 1
|
3月前
|
Java 索引 Spring
教程:Spring Boot中集成Elasticsearch的步骤
教程:Spring Boot中集成Elasticsearch的步骤
|
4月前
|
搜索推荐 Java 索引
教程:Spring Boot中集成Elasticsearch的步骤
教程:Spring Boot中集成Elasticsearch的步骤
|
10月前
|
Java Linux 数据安全/隐私保护
百度搜索:蓝易云【centos7系统安装elasticsearch8.7.0,并设置密码访问教程。】
现在,您已经成功安装并设置密码访问Elasticsearch 8.7.0。您可以使用设置的密码来访问和管理Elasticsearch实例。
199 1
|
5月前
|
安全 搜索推荐 定位技术
一张图30个知识点,全方位认知 Elasticsearch 技术发展
一张图30个知识点,全方位认知 Elasticsearch 技术发展
81 3
|
5月前
|
机器学习/深度学习 存储 计算机视觉
Elasticsearch 8.X “图搜图”实战
Elasticsearch 8.X “图搜图”实战
107 0
|
11月前
|
自然语言处理 关系型数据库 定位技术
分布式系列教程(35) -ElasticSearch文档映射
分布式系列教程(35) -ElasticSearch文档映射
69 0
|
11月前
|
JSON 自然语言处理 数据格式
分布式系列教程(33) -ElasticSearch DSL语言查询与过滤
分布式系列教程(33) -ElasticSearch DSL语言查询与过滤
181 0
|
11月前
分布式系列教程(32) -ElasticSearch条件查询
分布式系列教程(32) -ElasticSearch条件查询
190 0
|
11月前
|
自然语言处理 搜索推荐 索引
分布式系列教程(31) -ElasticSearch倒排索引
分布式系列教程(31) -ElasticSearch倒排索引
83 0
下一篇
无影云桌面