索引与类型
索引
查看索引
curl 127.0.0.1:9200/_cat/indices
请求
curl 127.0.0.1:9200/_cat
可获取用于查询的名称
创建索引
索引可以在添加文档数据时,通过动态映射的方式自动生成索引与类型。
索引也可以手动创建,通过手动创建,可以控制主分片数目、分析器和类型映射。
PUT /my_index
{
"settings": { ... any settings ... },
"mappings": {
"type_one": { ... any mappings ... },
"type_two": { ... any mappings ... },
...
}
}
注: 在Elasticsearch 5.x版本中,设置分片与设置索引的类型字段需要分两次设置完成。
删除索引
用以下的请求来 删除索引:
DELETE /my_index
你也可以这样删除多个索引:
DELETE /index_one,index_two
DELETE /index_*
你甚至可以这样删除 全部 索引:
DELETE /_all
DELETE /*
创建头条项目文章索引库
// 文章索引
curl -X PUT 127.0.0.1:9200/articles -H 'Content-Type: application/json' -d'
{
"settings" : {
"index": {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}
}
'
类型和映射
类型 在 Elasticsearch 中表示一类相似的文档,类型由 名称 和 映射 ( mapping)组成。
映射, mapping, 就像数据库中的 schema ,描述了文档可能具有的字段或 属性 、 每个字段的数据类型—比如 string
, integer
或 date
。
为了能够将时间字段视为时间,数字字段视为数字,字符串字段视为全文或精确值字符串, Elasticsearch 需要知道每个字段中数据的类型。
简单字段类型:
-
字符串:
text
(在elaticsearch 2.x版本中,为string类型) - 整数 :
byte
,short
,integer
,long
- 浮点数:
float
,double
- 布尔型:
boolean
- 日期:
date
头条项目文章类型映射
curl -X PUT 127.0.0.1:9200/articles/_mapping/article -H 'Content-Type: application/json' -d'
{
"_all": {
"analyzer": "ik_max_word"
},
"properties": {
"article_id": {
"type": "long",
"include_in_all": "false"
},
"user_id": {
"type": "long",
"include_in_all": "false"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 2
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"include_in_all": "true"
},
"status": {
"type": "integer",
"include_in_all": "false"
},
"create_time": {
"type": "date",
"include_in_all": "false"
}
}
}
'
-
_all
字段是把所有其它字段中的值,以空格为分隔符组成一个大字符串,然后被分析和索引,但是不存储,也就是说它能被查询,但不能被取回显示。_all
允许在不知道要查找的内容是属于哪个具体字段的情况下进行搜索。 -
analyzer
指明使用的分析器索引时的顺序如下:
- 字段映射里定义的
analyzer
-
否则索引设置中名为
default
的分析器,默认为standard
标准分析器在搜索时,顺序有些许不同:
-
查询自己定义的
analyzer
- 否则字段映射里定义的
analyzer
- 否则索引设置中名为
default
的分析器,默认为standard
标准分析器
- 字段映射里定义的
-
include_in_all
参数用于控制_all
查询时需要包含的字段。默认为 true。 -
boost
可以提升查询时计算相关性分数的权重。例如title
字段将是其他字段权重的两倍。
查看映射
curl 127.0.0.1:9200/articles/_mapping/article?pretty
映射修改
一个类型映射创建好后,可以为类型增加新的字段映射
curl -X PUT 127.0.0.1:9200/articles/_mapping/article -H 'Content-Type:application/json' -d '
{
"properties": {
"new_tag": {
"type": "text"
}
}
}
'
但是不能修改已有字段的类型映射,原因在于elasticsearch已按照原有字段映射生成了反向索引数据,类型映射改变意味着需要重新构建反向索引数据,所以并不能再原有基础上修改,只能新建索引库,然后创建类型映射后重新构建反向索引数据。
例如,将status字段类型由integer改为byte会报错
curl -X PUT 127.0.0.1:9200/articles/_mapping/article -H 'Content-Type:application/json' -d '
{
"properties": {
"status": {
"type": "byte"
}
}
}
'
需要从新建立索引
curl -X PUT 127.0.0.1:9200/articles_v2 -H 'Content-Type: application/json' -d'
{
"settings" : {
"index": {
"number_of_shards" : 3,
"number_of_replicas" : 1
}
}
}
'
curl -X PUT 127.0.0.1:9200/articles_v2/_mapping/article -H 'Content-Type: application/json' -d'
{
"_all": {
"analyzer": "ik_max_word"
},
"properties": {
"article_id": {
"type": "long",
"include_in_all": "false"
},
"user_id": {
"type": "long",
"include_in_all": "false"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"include_in_all": "true",
"boost": 2
},
"content": {
"type": "text",
"analyzer": "ik_max_word",
"include_in_all": "true"
},
"status": {
"type": "byte",
"include_in_all": "false"
},
"create_time": {
"type": "date",
"include_in_all": "false"
}
}
}
'
重新索引数据
curl -X POST 127.0.0.1:9200/_reindex -H 'Content-Type:application/json' -d '
{
"source": {
"index": "articles"
},
"dest": {
"index": "articles_v2"
}
}
'
为索引起别名
为索引起别名,让新建的索引具有原索引的名字,可以让应用程序零停机。
curl -X DELETE 127.0.0.1:9200/articles
curl -X PUT 127.0.0.1:9200/articles_v2/_alias/articles
查询索引别名
# 查看别名指向哪个索引
curl 127.0.0.1:9200/*/_alias/articles
# 查看哪些别名指向这个索引
curl 127.0.0.1:9200/articles_v2/_alias/*
文档
一个文档不仅仅包含它的数据 ,也包含 元数据(metadata) —— 有关文档的信息。 三个必须的元数据元素如下:
-
_index
文档在哪存放
-
_type
文档表示的对象类别
-
_id
文档唯一标识
索引文档(保存文档数据)
使用自定义的文档id
PUT /{index}/{type}/{id}
{
"field": "value",
...
}
curl -X PUT 127.0.0.1:9200/articles/article/150000 -H 'Content-Type:application/json' -d '
{
"article_id": 150000,
"user_id": 1,
"title": "python是世界上最好的语言",
"content": "确实如此",
"status": 2,
"create_time": "2019-04-03"
}'
自动生成文档id
PUT /{index}/{type}
{
"field": "value",
...
}
获取指定文档
curl 127.0.0.1:9200/articles/article/150000?pretty
# 获取一部分
curl 127.0.0.1:9200/articles/article/150000?_source=title,content\&pretty
注意:_version
每次修改文档数据,版本都会增加,可以当作乐观锁的依赖(判断标准)使用
判断文档是否存在
curl -i -X HEAD 127.0.0.1:9200/articles/article/150000
- 存在 200状态码
- 不存在 404状态码
更新文档
在 Elasticsearch 中文档是 不可改变 的,不能修改它们。 相反,如果想要更新现有的文档,需要 重建索引或者进行替换。我们可以使用相同的 index
API 进行实现。
例如修改title字段的内容,不可进行以下操作(仅传递title字段内容)
curl -X PUT 127.0.0.1:9200/articles/article/150000 -H 'Content-Type:application/json' -d '
{
"title": "python必须是世界上最好的语言"
}'
而是要索引完整文档内容
curl -X PUT 127.0.0.1:9200/articles/article/150000 -H 'Content-Type:application/json' -d '
{
"article_id": 150000,
"user_id": 1,
"title": "python必须是世界上最好的语言",
"content": "确实如此",
"status": 2,
"create_time": "2019-04-03"
}'
注意返回值_version的变化
删除文档
curl -X DELETE 127.0.0.1:9200/articles/article/150000
取回多个文档
curl -X GET 127.0.0.1:9200/_mget -d '
{
"docs": [
{
"_index": "articles",
"_type": "article",
"_id": 150000
},
{
"_index": "articles",
"_type": "article",
"_id": 150001
}
]
}'