ElasticSearch - 分布式全文检索引擎 - 概念介绍及基本操作

目录

零、概念介绍

0-1 内部结构

0-2 可视化工具

0-2-1 Kibana - win下使用

一、安装

1-0 Windows安装

1-1 Windows 下启动

1-2 Linux 下安装

1-3 curl 安装 - 提供一种将请求提交给Elastic的快捷方式

1-4 设置Elastic的远程访问

二、Elastic 的简单操作

三、Plugins 操作(windows环境下)

3-1 Plugins 安装

3-1-1 方式一、通过指定插件名

3-1-2 方式二、通过本地url

3-1-3 方式三、通过HTTP url

3-2 Plugins - Listing, Removing and Updating Installed

四、Index操作

4-1 Index 新建

4-2 Index 删除

 五、Document 操作

5-1 更新文档

​5-2 删除文档

5-3 文档的批量处理


零、概念介绍

简单学习参考

官网

官方文档

基础学习参考

Elasticsearch索引原理

Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.

本质上是一个分布式数据库,允许多台服务器协同工作,每台服务器可以允许多个Elastic实例,单个实例被称为一个节点(node),一组节点构成一个集群(cluster)

当然 Elasticsearch 并不仅仅是 Lucene 那么简单,它不仅包括了全文搜索功能,还可以进行以下工作:

  • 分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索。
  • 可实现亿级数据实时查询
  • 实时分析的分布式搜索引擎。
  • 可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据。

0-1 内部结构

  • Index - 数据管理的顶层单位(单个数据库)
  • Document - Index内的单条记录,多条文档构成了一个Index(Json格式文档)
    • 注意!同一个Index内的Document不要求有相同的结构(scheme),但最好保持相同,有利于提高搜索效率。
  • Type - Index内的虚拟逻辑分组,用来过滤Document
    • 比如weather这个 Index 里面,可以按城市分组(北京和上海),也可以按气候分组(晴天和雨天),这种分组就叫做 Type
    • 不同的Type应该有相似的结构,例如:性质完全不同的数据(比如products和logs)应该存成两个 Index,而不是一个 Index 里面的两个 Type(虽然可以做到)。
    • 注意!Elastic 6.X版本中只允许每个Index包含一个Type,7.X将会彻底删除Type

0-2 可视化工具

参考链接

Kibana - 下载链接

Kibana - 官方使用文档

Grafana - 下载链接

0-2-1 Kibana - win下使用

配置文件修改,将hosts指向elastic开启端口

需要等待一段时间,等终端开始显示完成

使用浏览器访问地址http://localhost:5601

一、安装

官网安装文档

注意:Elasticsearch是用Java开发的,最新版本的Elasticsearch需要安装jdk1.8以上的环境

安装包下载完,解压,进入到bin目录,启动 elasticsearch.bat 即可

1-0 Windows安装

https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.0.msi

windows 建议使用MSI程序包,可以基于GUI界面完成安装

1-1 Windows 下启动

节点成功运行显示

# 使用命令行启动节点和单个集群(注意进入exe目录下)
.\elasticsearch.exe\elasticsearch.exe
​
# 使用命令行启动节点的时候覆盖集群或节点名称
./elasticsearch -Ecluster.name=my_cluster_name -Enode.name=my_node_nameelasticsearch -Ecluster.name=my_cluster_name -Enode.name=my_node_name

1-2 Linux 下安装

# 命令行方式下载
$ wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.7.0.tar.gz
$ tar -xvf elasticsearch-6.7.0.tar.gz
$ cd elasticsearch-6.7.0 / bin
​
# 启动
$ ./elasticsearch
​
# 报错"max virtual memory areas vm.maxmapcount [65530] is too low"
$ sudo sysctl -w vm.max_map_count=262144
​
# 查看默认9200的端口信息
$ curl localhost:9200
{
  "name" : "atntrTf",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "tf9250XhQ6ee4h7YI11anA",
  "version" : {
    "number" : "5.5.1",
    "build_hash" : "19c13d0",
    "build_date" : "2017-07-18T20:44:24.823Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.0"
  },
  "tagline" : "You Know, for Search"
}
​
''' 按下 Ctrl + C,Elastic 就会停止运行。 '''

1-3 curl 安装 - 提供一种将请求提交给Elastic的快捷方式

curl下载链接(注意系统匹配)

curl for win 的安装包

注意!记得 将curl.exe 配置到系统环境变量内

1-4 设置Elastic的远程访问

默认情况下,Elastic 只允许本机访问,如果需要远程访问,可以修改 Elastic 安装目录的config/elasticsearch.yml文件,去掉network.host的注释,将它的值改成0.0.0.0,然后重新启动 Elastic。

network.host: 0.0.0.0

上面代码中,设成0.0.0.0让任何人都可以访问。线上服务不要这样设置,要设成具体的 IP。

二、Elastic 的简单操作

注意!命令必须为双引号!!

# 查看集群健康
curl -X GET "localhost:9200/_cat/indices?v"
​
# 查看集群的节点列表
curl -X GET "localhost:9200/_cat/nodes?v"
​
# 列出所有index
curl -X GET "localhost:9200/_cat/indices?v"
# health status index uuid pri rep docs.count docs.deleted store.size pri.store.size 返回响应表示未存在相关index

三、Plugins 操作(windows环境下)

官方文档Plugins操作

官方文档 - 支持插件查询(非单页)

3-1 Plugins 安装

3-1-1 方式一、通过指定插件名

# 语法
elasticsearch-plugin install [plugin_name]
# 举例
elasticsearch-plugin install analysis-icu

3-1-2 方式二、通过本地url

# 语法
elasticsearch-plugin install [url]
# unix下
elasticsearch-plugin install file:///path/to/plugin.zip
# win下
elasticsearch-plugin install file:///C:/path/to/plugin.zip

3-1-3 方式三、通过HTTP url

elasticsearch-plugin install http://some.domain/path/to/plugin.zip
​
# 插件脚本将拒绝与具有不受信任证书的HTTPS URL通信。
# 要使用自签名HTTPS证书,您需要将CA证书添加到本地Java信任库,并将该位置传递给脚本,如下所示:
#(linux下)
sudo ES_JAVA_OPTS="-Djavax.net.ssl.trustStore=/path/to/trustStore.jks" bin/elasticsearch-plugin install https://host/plugin.zip

3-2 Plugins - Listing, Removing and Updating Installed

# list
elasticsearch-plugin list
​
# rm
elasticsearch-plugin remove [pluginname]
​
# install
elasticsearch-plugin install [pluginname]
​
# update (rm + install)
elasticsearch-plugin remove [pluginname]
elasticsearch-plugin install [pluginname]

四、Index操作

4-1 Index 新建

# 创建 customer 的 index
curl -X PUT "localhost:9200/customer?pretty"
# 查看所有 index
curl -X GET "localhost:9200/_cat/indices?v"

4-2 Index 删除

# 删除指定的index
curl -X DELETE "localhost:9200/customer?pretty"
# 查看所有index
curl -X GET "localhost:9200/_cat/indices?v"

 五、Document 操作

建议在Kibana下使用,不建议使用win的CMD操作(不是很好支持换行等操作)

# 注意!该命令操作都在kibana下实现,若需要强行win下实现,必须使用双引号
​
# 在customer(index)下创建 id 为 1 的 document ,json格式,内容为name
curl -XPUT "http://localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}'
​
PUT /customer/_doc/1?pretty
{
  "name": "John Doe"
}
​
# 查看文档内容
curl -X GET "localhost:9200/customer/_doc/1?pretty"
​
GET /customer/_doc/1?pretty
# 修改文档内字段
curl -XPUT "http://localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "hello"
}'

 5-1 更新文档

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

 

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

# 使用简单脚本进行更新,使用script脚本将年龄更新到到25
POST /customer/_doc/1/_update?pretty
{
  "script" : "ctx._source.age += 5"
}
​
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}
'
GET /customer/_doc/1?pretty
​

 5-2 删除文档

# 删除指定文档2
DELETE /customer/_doc/2?pretty
​
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"
​
GET /customer/_doc/2?pretty

 

 

5-3 文档的批量处理

# 批量调用指定索引的2个文档
POST /customer/_doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

# 更新第一个文档,删除第二个文档
POST /customer/_doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}

猜你喜欢

转载自blog.csdn.net/qq_33961117/article/details/88887885
今日推荐