赞
踩
GET /_cat/indices?v
es 中会默认存在一个名为.kibana的索引
PUT /movie_index
ES 是不删除也不修改任何数据的,而是增加版本号
DELETE /movie_index
格式 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"}
]
}
GET movie_index/movie/1
和新增没有区别 要求:必须包括全部字段
PUT /movie_index/movie/3
{
"id":"3",
"name":"incident red sea",
"doubanScore":"5.0",
"actorList":[
{"id":"1","name":"zhang chen"}
]
}
POST movie_index/movie/3/_update
{
"doc": {
"doubanScore":"7.0"
}
}
DELETE movie_index/movie/3
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"
}
]
}
。。。。。。。。
。。。。。。。。
}
GET movie_index/movie/_search
{
"query":{
"match_all": {}
}
}
GET movie_index/movie/_search
{
"query":{
"match": {"name":"red"}
}
}
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":"rad"}
}
}
校正匹配分词,当一个单词都无法准确匹配,es通过一种算法对非常接近的单词也给与一定的评分,能够查询出来,但是消耗更多的性能。
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": "1" }},
{"term": { "actorList.id": "3" }}
],
"must":{"match":{"name":"red"}}
}
}
GET movie_index/movie/_search
{
"query": {
"bool": {
"filter": {
"range": {
"doubanScore": {"gte": 8}
}
}
}
}
}
关于范围操作符:
| | |
|–|–|–|–|
|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"
}
}
]
}
GET movie_index/movie/_search
{
"query": { "match_all": {} },
"from": 1,
"size": 1
}
第二页 每页10行 from = (pageNo-1)*size
(2-1) * 10(size) = 10(from)
GET movie_index/movie/_search
{
"query": { "match_all": {} },
"_source": ["name", "doubanScore"]
}
GET movie_index/movie/_search
{
"query":{
"match": {"name":"red sea"}
},
"highlight": {
"fields": {"name":{} }
}
}
取出每个演员共参演了多少部电影
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"
}
}
}
}
}
}
聚合时为何要加 .keyword后缀?
.keyword 是某个字符串字段,专门储存不分词格式的副本 ,在某些场景中只允许只用不分词的格式,比如过滤filter 比如 聚合aggs, 所以字段要加上.keyword的后缀。
1.安装
下载好的zip包,请解压后放到 …/elasticsearch/plugins/ik(需要在plugins下创建ik)
然后重启es
2.测试使用
使用默认
GET movie_index/_analyze
{
"text": "我是中国人"
}
请观察结果
使用分词器
GET movie_index/_analyze
{ "analyzer": "ik_smart",
"text": "我是中国人"
}
请观察结果
另外一个分词器
ik_max_word
GET movie_index/_analyze
{ "analyzer": "ik_max_word",
"text": "我是中国人"
}
请观察结果
能够看出不同的分词器,分词有明显的区别,所以以后定义一个type不能再使用默认的mapping了,要手工建立mapping, 因为要选择分词器。
/elasticsearch/plugins/ik/config/中的IKAnalyzer.cfg.xml
修改自己的地址8082端口号已经省略
记得注释掉这一行
在nginx/conf/nginx.conf添加es词库地址
重启nginx
访问http://hadoop102/fenci/mykeyword.txt
之前说type可以理解为table,那每个字段的数据类型是如何定义的呢
查看mapping
GET movie_index/_mapping/movie
实际上每个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定义。
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"
}
}
}
}
}
}
}
插入数据
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":"张晨"}
]
}
查询测试
GET /movie_chn/movie/_search
{
"query": {
"match": {
"name": "红海战役"
}
}
}
GET /movie_chn/movie/_search
{
"query": {
"term": {
"actorList.name": "张译"
}
}
}
索引别名就像一个快捷方式或软连接,可以指向一个或多个索引,也可以给任何一个需要索引名的API来使用。别名 带给我们极大的灵活性,允许我们做下面这些:
思考一个问题为什么要使用别名的例子
我想要统计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"
}
}
}
}
}
}
}
为已存在的索引增加别名
POST _aliases
{
"actions": [
{ "add": { "index": "movie_chn_xxxx", "alias": "movie_chn_2020-query" }}
]
}
也可以通过加过滤条件缩小查询范围,建立一个子集视图
POST _aliases
{
"actions": [
{ "add":
{ "index": "movie_chn_xxxx",
"alias": "movie_chn0919-query-zhhy",
"filter": {
"term": { "actorList.id": "3"
}
}
}
}
]
}
查询别名。 与使用普通索引没有区别
GET movie_chn_2020-query/_search
删除某个索引的别名
POST _aliases
{
"actions": [
{ "remove": { "index": "movie_chn_xxxx", "alias": "movie_chn_2020-query" }}
]
}
为某个别名进行无缝切换
POST /_aliases
{
"actions": [
{ "remove": { "index": "movie_chn_xxxx", "alias": "movie_chn_2020-query" }},
{ "add": { "index": "movie_chn_yyyy", "alias": "movie_chn_2020-query" }}
]
}
查询别名列表
GET _cat/aliases?v
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"
}
}
}
}
}
其中 “index_patterns”: [“movie_test*”], 的含义就是凡是往movie_test开头的索引写入数据时,如果索引不存在,那么es会根据此模板自动建立索引。
在 “aliases” 中用{index}表示,获得真正的创建的索引名。
POST movie_test_2020xxxx/_doc
{
"id":"333",
"name":"zhang3"
}
GET _cat/templates
GET _template/template_movie2020
或者
GET _template/template_movie*
DELETE _template/gmall0105_dau_info_template
DELETE gmall0105_dau_info_2021-10-01
{
"query": {
"match_all": {
}
}
}
索引模板就是利用es,在没有索引的时候使用自动腿短,来建立索引
注意 如果你们es中的shard特别多,有可能创建索引会变慢
如果延迟不能接受 建议别用模板,而是用定时脚本建立
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。