#Elasticsearch# 文章列表 Elasticsearch 介绍 Elasticsearch 7:快速上手 Elasticsearch 7:关于 Index、Type、Document Elasticsearch 7:安装与启动 Elasticsearch 7:Kibana 的使用 Elasticsearch 7:下载历史版本 Elasticsearch 7:文档唯一性 Elasticsearch 7:默认端口与端口设置 Elasticsearch 7:创建和删除索引 Elasticsearch 7:自定义 mapping 和 settings Elasticsearch 7:设置索引副本数量和分片数量 Elasticsearch 7:查看所有索引 Elasticsearch 7:数据类型 Elasticsearch 7:字符串类型 keyword 、text Elasticsearch 7:数组 Elasticsearch 7:添加和更新文档 Elasticsearch 7:通过 _bulk 批量添加文档 Elasticsearch 7:使用 from 、size 进行分页查询 Elasticsearch 7:查询中使用 sort 进行排序 Elasticsearch 7:查询结果只展示部分字段 Elasticsearch 7:查询结果中展示 _version 字段 Elasticsearch 7:使用 ignore_above 限制字符串长度 Elasticsearch 7:动态映射 Elasticsearch 7:doc_values 属性 Elasticsearch 7:刷新周期 refresh_interval Elasticsearch 7:使用 _refresh 刷新索引 Elasticsearch 7:分片(shard)限制 Elasticsearch 7:使用 _cat thread_pool 查询线程池运行情况 Elasticsearch 7:事务日志 translog Elasticsearch 7:文档 _id 的长度限制 Elasticsearch 7:分片 shard Elasticsearch 7:滚动查询 Elasticsearch 7:聚合查询 Elasticsearch 7:索引模板 Elasticsearch 7:获取文档所属的 shard Elasticsearch 7:获取版本号 Elasticsearch 7:获取指定 shard 中的文档 Elasticsearch 7:获取 shard 统计信息 Elasticsearch 7:搜索实战 Elasticsearch 7:Python 客户端 Elasticsearch 7:Java TransportClient API 客户端 Elasticsearch 7:Java REST Client API 客户端 Elasticsearch:将 SQL 转换为 DSL Elasticsearch 6 快速上手 Elasticsearch 5 快速上手 Elasticsearch 5:禁止自动创建索引 Elasticsearch 5:禁止动态增加字段 Elasticsearch 产品版本支持周期 基于 Elasticsearch 的站内搜索引擎实战

Elasticsearch 5 快速上手


#Elasticsearch#


截止2019-06-28,ES 的最新版本是 7.2.0。

可以进入 https://www.elastic.co/cn/downloads/past-releases 如果要下载旧版本的安装包。

安装 Elasticsearch

前置条件是安装 Java。

然后去 ES 官网下载 ES。下载地址

下载后,解压。

启动:

$ cd elasticsearch-5.6.16
$ ./bin/elasticsearch

使用 curl 操作与 ES 交互

ES 默认用 9200 端口提供 HTTP 交互服务。

查看信息

请求:

$ curl http://localhost:9200

# 响应如下
{
  "name" : "E__dOS_",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "uy2dZgwgToGyW34FY2gr6w",
  "version" : {
    "number" : "5.6.16",
    "build_hash" : "3a740d1",
    "build_date" : "2019-03-13T15:33:36.565Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

创建 Index

使用 HTTP PUT 请求创建 Index:

$ curl -X PUT 'localhost:9200/school?pretty'

# 响应如下
{
    "acknowledged":true,
    "shards_acknowledged":true,
    "index":"school"
}

删除 Index

使用 HTTP DELETE 请求删除 Index:

$ curl -X DELETE 'localhost:9200/school?pretty'

# 响应如下
{
  "acknowledged" : true
}

kibana 入门

kibana 是 ES 的前端,通过它可以快速进行数据操作、查询、集群状态查看等。

安装

到官网下载 kibana,解压。

启动

$ cd kibana-6.6.0
$ ./bin/kibana

kibana 默认与本地 ES 的 9200 端口通信。kibana 提供的默认网页服务端口是 5601。

Dev Tools 的简单使用

在 Dev Tools 中可以以非常简洁的方式实现数据操作。

进入 http://localhost:5601 ,选择 Dev tools。或者直接进入 http://localhost:5601/app/kibana#/dev_tools 。

例如创建索引:

PUT school

点击代表执行的绿色按钮后,右侧显示结果:

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "school"
}

增删查改快速入门

以下均在 Kibana 的 Dev Tools 中测试。

创建索引

如索引名是 school。

PUT school

索引不能重复创建,重复创建时会报错。

删除索引

DELETE school

创建索引,同时创建 type

PUT school
{
  "mappings" : {
    "student" : {
      "properties" : {
        "name" : {
          "type" : "keyword"
        }
      }
    }
  }
}

创建索引后,再创建 type

# 步骤1
PUT school

# 步骤2
POST school/student/_mapping
{
  "properties" : {
    "name" : {
      "type" : "keyword"
    }
  }
}

在 ES 5 中,虽然一个index下面,可以有多个type,但是多个type相同名称的字段的类型必须相同,否则创建时会报错。

获取索引下的所有类型

# 请求
GET /school/_mapping

# 响应
{
  "school": {
    "mappings": {
      "student": {
        "properties": {
          "name": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

删除某个索引下的某个 type / mapping

不支持。可以把索引删除,然后重建。


( 本文完 )