ES笔记

2021-02-25  本文已影响0人  安申

1. es 封装内部 lucene jar 包,基于 lucene API 进行检索查询

2. ElasticSearch 与数据库的类比

关系型数据库(比如Mysql) 非关系型数据库(Elasticsearch)
数据库Database 索引Index
表Table 类型Type(6.0版本之后在一个索引下面只能有一个,7.0版本之后取消了Type)
数据行Row 文档Document(JSON格式)
数据列Column 字段Field
约束 Schema 映射Mapping

3.查看es中有哪些索引

GET /_cat/indices?v

4.表头的含义

key value
health green(集群完整) yellow(单点正常、集群不完整) red(单点不正常)
status 是否能使用
index 索引名
uuid 索引统一编号
pri 主节点几个
rep 从节点几个
docs.count 文档数
docs.deleted 文档被删了多少
store.size 整体占空间大小
pri.store.size 主节点占空间大小

5.操作说明

(1)增加一个索引

PUT /movie_index

(2)删除一个索引

ES 是不删除也不修改任何数据的,而是增加版本号
DELETE /movie_index

(3)代码范例

1)格式 PUT /index/type/id

PUT /movie_index/movie/1
{ "id":1,
  "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":2,
  "name":"operation meigong river",
  "doubanScore":8.0,
  "actorList":[  
{"id":3,"name":"zhang han yu"}
]
}

PUT /movie_index/movie/3
{
  "id":3,
  "name":"incident red sea",
>  "doubanScore":5.0,
  "actorList":[  
{"id":4,"name":"zhang chen"}
]
}

如果之前没建过index或者type,es 会自动创建。

(4)直接用id查找

GET movie_index/movie/1

(5)修改—整体替换

PUT /movie_index/movie/3
{
  "id":"3",
  "name":"incident red sea",
  "doubanScore":"5.0",
  "actorList":[  
{"id":"1","name":"zhang chen"}
]
}

(6)修改—某个字段

POST movie_index/movie/3/_update
{ 
  "doc": {
    "doubanScore":"7.0"
  }
}

(7)删除一个document

DELETE movie_index/movie/3

(8)搜索type全部数据

GET movie_index/movie/_search

结果解析
{
  "took": 2,    //耗费时间 毫秒
  "timed_out": false, //是否超时
  "_shards": {
    "total": 5,   //发送给全部5个分片
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,  //命中3条数据
    "max_score": 1,   //最大评分
    "hits": [  // 结果
      {
        "_index": "movie_index",
        "_type": "movie",
        "_id": 2,
        "_score": 1,
        "_source": {
          "id": "2",
          "name": "operation meigong river",
          "doubanScore": 8.0,
          "actorList": [
            {
              "id": "1",
              "name": "zhang han yu"
            }
          ]
        }
          ...
        ...
      }

(9)按条件查询(全部)

GET movie_index/movie/_search
{
  "query":{
    "match_all": {}
  }
}

(10)按分词查询

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

(11)按分词子属性查询

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

(12)match_phrase

GET movie_index/movie/_search
{
    "query":{
      "match_phrase": {"name":"operation red"}
    }
}

按短语查询,不再利用分词技术,直接用短语在原始数据中匹配

(13)fuzzy查询

GET movie_index/movie/_search
{
    "query":{
      "fuzzy": {"name":"rad"}
    }
}

校正匹配分词,当一个单词都无法准确匹配,es通过一种算法对非常接近的单词也给与一定的评分,能够查询出来,但是消耗更多的性能。

(14)过滤--查询后过滤

GET movie_index/movie/_search
{
    "query":{
      "match": {"name":"red"}
    },
    "post_filter":{
      "term": {
        "actorList.id": 3
      }
    }
}

过滤出actorList.id=3的那条数据

(15)过滤--查询前过滤(推荐使用)

GET movie_index/movie/_search
{ 
    "query":{
        "bool":{
          "filter":[ {"term": {  "actorList.id": "1"  }},
                     {"term": {  "actorList.id": "3"  }}
           ], 
           "must":{"match":{"name":"red"}}
         }
    }
}

(16)过滤--按范围过滤

GET movie_index/movie/_search
{
   "query": {
     "bool": {
       "filter": {
         "range": {
            "doubanScore": {"gte": 8}
         }
       }
     }
   }
}

关于范围操作符:

gt 大于
lt 小于
gte 大于等于 great than or equals
lte 小于等于 less than or equals

(17)排序

GET movie_index/movie/_search
{
  "query":{
    "match": {"name":"red sea"}
  }
  , "sort": [
    {
      "doubanScore": {
        "order": "desc"
      }
    }
  ]
}

(18)分页查询

GET movie_index/movie/_search
{
  "query": { "match_all": {} },
  "from": 1,
  "size": 1
}

(19)指定查询的字段

GET movie_index/movie/_search
{
  "query": { "match_all": {} },
  "_source": ["name", "doubanScore"]
}

(20)高亮

GET movie_index/movie/_search
{
    "query":{
      "match": {"name":"red sea"}
    },
    "highlight": {
      "fields": {"name":{} }
    }
}

(21)聚合

取出每个演员共参演了多少部电影

GET movie_index/movie/_search
{ 
  "aggs": {
    "groupby_actor": {
      "terms": {
        "field": "actorList.name.keyword"  
      }
    }
  }
}

每个演员参演电影的平均分是多少,并按评分排序

GET movie_index/movie/_search
{ 
  "aggs": {
    "groupby_actor_id": {
      "terms": {
        "field": "actorList.name.keyword" ,
        "order": {
          "avg_score": "desc"
          }
      },
      "aggs": {
        "avg_score":{
          "avg": {
            "field": "doubanScore" 
          }
        }
       }
    } 
  }
}
上一篇 下一篇

猜你喜欢

热点阅读