ElasticSearch

1.1 ElasticSearch重要概

  • cluster:整个ES默认就是集群状态,整个集群是一份完整、互备的数据

  • node:集群中一个节点,一个进程是一个node

  • shard:分片,即使是一个节点中的数据也会通过hash算法,分成多个片段,默认是5片(7.0默认1片)

  • index:相当于database,对用户来说是一个逻辑数据库,理论上被分为多个shard存放,也可能存放在多个node中

  • type:相当于table,同一json格式的数据集(6.x只允许建一个,7.x废弃,index相当于table)

  • document:相当于row,面向对象里的Object

1.2 ES特点,优点

  • 倒排索引

  • 天然分片

  • 天然集群

  • 天然索引

1.3 ES怎么用的,用来做什么?为什么用ES?ES最大的优点,最适合解决什么样的问题?ES集群是几个节点?

 redismysqlelasticsearchhbasehadoop/hive
容量/容量扩展 较大 海量 海量
查询时效性 极高 中等 较高 中等
查询灵活性 较差 k-v模式 非常好,支持sql 较好,关联查询较弱,但是可以全文检索,DSL语言可以处理过滤、匹配、排序、聚合等各种操作 较差,主要靠rowkey,scan的话性能不行,或者建立二级索引 非常好,支持sql
写入速度 极快 中等 较快 较快
一致性、事务

 

1.4 ES适合的场景:

  1. 检索。ES本身作为一个搜索引擎,用来处理检索的任务再合适不过。你可以在线上项目中直接将内容写入ES以提供检索服务,也可以把以往的数据导入ES以处理特定的需求。

  2. 统计。ES的统计也是基于检索功能的,聚合功能使得统计结果处理起来非常方便。如果你只需要统计而不用检索,可能有其他工具更适合你,比如Spark SQL。

1.5 ES基本操作

  • 查看ES中有哪些索引

 GET /_cat/indices?v
  • 增加索引

 PUT /movie_index
  • 删除索引

 DELETE /movie_index
  • 新增文档(如果之前没有建过index或者type,ES会自动创建)(也是修改文档

 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"}
  ]
 }
 
  • 搜索 type 全部数据

 GET /movie_index/movie/_search
  • 查找指定 id 的 document 数据

 GET /movie_index/movie/1
  • 删除一个document

 DELETE /movie_index/movie/3
  • 按条件查询(全部)

 GET /movie_index/movie/_search
 {
  "query": {
    "match_all": {}
  }
 }
  • 按照字段的分词查询

 GET /movie_index/movie/_search
 {
  "query": {
    "match": {
      "name": "sea"
    }
  }
 }
  • 按照分词子属性查询

 GET /movie_index/movie/_search
 {
  "query": {
    "match": {
      "actorList.name": "zhang"
    }
  }
 }
  • 按照短语查询

 GET /movie_index/movie/_search
 {
  "query": {
    "match_phrase": {
      "name": "operation red"
    }
  }
 }
  • 模糊查询

 GET /movie_index/movie/_search
 {
  "query": {
    "fuzzy": {
      "name": "red"
    }
  }
 }
  • 过滤(查询后过滤)

 GET /movie_index/movie/_search
 {
  "query": {
    "match": {
      "name": "red"
    }
  },
  "post_filter": {
    "term": {
      "actorList.id": "3"
    }
  }
 }
  • 查询前过滤(推荐使用)

 GET movie_index/movie/_search
 {
  "query": {
    "bool": {
      "filter": [
        {
  "term": {"actorList.id": 3}
        },
        {
            "term":{"actorList.id": 1}
        }
      ],
      "must":
  {
  "match": {"name": "zhang"}
  }    
    }
  }
 }
  • 按范围过滤

 GET movie_index/movie/_search
 {
  "query": {
    "bool": {
      "filter": {
        "range": {
          "doubanScore": {
            "gt": 5,
            "lt": 9
          }
        }
      }
    }
  }
 }
  • 排序

 GET movie_index/movie/_search
 {
  "query":{
    "match": {"name":"red operation"}
  }
  , "sort": [
    {
      "doubanScore": {
        "order": "desc"
      }
    }
  ]
 }
  • 分页查询

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

 每个演员参演了多少部电影
 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"
          }
        }
        }
    }
  }
 }



ElasticSearch
ElasticSearch
原文地址:https://www.cnblogs.com/hyunbar/p/12526735.html