ElasticSerach(二)

1、ElasticSearch 基础概念

1.1、近实时(Near Realtime/NRT)

ElasticSearch 是一个近实时的搜索平台。从生成文档索引到文档可搜索,有一个轻微的延迟(通常1s)

1.2、集群(Cluster).

ES 默认就是集群状态,整个集群是一份完整、互备的数据。集群是一个多节点(服务器) 的集合。集群中的节点一起存储数据,对外提供搜索功能。集群有一个唯一的名称标识,即有一个集群名称,可以通过 elasticsearch/config/elasticsearch.yml  里的cluster.name字段进行配置。集群名称很重要,节点都是通过集群名称加入集群的。奇骏名称不能其他ES集群名称重名,取名一般要有明确含义,以免引起混乱。另外集群节点数不受限制,一个节点也可以是一个集群。

1.3、节点(Node)

节点是一个服务器,,属于某个集群。节点用于存储数据,参与集群的索引和搜索功能。与集群一样,节点也是通过名称来标识的。默认情况下,启东集群时会分配给节点一个UUID作为名称。如果需要,可以给节点取名,

1.4、索引(Index)

索引是具有某种意义的文档集合,相当于一本书的目录。例如,可以为客户数据简历索引,为订单数据建议另一个索引。索引由名称标识(小写),可以使用该名称,对索引中的文档进行简历索引、搜索、更新和删除操作。一个集群内,索引数量不受限制。

1.5、类型(Type)

类似于rdbms 的 table,但是与其说是table,其实更像面向对象的class,同一个JSON的格式的数据集合。(6.x 版本只许建一个,7.0废弃,index 其实相当于table级别的感念)

1.6、文档(Document)

文档是可以通过索引的基本信息单元,相当于书的章节。例如;可以为单个客户创建一个文档,为单个订单创建另一个文档,文档用JSON表示。在索引中,理论上可以存储任意数量的文档。类似于rdbms 的row,面向对象的object。

1.7、分片和副本(Shards & Replicas)

索引可能存储大量数据,数据量可能超过单个节点的硬件限制。例如,一个索引包含10亿个文档,将占用1TB的磁盘空间,单个节点的磁盘放不下,ES 提供了索引分片的功能,创建索引时,可以定义所需的分片数量。每个分片本身就是一个功能齐全,独立的索引,可以托管在集群中任一节点上。分片之所以重要有两点:

  • 允许水平切分内容,以便内容可以存储到put服务器上
  • 允许跨分片操作,提高性能/吞吐量

分片如何部署,如何跨片搜索完全由ES管理,对外透明。网络环境随时可能出现故障,如果某个分片由于某种原因消失,那么使用故障转移机制是非常有用的,ES 允许分片创建副本副本之所以重要,主要有2个原因:

  • 在分片/节点失败时提供高可用性。因此,原分片与副本不应放在同一个节点上。
  • 扩展吞吐量,因为可以在所有副本上并行执行搜索。

总而言之,索引可以分片,索引分片可以创建副本。复制后,每个索引将具有主分片与副本分片。创建索引时,可以为每个索引定义分片和副本的数量。之后,还可以随时动态更改副本数量。您可以使用_shrink和_split api更改现有索引的分片数量,但动态修改副本数量相当麻烦,最好还是预先计划好分片数量。默认情况下,Elasticsearch中的每个索引分配一个主分片和一个副本(7.X之前,默认是5片,副本是0。7.X默认改为1片,副本为1)。如果集群中有两个节点,就可以将索引主分片部署在一个节点,副本分片放在另一个节点,提高可用性。

1.8、ES 概念和 Mysql 对比

MySQL

ES5.X

ES6.X

ES7.X

Database

Index

 

 

Table

Type

Index(Type成了摆设)

Index(Type被移除掉)

Row

Document

Document

 

Column

Field

Field

 

 

例如有一下实体

public class  Movie {
       String id;
     String name;
     Double doubanScore;
     List<Actor> actorList;
}

public class Actor{
String id;
String name;
}

这两个对象如果放在关系型数据库保存,会被拆成2张表,但是ElasticSearch是用一个json来表示一个document。类似豆瓣某个电影详情页 https://movie.douban.com/ 保存到ES中应该是

{
  "id":"1",
  "name":"operation red sea",
  "doubanScore":"8.5",
  "actorList":[  
{"id":"1","name":"zhangyi"},
{"id":"2","name":"haiqing"},
{"id":"3","name":"zhanghanyu"}
] 
}

2、ElasticSearch RestFulAPI(DSL)

DSL全称 Domain Specific language,即特定领域专用语言

2.1、全局操作

2.1.1、查询集群健康状态

#全局展示
GET /_cat/health
#全局展示带表头
GET /_cat/health?v

结果

集群的健康状态有红、黄、绿三个状态:

  • 绿–一切正常(集群功能齐全)
  • 黄–所有数据可用,但有些副本尚未分配(集群功能完全)
  • 红–有些数据不可用(集群部分功能)

 2.1.2、查询集群节点状态

#查看节点情况
GET /_cat/nodes?v
结果

 

2.2、索引操作

2.2.1、查询索引状态

#查看索引状态
GET /_cat/indices?v

 

 

 

字段解释 

ES默认存在的一些索引
health
green:集群完整,yellow:单点正常,集群不完整,red:集群并与正常
status
是否能使用
index
索引名称
uuid
索引ID
pri
主节点分片
rep
副本数
docs.deleted
文档被删了多少
docs.count
文档数
docs.size
整体空间大小
pri.store.size
主节点站空间大小

2.2.2、创建索引

#创建索引
PUT /movie_index

结果

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "movie_index"
}
使用PUT创建名为“movie_index”的索引。末尾追加pretty,可以漂亮地打印JSON响应(如果有的话)。红色警告说在7.x分片数会由默认的5改为1,我们忽略即可索引名命名要求:

2.2.3、查询索引分片情况

#查看分片情况
GET /_cat/shards/movie_index?v

结果

ndex       shard prirep state   docs store ip              node
movie_index 3     p      STARTED    0  230b 192.168.124.141 node-1
movie_index 3     r      STARTED    0  230b 192.168.124.143 node-3
movie_index 1     p      STARTED    0  230b 192.168.124.142 node-2
movie_index 1     r      STARTED    0  230b 192.168.124.141 node-1
movie_index 4     p      STARTED    0  230b 192.168.124.142 node-2
movie_index 4     r      STARTED    0  230b 192.168.124.141 node-1
movie_index 2     r      STARTED    0  230b 192.168.124.142 node-2
movie_index 2     p      STARTED    0  230b 192.168.124.143 node-3
movie_index 0     p      STARTED    0  230b 192.168.124.141 node-1
movie_index 0     r      STARTED    0  230b 192.168.124.143 node-3
默认5个分片,1个副本。所以看到一共有10个分片,5个主,每一个主分片对应一个副本,注意:同一个分片的主和副本肯定不在同一个节点上 

3.2.4、删除索引

#删除
DELETE movie_index

响应

{
  "acknowledged" : true
}

2.3、文档操作

2.3.1、创建文档

#创建文档
PUT /movie_index/movie/1
{
   "id":100,
   "name":"operation red sea",
   "doubanScore":8.5,
   "actorlist":
   [
     {"id":1,"name":"zhang yi"},
      {"id":2,"name":"hai qing"},
       {"id":3,"name":"zhang han yu"}
     ]
}

PUT /movie_index/movie/2
{
  "id":101,
  "name":"peration meigong river",
     "doubanScore": 8.1,
   "actorList":
   [
     {"id":1,
       "name":"zhang han yu"
     }]
}


PUT /movie_index/movie/3
{
    "id": 300,
    "name": "incident red sea",
    "doubanScore": 5.0,
    "actorList": [{
        "id": 4,
        "name": "zhang san feng"
    }]
}
注意,Elasticsearch并不要求,先要有索引,才能将文档编入索引。创建文档时,如果指定索引不存在,将自动创建。默认创建的索引分片是5,副本是1,我们创建的文档会在其中的某一个分片上存一份,副本上存一份,所以看到的响应_shards-total:2

2.3.2、查询文档

查看全部文档

GET /movie_index/_search

结果

{
  "took" : 93,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "id" : 101,
          "name" : "peration meigong river",
          "doubanScore" : 8.1,
          "actorList" : [
            {
              "id" : 1,
              "name" : "zhang han yu"
            }
          ]
        }
      },
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : 100,
          "name" : "operation red sea",
          "doubanScore" : 8.5,
          "actorlist" : [
            {
              "id" : 1,
              "name" : "zhang yi"
            },
            {
              "id" : 2,
              "name" : "hai qing"
            },
            {
              "id" : 3,
              "name" : "zhang han yu"
            }
          ]
        }
      },
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "id" : 300,
          "name" : "incident red sea",
          "doubanScore" : 5.0,
          "actorList" : [
            {
              "id" : 4,
              "name" : "zhang san feng"
            }
          ]
        }
      }
    ]
  }
}
View Code
查询某个索引指定文档
GET /movie_index/movie/2

结果

#结果
{
  "_index" : "movie_index",
  "_type" : "movie",
  "_id" : "2",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,#查询到了数据
  "_source" : {
    "id" : 101,
    "name" : "peration meigong river",
    "doubanScore" : 8.1,
    "actorList" : [
      {
        "id" : 1,
        "name" : "zhang han yu"
      }
    ]
  }
}
#查询一个不存咋的文档
GET /movie_index/movie/21
#结果
{
  "_index" : "movie_index",
  "_type" : "movie",
  "_id" : "21",
  "found" : false #表示未查询到结果
}
View Code
这里有一个字段found为真,表示找到了一个ID为1的文档,另一个字段_source,该字段返回完整JSON文档

2.3.3、根据ID删除文档

##删除整个文档G 
DELETE /movie_index
#删除指定ID文档
DELETE /movie_index/movie/3

2.3.4、更新文档

PUT /movie_index/movie/3
{
    "id": 300,
    "name": "incident red sea",
    "doubanScore": 5.0,
    "actorList": [{
        "id": 4,
        "name": "风清扬"
    }]
}

结果

#返回结果
{
  "_index" : "movie_index",
  "_type" : "movie",
  "_id" : "3",
  "_version" : 3,
  "result" : "updated",#更新操作
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 2,
  "_primary_term" : 1
}
查看更新后的内容
GET /movie_index/movie/3
{
  "_index" : "movie_index",
  "_type" : "movie",
  "_id" : "3",
  "_version" : 3,
  "_seq_no" : 2,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "id" : 300,
    "name" : "incident red sea",
    "doubanScore" : 5.0,
    "actorList" : [
      {
        "id" : 4,
        "name" : "风清扬"
      }
    ]
  }
}
View Code
#POST 不指定ID 会插入数据,ID随机生成,新增操作 不保证幂等性,指定ID可以
POST /movie_index/movie/
{"id":300,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[ 
  {"id":4,"name":"zhang cuishan"
  }
  ]
}
#更新某个文档的部分数据
POST /movie_index/movie/1/_update
{
  "doc":{"name":"红海行动"}
}
#根据ID更新文档
POST /movie_index/movie/2/_update
{
  "doc":{"name":"湄公河行动"}
}
#删除文档属性
POST /movie_index/movie/1/_update
{
"script" : "ctx._source.remove('name')"
}
#删除文档
POST/movie_index /_delete_by_query
{
"query": {"match_all": {}}
}
#批处理:新增两条记录
POST /movie_index/movie/_bulk
{"index":{"_id":66}}
{"id":300,"name":"incident red sea","doubanScore":5.0,"actorList":[{"id":4,"name":"zhang cuishan"}]}
{"index":{"_id":88}}
{"id":300,"name":"incident red sea","doubanScore":5.0,"actorList":[{"id":4,"name":"zhang cuishan"}]}   
#批处理:更新一条记录 删除一条记录
POST /movie_index/movie/_bulk
{"update":{"_id":"66"}}
{"doc": { "name": "wudangshanshang" } }
{"delete":{"_id":"88"}}

2.4、查询操作

2.4.1、查询全部数据

#查询文档结构
GET /movie_index
#查询全部数据
GET /movie_index/_search

2.4.2、按条件查询(全部数据)

#JSON 形式查询全部
GET /movie_index/_search
{
  "query":{
    "match_all":{}
  }
}
查询效果等价于 GET /movie_index/_search

2.4.3、按分词查询(必须使用分词 text 类型)

GET /movie_index/_search
{
  "query": {
    "match": {
      "name": "operation red sea"
    }
  }
}

结果

{
  "took" : 43,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.8630463,
    "hits" : [
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "1",
        "_score" : 0.8630463,
        "_source" : {
          "id" : 100,
          "name" : "operation red sea",
          "doubanScore" : 8.5,
          "actorlist" : [
            {
              "id" : 1,
              "name" : "zhang yi"
            },
            {
              "id" : 2,
              "name" : "hai qing"
            },
            {
              "id" : 3,
              "name" : "zhang han yu"
            }
          ]
        }
      },
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "id" : 300,
          "name" : "incident red sea",
          "doubanScore" : 5.0,
          "actorList" : [
            {
              "id" : 4,
              "name" : "风清扬"
            }
          ]
        }
      }
    ]
  }
}
View Code

2.4.4、按分词子属性查询

GET /movie_index/_search
{
  "query": {
    "match": {
      "actorList.name": "zhang han yu"
    }
  }
}
GET movie_index/movie/_search
{
  "query":{
    "match": {"actorList.name":"zhang han yu"}
  }
}

结果

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.8630463,
    "hits" : [
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "2",
        "_score" : 0.8630463,
        "_source" : {
          "id" : 101,
          "name" : "peration meigong river",
          "doubanScore" : 8.1,
          "actorList" : [
            {
              "id" : 1,
              "name" : "zhang han yu"
            }
          ]
        }
      }
    ]
  }
}
View Code

2.4.5、按短语查询 类似于 like

GET /movie_index/movie/_search
 {
   "query": {
     "match_phrase": {
       "actorList.name": "zhang han yu"
     }
   }

结果

{
  "took" : 22,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.8630463,
    "hits" : [
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "2",
        "_score" : 0.8630463,
        "_source" : {
          "id" : 101,
          "name" : "peration meigong river",
          "doubanScore" : 8.1,
          "actorList" : [
            {
              "id" : 1,
              "name" : "zhang han yu"
            }
          ]
        }
      }
    ]
  }
}
View Code
模糊匹配
 GET /movie_index/_search
{
  "query": {
    "match": {
      "actorList.name": "zhang han"
    }
  }
}

结果

{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "2",
        "_score" : 0.5753642,
        "_source" : {
          "id" : 101,
          "name" : "peration meigong river",
          "doubanScore" : 8.1,
          "actorList" : [
            {
              "id" : 1,
              "name" : "zhang han yu"
            }
          ]
        }
      },
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "3",
        "_score" : 0.2876821,
        "_source" : {
          "id" : 300,
          "name" : "incident red sea",
          "doubanScore" : 5.0,
          "actorList" : [
            {
              "id" : 4,
              "name" : "zhang san feng"
            }
          ]
        }
      }
    ]
  }
}
View Code

2.4.6、精准搜索匹配

GET /movie_index/movie/_search
{
  "query": {
    "term": {
      "actorList.name.keyword": "zhang han yu"
   }
  }
}
actorList.name.keyword 表示不使用分词字符串进行精准匹配

结果

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "2",
        "_score" : 0.2876821,
        "_source" : {
          "id" : 101,
          "name" : "peration meigong river",
          "doubanScore" : 8.1,
          "actorList" : [
            {
              "id" : 1,
              "name" : "zhang han yu"
            }
          ]
        }
      }
    ]
  }
}

精准匹配

GET /movie_index/_search
{
  "query": {
    "term": {
      "actorList.name.keyword": "zhang han"
      
    }
  }

2.4.7、容错匹配 fuzzy

#容错普i配
GET /movie_index/_search
{
  "query": {
    "fuzzy": {
      "name":  "ref" 
    }
  }
}

结果

结果 容错:及时匹配项有一点出入,也会匹配出结果
{
  "took" : 33,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.19178805,
    "hits" : [
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "1",
        "_score" : 0.19178805,
        "_source" : {
          "id" : 100,
          "name" : "operation red sea",
          "doubanScore" : 8.5,
          "actorlist" : [
            {
              "id" : 1,
              "name" : "zhang yi"
            },
            {
              "id" : 2,
              "name" : "hai qing"
            },
            {
              "id" : 3,
              "name" : "zhang han yu"
            }
          ]
        }
      },
      {
        "_index" : "movie_index",
        "_type" : "movie",
        "_id" : "3",
        "_score" : 0.19178805,
        "_source" : {
          "id" : 300,
          "name" : "incident red sea",
          "doubanScore" : 5.0,
          "actorList" : [
            {
              "id" : 4,
              "name" : "zhang san feng"
            }
          ]
        }
      }
    ]
  }
}
View Code

 

posted @ 2022-04-28 16:35  晓枫的春天  阅读(40)  评论(0编辑  收藏  举报