当前位置:   article > 正文

elasticsearch restful api (DSL)_es查看所有索引

es查看所有索引


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

查看es中有哪些索引

GET /_cat/indices?v
  • 1

es 中会默认存在一个名为.kibana的索引

增加一个索引

PUT /movie_index
  • 1

删除一个索引

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

DELETE /movie_index
  • 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"}
]
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

直接用id查找

GET movie_index/movie/1
  • 1

修改—整体替换

和新增没有区别 要求:必须包括全部字段

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

修改—某个字段

POST movie_index/movie/3/_update
{ 
  "doc": {
    "doubanScore":"7.0"
  } 
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

删除一个document

DELETE movie_index/movie/3
  • 1

搜索type全部数据

GET movie_index/movie/_search
  • 1

结果

{
  "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"
            }
          ]
        }
          。。。。。。。。
          。。。。。。。。
      }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

按条件查询(全部)

GET movie_index/movie/_search
{
  "query":{
    "match_all": {}
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

分词查询

GET movie_index/movie/_search
{
  "query":{
    "match": {"name":"red"}
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

按分词子属性查询

GET movie_index/movie/_search
{
  "query":{
    "match": {"actorList.name":"zhang"}
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

match phrase

GET movie_index/movie/_search
{
    "query":{
      "match_phrase": {"name":"operation red"}
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

fuzzy查询

GET movie_index/movie/_search
{
    "query":{
      "fuzzy": {"name":"rad"}
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

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

过滤–查询后过滤

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

过滤–查询前过滤(推荐使用)

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

过滤–按范围过滤

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

关于范围操作符:
| | |
|–|–|–|–|
|gt | 大于 | | |
it|小于 | |
|gte |大于等于 great than or equals |
|lte|小于等于 less than or equals|

排序

如果不成功把doubanScore改成doubanScore.keyword

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

分页查询

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

第二页 每页10行 from = (pageNo-1)*size
(2-1) * 10(size) = 10(from)

指定查询的字段

GET movie_index/movie/_search
{
  "query": { "match_all": {} },
  "_source": ["name", "doubanScore"]
}
  • 1
  • 2
  • 3
  • 4
  • 5

高亮

GET movie_index/movie/_search
{
    "query":{
      "match": {"name":"red sea"}
    },
    "highlight": {
      "fields": {"name":{} }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

聚合

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

GET movie_index/movie/_search
{ 
  "aggs": {
    "groupby_actor": {
      "terms": {
        "field": "actorList.name.keyword"  
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

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

GET movie_index/movie/_search
{ 
  "aggs": {
    "groupby_actor_id": {
      "terms": {
        "field": "actorList.name.keyword" ,
        "order": {
          "avg_score": "desc"
          }
      },
      "aggs": {
        "avg_score":{
          "avg": {
            "field": "doubanScore" 
          }
        }
       }
    } 
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

聚合时为何要加 .keyword后缀?
.keyword 是某个字符串字段,专门储存不分词格式的副本 ,在某些场景中只允许只用不分词的格式,比如过滤filter 比如 聚合aggs, 所以字段要加上.keyword的后缀。

中文分词

1.安装
下载好的zip包,请解压后放到 …/elasticsearch/plugins/ik(需要在plugins下创建ik)
然后重启es
2.测试使用
使用默认

GET movie_index/_analyze
{  
  "text": "我是中国人"
}
  • 1
  • 2
  • 3
  • 4

请观察结果
使用分词器

GET movie_index/_analyze
{  "analyzer": "ik_smart", 
  "text": "我是中国人"
}
  • 1
  • 2
  • 3
  • 4

请观察结果
另外一个分词器
ik_max_word

GET movie_index/_analyze
{  "analyzer": "ik_max_word", 
  "text": "我是中国人"
}
  • 1
  • 2
  • 3
  • 4

请观察结果
能够看出不同的分词器,分词有明显的区别,所以以后定义一个type不能再使用默认的mapping了,要手工建立mapping, 因为要选择分词器。

自定义词库

/elasticsearch/plugins/ik/config/中的IKAnalyzer.cfg.xml
修改自己的地址8082端口号已经省略
记得注释掉这一行
在这里插入图片描述
在nginx/conf/nginx.conf添加es词库地址
在这里插入图片描述
在这里插入图片描述
重启nginx
访问http://hadoop102/fenci/mykeyword.txt

关于mapping

之前说type可以理解为table,那每个字段的数据类型是如何定义的呢
查看mapping

GET movie_index/_mapping/movie
  • 1

实际上每个type中的字段是什么数据类型,由mapping定义。
但是如果没有设定mapping系统会自动,根据一条数据的格式来推断出应该的数据格式。
 true/false → boolean
 1020 → long
 20.1 → double
 “2018-02-01” → date
 “hello world” → text +keyword
默认只有text会进行分词,keyword是不会分词的字符串。
mapping除了自动定义,还可以手动定义,但是只能对新加的、没有数据的字段进行定义。一旦有了数据就无法再做修改了。
注意:虽然每个Field的数据放在不同的type下,但是同一个名字的Field在一个index下只能有一种mapping定义。

  • 基于中文分词搭建索引
    建立mapping
PUT movie_chn
{
  "mappings": {
    "movie":{
      "properties": {
        "id":{
          "type": "long"
        },
        "name":{
          "type": "text"
          , "analyzer": "ik_smart"
        },
        "doubanScore":{
          "type": "double"
        },
        "actorList":{
          "properties": {
            "id":{
              "type":"long"
            },
            "name":{
              "type":"keyword"
            }
          }
        }
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

插入数据

PUT /movie_chn/movie/1
{ "id":1,
  "name":"红海行动",
  "doubanScore":8.5,
  "actorList":[  
  {"id":1,"name":"张译"},
  {"id":2,"name":"海清"},
  {"id":3,"name":"张涵予"}
 ]
}
PUT /movie_chn/movie/2
{
  "id":2,
  "name":"湄公河行动",
  "doubanScore":8.0,
  "actorList":[  
{"id":3,"name":"张涵予"}
]
}

PUT /movie_chn/movie/3
{
  "id":3,
  "name":"红海事件",
  "doubanScore":5.0,
  "actorList":[  
{"id":4,"name":"张晨"}
]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

查询测试

GET /movie_chn/movie/_search
{
  "query": {
    "match": {
      "name": "红海战役"
    }
  }
}

GET /movie_chn/movie/_search
{
  "query": {
    "term": {
      "actorList.name": "张译"
    }
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

索引别名 _aliases

索引别名就像一个快捷方式或软连接,可以指向一个或多个索引,也可以给任何一个需要索引名的API来使用。别名 带给我们极大的灵活性,允许我们做下面这些:

  1. 给多个索引分组 (例如, last_three_months)
  2. 给索引的一个子集创建视图
  3. 在运行的集群中可以无缝的从一个索引切换到另一个索引

思考一个问题为什么要使用别名的例子
我想要统计2020年6月份的数量,但我们每天都插有当天的数据数据
解决方案:我们设立别名都为6月份
index_a_20200616 as index_a_202006
index_a_20200617 as index_a_202006

创建索引别名

PUT movie_chn_2020
{  "aliases": {
      "movie_chn_2020-query": {}
  }, 
  "mappings": {
    "movie":{
      "properties": {
        "id":{
          "type": "long"
        },
        "name":{
          "type": "text"
          , "analyzer": "ik_smart"
        },
        "doubanScore":{
          "type": "double"
        },
        "actorList":{
          "properties": {
            "id":{
              "type":"long"
            },
            "name":{
              "type":"keyword"
            }
          }
        }
      }
    }
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

为已存在的索引增加别名

POST  _aliases
{
    "actions": [
        { "add":    { "index": "movie_chn_xxxx", "alias": "movie_chn_2020-query" }}
    ]
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

也可以通过加过滤条件缩小查询范围,建立一个子集视图

POST  _aliases
{
    "actions": [
        { "add":    
{ "index": "movie_chn_xxxx", 
"alias": "movie_chn0919-query-zhhy",
            "filter": {
                "term": {  "actorList.id": "3"
                 }
               }
 }
}
    ]
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

查询别名。 与使用普通索引没有区别

GET movie_chn_2020-query/_search
  • 1

删除某个索引的别名

POST  _aliases
{
    "actions": [
        { "remove":    { "index": "movie_chn_xxxx", "alias": "movie_chn_2020-query" }}
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

为某个别名进行无缝切换

POST /_aliases
{
    "actions": [
        { "remove": { "index": "movie_chn_xxxx", "alias": "movie_chn_2020-query" }},
        { "add":    { "index": "movie_chn_yyyy", "alias": "movie_chn_2020-query" }}
    ]
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

查询别名列表

GET  _cat/aliases?v
  • 1

索引模板

Index Template 索引模板,顾名思义,就是创建索引的模具,其中可以定义一系列规则来帮助我们构建符合特定业务需求的索引的 mappings 和 settings,通过使用 Index Template 可以让我们的索引具备可预知的一致性。
常见的场景: 分割索引
分割索引就是根据时间间隔把一个业务索引切分成多个索引。
比如 把order_info 变成 order_info_20200101,order_info_20200102 ……
这样做的好处有两个:
1 结构变化的灵活性:因为elasticsearch不允许对数据结构进行修改。但是实际使用中索引的结构和配置难免变化,那么只要对下一个间隔的索引进行修改,原来的索引位置原状。这样就有了一定的灵活性。
2 查询范围优化: 因为一般情况并不会查询全部时间周期的数据,那么通过切分索引,物理上减少了扫描数据的范围,也是对性能的优化。

  • 创建模板
PUT _template/template_movie2020
{
  "index_patterns": ["movie_test*"],                  
  "settings": {                                               
    "number_of_shards": 1
  },
  "aliases" : { 
    "{index}-query": {},
    "movie_test-query":{}
  },
  "mappings": {                                          
"_doc": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "movie_name": {
          "type": "text",
          "analyzer": "ik_smart"
        }
      }
    }
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

其中 “index_patterns”: [“movie_test*”], 的含义就是凡是往movie_test开头的索引写入数据时,如果索引不存在,那么es会根据此模板自动建立索引。
在 “aliases” 中用{index}表示,获得真正的创建的索引名。

  • 测试
POST movie_test_2020xxxx/_doc
{
  "id":"333",
  "name":"zhang3"
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 查看系统中已有的模板清单
GET  _cat/templates
  • 1
  • 查看某个模板详情
GET  _template/template_movie2020
或者
GET  _template/template_movie*
  • 1
  • 2
  • 3
  • 删除模板
DELETE _template/gmall0105_dau_info_template
  • 1
  • 删除模板里面别名的内容
    delete 索引
DELETE gmall0105_dau_info_2021-10-01
{
    "query": {
        "match_all": {
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

注意事项

索引模板就是利用es,在没有索引的时候使用自动腿短,来建立索引
注意 如果你们es中的shard特别多,有可能创建索引会变慢
如果延迟不能接受 建议别用模板,而是用定时脚本建立

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号