一、什么是ES

Elaticsearch,简称为ES, ES是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别的数据。ES也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。

二、ES使用场景

2013年初,GitHub抛弃了Solr,采取ElasticSearch来做PB级的搜索。“GitHub使用ElasticSearch搜索20TB的数据,包括13亿文件和1300亿行代码”。
维基百科:启动以ElasticSearch为基础的核心搜索架构。
SoundCloud:“SoundCloud使用ElasticSearch为1.8亿用户提供即时而精准的音乐搜索服务”。
百度:百度目前广泛使用ElasticSearch作为文本数据分析,采集百度所有服务器上的各类指标数据及用户自定义数据,通过对各种数据进行多维分析展示,辅助定位分析实例异常或业务层面异常。目前覆盖百度内部20多个业务线(包括casio、云分析、网盟、预测、文库、直达号、钱包、风控等),单集群最大100台机器,200个ES节点,每天导入30TB+数据。
新浪使用ES分析处理32亿条实时日志。
阿里使用ES构建自己的日志采集和分析体系。

三、ES主要解决问题

1)检索相关数据
2)返回统计结果
3)速度要快

四、Lucene与ES关系

1)Lucene只是一个库。想要使用它,你必须使用Java来作为开发语言并将其直接集成到你的应用中,更糟糕的是,Lucene非常复杂,你需要深入了解检索的相关知识来理解它是如何工作的。
2)Elasticsearch也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。

ES概述

Elasticsearch是面向文档的,这意味着它可以存储整个对象或文档(document)。然而它不仅仅是存储,还会索引(index)每个文档的内容使之可以被搜索。在Elasticsearch中,你可以对文档(而非成行成列的数据)进行索引、搜索、排序、过滤。

Docker部署ES

1.从阿里云私有仓库拉取镜像
docker pull  registry.cn-shanghai.aliyuncs.com/all-public/elasticsearch:7.17.25
docker pull  registry.cn-shanghai.aliyuncs.com/all-public/kibana:7.17.25

2.安装docker
docker network create elastic
docker run -d --name es01-test --net es -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 -e "discovery.type=single-node" 402d32ac1e34 

3.安装kibana
docker run -d --name kib01-test --net es -p 127.0.0.1:5601:5601 -e "ELASTICSEARCH_HOSTS=http://es01-test:9200" 9b099d33c968 

4.访问kibana地址 http://localhost:5601

curl 来与 ES的 RESTful API 进行通信

      <1>     <2>                   <3>    <4>
curl -XGET 'http://localhost:9200/_count?pretty' -d '
{  <5>
    "query": {
        "match_all": {}
    }
}
'

1.相应的 HTTP 请求方法 或者 变量 : GET, POST, PUT, HEAD 或者 DELETE。
2.集群中任意一个节点的访问协议、主机名以及端口。
3.请求的路径。
4.任意一个查询后再加上 ?pretty 就可以生成 更加美观 的JSON反馈,以增强可读性。
5.一个 JSON 编码的请求主体(如果需要的话)。

冷知识:GET HEAD 不会改变资源请求,PUT具有幂等性、POST没有幂等性

ES API 示例

1.创建索引 PUT有幂等性,再次创建就会返回error
curl -XPUT "http://localhost:9200/shopping?pretty"
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "shopping"
}

2.POST 不具有幂等性但是也无法创建索引,只允许 GET, HEAD, PUT, DELETE
curl -XPOST "http://localhost:9200/shopping?pretty"
{
  "error" : "Incorrect HTTP method for uri [/shopping?pretty] and method [POST], allowed: [GET, HEAD, PUT, DELETE]",
  "status" : 405
}

3.查看索引信息
curl -X GET "http://localhost:9200/shopping?pretty"
{
  "shopping" : {    
    "aliases" : { }, 
    "mappings" : { }, 
    "settings" : { 
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",           #默认1分片
        "provided_name" : "shopping",
        "creation_date" : "1731597959577",  #索引创建时间
        "number_of_replicas" : "1",         #默认1副本
        "uuid" : "6ZN_jq4XRi-Dv3l--KmXog",  #唯一ID
        "version" : {
          "created" : "7172599"
        }
      }
    }
  }
}

4.查看所有index信息 v详细信息 yellow是因为缺少副本分片分配节点
curl -X GET "http://localhost:9200/_cat/indices?v"
health status index                            uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .geoip_databases                 QwGxOxVXS-ubFWrXw0xUwg   1   0         37            0     35.3mb         35.3mb
green  open   .apm-custom-link                 0lAY1gWxSOyqTiJMe1V-aw   1   0          0            0       227b           227b
green  open   .kibana_7.17.25_001              eCh43x2ASKK7a3zYN6LxyQ   1   0         15            0      2.3mb          2.3mb
green  open   .apm-agent-configuration         TC4MJh5rSG28xb8U2OpaTA   1   0          0            0       227b           227b
green  open   .kibana_task_manager_7.17.25_001 d3aZfExmRXe4zcgQYU3DRg   1   0         17         2889    451.7kb        451.7kb
yellow open   shopping                         6ZN_jq4XRi-Dv3l--KmXog   1   1          0            0       227b           227b

5.删除索引
curl -XDELETE "http://localhost:9200/shopping?pretty"
{
  "acknowledged" : true
}

6.向索引文档中添加数据
curl -XPOST "http://localhost:9200/shopping/_doc" -H "Content-type: application/json" -d'{
  "title":"小米手机",
  "category":"小米",
  "images":"http://www.gulixueyuan.com/xm.jpg",
  "price":3999.00
}'
{
  "_index" : "shopping",
  "_type" : "_doc",               
  "_id" : "nMNhK5MB5s028Is5BdJM", #随机iD 不指定
  "_version" : 1,                 
  "result" : "created",
  "_shards" : {
    "total" : 2,                 #返回结果 ms单位
    "successful" : 1,            #成功
    "failed" : 0                 #没有失败
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

7.设置ID内容
curl -X POST "http://localhost:9200/shopping/_doc/1?pretty" -H "Content-type: application/json" -d'{
  "title":"小米手机",
  "category":"小米",
  "images":"http://www.gulixueyuan.com/xm.jpg",
  "price":3999.00
}'
{
  "_index" : "shopping", 
  "_type" : "_doc",
  "_id" : "1",    #返回ID 1
  "_version" : 1, 
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}

8.curl -X PUT "http://localhost:9200/shopping/_create/2?pretty" -H "Content-type: application/json" -d'{
    "title":"小米手机",
    "category":"小米",
    "images":"http://www.gulixueyuan.com/xm.jpg",
    "price":3999.00
}'

_create 来创建文档,但返回的结果中,_type 字段仍然显示为 _doc,从Elasticsearch 7.x 版本开始,文档类型 (_type) 已经被弃用,并且所有文档都默认使用 _doc 作为文档类型。尽管使用了 _create 来确保文档只在不存在时创建,但文档类型仍然使用默认的 _doc。
{
  "_index" : "shopping",
  "_type" : "_doc",  #文档类型没有变化
  "_id" : "2",    #ID 2
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

冷知识:elasticsearch 8x版本中以及移除了_type 类型

9.查询文档ID1的内容
curl -XGET "http://localhost:9200/shopping/_doc/1?pretty"
{
  "_index" : "shopping",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "小米手机",
    "category" : "小米",
    "images" : "http://www.gulixueyuan.com/xm.jpg",
    "price" : 3999.0
  }
}

10.查询文档全部内容
curl -XGET "http://localhost:9200/shopping/_search?pretty"
{
  "took" : 2,   #
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,     #总共4条数据
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "shopping",
        "_type" : "_doc",
        "_id" : "m8NgK5MB5s028Is5u9La",
        "_score" : 1.0,
        "_source" : {
          "title" : "小米手机",
          "category" : "小米",
          "images" : "http://www.gulixueyuan.com/xm.jpg",
          "price" : 3999.0
        }
      },
      ........

今日总结:
1.了解es 是什么、什么场景适用、解决什么问题、为什么使用
2.通过docker 方式简单安装测试环境
3.通过ES API 查询各类方法
4.基础语法学习

更多推荐