赞
踩
dynamic:true 字段可以动态创建,更新数据时可以保存索引中不存在的字段
dynamic:false 更新数据时,字段只能为索引现有字段
PUT /student-001 { "mappings": { "dynamic": "true", "properties": { "birthday": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss||strict_date_optional_time||epoch_millis" }, "introduce": { "type": "text" }, "name": { "type": "keyword" }, "sex": { "type": "keyword" }, "stu_id": { "type": "long" } } } }
POST _aliases
{
"actions": [
{
"add": {
"index": "student-001",
"alias": "student"
}
}
]
}
场景 原来别名索引student 对应真实索引为student-001,但是后面因为业务需求,该索引相关字段类型发生了变化,这时按照新的需求建立了新的索引student-002,对应别名也是student ,此时我们不再需要向student-001写入数据,只向student-002写入数据。
POST /student-001/_aliases/student
{
"is_write_index":false
}
POST /student-002/_aliases/student
{
"is_write_index":true
}
POST /student/_doc
{
"birthday":"1999-01-01",
"name":"小明",
"introduce":"xxxxxxxxxxxxxx....",
"sex":"男",
"stu_id":1
}
删除数据有两种方式,可以根据数据自身id直接删除,也可以根据条件删除
DELETE student/_doc/ieVaE4QBt9FqGj2fyOdD
POST student/_delete_by_query
{
"query": {
"term": {
"name": "小明"
}
}
}
修改数据可以根据id和条件来修改
POST student/_update/OediE4QBt9FqGj2fw1I2
{
"doc": {
"sex":"女"
}
}
POST student/_update_by_query
{
"query": {
"term": {
"name": "小明"
}
},
"script": {
"source": "ctx._source['sex'] = '男'"
}
}
查询涉及到的知识比较多,有term,terms,match,match_phrase,wildcard等等,这儿简单的列举一种
GET student/_search
{
"query": {
"term": {
"name": "小明"
}
}
}
这儿以name字段来分类统计
size:指定数量
GET student/_search
{
"aggs": {
"NAME": {
"terms": {
"field": "name",
"size": 10
}
}
}
}
通过截取name属性前5个字符长度来进行分类统计
GET student/_search
{
"aggs": {
"NAME": {
"terms": {
"field": "name",
"size": 10,
"script": "doc['name'].value.substring(0,5)"
}
}
}
}
根据某一属性进行拍重查询
GET /student/_search
{
"collapse": {
"field": "name"
}
}
添加一个int类型的班级id字段
PUT student/_mapping
{
"properties": {
"class_id": {
"type": "integer"
}
}
}
异常:Trying to create too many buckets. Must be less than or equal to: [65535] but was [65536
PUT /_cluster/settings
{
"persistent": {
"search.max_buckets": 100000
}
}
业务中,进行更新数据操作后,会发现数据没有立即更新,这是因为es会将索引数据进行缓存,根据需求,可以自己设置缓存刷新时间
PUT student/_settings
{
"refresh_interval": "50ms"
}
未完待续…
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。