elasticsearch-索引操作

获取索引设置 http://192.168.31.46:9200/blog,blog2/_settings

{
    "blog2": {
        "settings": {
            "index": {
                "refresh_interval": "1s",
                "number_of_shards": "5",
                "provided_name": "blog2",
                "creation_date": "1535919566680",
                "number_of_replicas": "2",
                "uuid": "giYIMeckRgmViZ2k3RUfLw",
                "version": {
                    "created": "5040399"
                }
            }
        }
    },
    "blog": {
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "require": {
                            "_name": "shrink_node_name"
                        }
                    }
                },
                "refresh_interval": "1s",
                "number_of_shards": "5",
                "blocks": {
                    "write": "false"
                },
                "provided_name": "blog",
                "creation_date": "1535915560740",
                "number_of_replicas": "3",
                "uuid": "jPiYBwfdRDCFNRsLjCf9ng",
                "version": {
                    "created": "5040399"
                }
            }
        }
    }
}

PUT http://192.168.31.46:9200/_mget

{
    "docs":[
        {
            "_index":"blog",
            "_type":"article",
            "_id":1
        },
        {
            "_index":"blog",
            "_type":"twitter",
            "_id":1
        }
    ]
}

POST http://192.168.31.46:9200/test/type1/1/_update

{
    "script":{
        "inline":"if(ctx._source.tags.contains(params.tag)) {ctx.op=\"delete\"} else { ctx.op=\"none\"}",
        "lang":"painless",
        "params":{
            "tag":"yellow"
        }
    }
}

收缩索引  PUT http://192.168.31.46:9200/blog/_shrink/blog_new

{
    "source":{
        "index":"blog2"
    },"dest":{
        "index":"blog"
    }
}

关闭索引

POST http://192.168.31.46:9200/logstash-elasticsearch-log-2018.08.30/_close

{
    "blocks.write":false
}

复制索引  POST http://192.168.31.46:9200/_reindex

{
    "source":{
        "index":"blog2"
    },"dest":{
        "index":"blog"
    }
}

查询更新 POST http://192.168.31.46:9200/test/_update_by_query

{
    "script":{
        "inline":"ctx._source.content=params.content",
        "lang":"painless",
        "params":{
            "content":"this is china"
        }
    },
    "query":{
        "term":{
            "title":"kafka consumer"
        }
    }
}

删除文档 DELETE http://192.168.31.46:9200/blog/article/AWWcDeSFpQDi8LktErPP

{
    "found": true,
    "_index": "blog",
    "_type": "article",
    "_id": "AWWcDeSFpQDi8LktErPP",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 4,
        "successful": 2,
        "failed": 0
    }
}

查询删除 http://192.168.31.46:9200/blog/_delete_by_query

{
    "query":{
        "term":{
            "title":"kafka consumer"
        }
    }
}

curl -PUT http://192.168.31.46:9200/_bulk --data-binary @request 

{ "name":"jack", "age" :18}

{"index":{"_index":"zhouls","_type":"emp","_id":"11"}}

{"name":"tom", "age":27}

{"update":{"_index":"zhouls","_type":"emp", "_id":"2"}}

{"doc":{"age" :22}}

{"delete":{"_index":"zhouls","_type":"emp","_id":"1"}}

{
    "took":7846,
    "errors":true,
    "items":[
        {
            "index":{
                "_index":"zhouls",
                "_type":"emp",
                "_id":"10",
                "_version":1,
                "result":"created",
                "_shards":{
                    "total":3,
                    "successful":2,
                    "failed":0
                },
                "created":true,
                "status":201
            }
        },
        {
            "index":{
                "_index":"zhouls",
                "_type":"emp",
                "_id":"11",
                "_version":1,
                "result":"created",
                "_shards":{
                    "total":3,
                    "successful":1,
                    "failed":0
                },
                "created":true,
                "status":201
            }
        },
        {
            "update":{
                "_index":"zhouls",
                "_type":"emp",
                "_id":"2",
                "status":404,
                "error":{
                    "type":"document_missing_exception",
                    "reason":"[emp][2]: document missing",
                    "index_uuid":"Gju1E0Q0TfKd0UnLKghN4g",
                    "shard":"2",
                    "index":"zhouls"
                }
            }
        },
        {
            "delete":{
                "found":false,
                "_index":"zhouls",
                "_type":"emp",
                "_id":"1",
                "_version":1,
                "result":"not_found",
                "_shards":{
                    "total":3,
                    "successful":1,
                    "failed":0
                },
                "status":404
            }
        }
    ]
}

put http://192.168.31.46:9200/balls

http://192.168.31.46:9200/balls/_mapping

{
    "balls": {
        "mappings": {
            "_default_": {
                "dynamic_templates": [
                    {
                        "message_field": {
                            "path_match": "@message",
                            "match_mapping_type": "string",
                            "mapping": {
                                "norms": false,
                                "type": "text"
                            }
                        }
                    },
                    {
                        "string_fields": {
                            "match": "*",
                            "match_mapping_type": "string",
                            "mapping": {
                                "fields": {
                                    "smart": {
                                        "norms": false,
                                        "type": "text"
                                    }
                                },
                                "type": "keyword"
                            }
                        }
                    }
                ],
                "properties": {
                    "@timestamp": {
                        "type": "date"
                    }
                }
            }
        }
    }
}

{
    "books": {
        "mappings": {
            "it": {
                "dynamic_templates": [
                    {
                        "message_field": {
                            "path_match": "@message",
                            "match_mapping_type": "string",
                            "mapping": {
                                "norms": false,
                                "type": "text"
                            }
                        }
                    },
                    {
                        "string_fields": {
                            "match": "*",
                            "match_mapping_type": "string",
                            "mapping": {
                                "fields": {
                                    "smart": {
                                        "norms": false,
                                        "type": "text"
                                    }
                                },
                                "type": "keyword"
                            }
                        }
                    }
                ],
                "properties": {
                    "@timestamp": {
                        "type": "date"
                    },
                    "id": {
                        "type": "long"
                    },
                    "name": {
                        "type": "keyword",
                        "fields": {
                            "smart": {
                                "type": "text",
                                "norms": false
                            }
                        }
                    },
                    "publish_date": {
                        "type": "date"
                    }
                }
            }
        }
    }
}

PUT http://192.168.31.46:9200/plays/_mapping

{
    "mappings":{
        "it":{
            "dynamic":"strict",
            "properties":{
                "title":{
                    "type":"text"
                },
                "publish_date":{
                    "type":"date"
                }
            }
        }
    }
}

版本控制

悲观锁控制:传统关系型数据库,行锁、表锁、读锁、写锁

乐观锁控制:elasticsearch使用,适用于读操作比较多的应用类型,可省去锁开销,可以提高吞吐量

1 内部版本控制机制要求每次操作请求,只有当版本号相等时才能操作成功,

2 外部版本控制要求外部文档版本比内部版本文档高时才能更新成功。

路由机制

通过哈希算法,将具有相同哈希值的文档放到同一个主分片中,分片位置计算方法:shard=hash(routing)%Number_of_primary_shards

PUT http://192.168.31.53:9200/website/blog/4?routing=user123

映射详解

动态映射

PUT http://192.168.31.46:9200/books

{
    "mappings":{
        "it":{
            "dynamic":"strict",//dynamic       true,自动添加字段,false-忽略新的字段,strict,发现新的字段抛出异常
            "properties":{
                "title":{
                    "type":"text"
                },
                "publish_date":{
                    "type":"date"
                }
            }
            
        }
    }
}

通过设置

{
    "mappings":{
        "my_type":{
            "date_detection":false
        }
    }
}

关闭日期检测,

静态映射,创建索引时手动指定索引映射,添加更加详细更精准的配置信息

PUT http://192.168.31.46:9200/myindex

{
    "mappings":{
        "user":{
            "_all":{
                "enabled":false
            },
            "properties":{
                "title":{
                    "type":"text"
                },
                "name":{
                    "type":"text"
                },
                "age":{
                    "type":"integer"
                }
            }
        },
        "blogpost":{
            "_all":{
                "enabled":false
            },
            "properties":{
                "title":{
                    "type":"text"
                },
                "body":{
                    "type":"text"
                },
                "user_id":{
                    "type":"integer"
                },
                "created":{
                    "type":"date",
                    "format":"strict_date_optional_time || epoch_millis"
                }
            }
        }
    }
}

字段类型

一级分类 二级分类 具体类型
核心类型 字符串类型

string,text,keyword(text全文搜索,内容,倒排索引,词项,不用于排序,很少用于聚合)

keyword 索引结构化的字段,email地址,主机名,状态码,标签,用于过滤,排序,聚合,只能通过精确值搜索到

整数类型 integer,long,short,byte(在满足需求的情况下,尽可能选择范围小的数据类型。比如,某个字段的取值最大值不会超过100,那么选择byte类型即可。迄今为止吉尼斯记录的人类的年龄的最大值为134岁,对于年龄字段,short足矣。字段的长度越短,索引和搜索的效率越高
浮点类型 double,float,half_float,scaled_float
逻辑类型 boolean  true false "true" "false"
日期类型 date(日期格式的字符串,毫秒数,秒数,内部会将日期数据转换为UTC,并存储为milliseconds-since-the-epoch的long型整数)
范围类型 range
二进制类型 binary(base64来表示索引中存储的二进制数据,可用来存储二进制形式的数据,例如图像。默认情况下,该类型的字段只存储不索引。二进制类型只支持index_name属性
复合类型 数组类型 array 
对象类型 object
嵌套类型 nested(nested对象类型可以保持数组中每个对象的独立性,将数组中每个对象作为独立隐藏文档来索引,每个嵌套对象可以被搜索,
地理类型 地理坐标类型 geo_point(存储地理位置信息的经纬度)“properties”:{"location":{"type":"geo_point"}}
地理地图 geo_shape
特殊类型 IP类型 ip
范围类型 completion
令牌计数类型 token_count
附件类型 attachment
抽取类型 percolator

元数据概述

分类 元数据 说明
文档属性元数据 _index 文档所属的索引
_id 文档的id
_type 文档所属类型
_uid 由_type和_id字段组成
文档元数据 _source 文档的原生json字符串
_size 整个_source字段的字节数大小
索引元数据 _all 自动组合所有的字段值
_field_names 索引了每个字段的名称
路由元数据 _parent 指定文档之间父子关系,已过时
_routing 将一个文档根据路由存储到指定分片上
自定义元数据 _meta 用于自定义元数据

https://blog.csdn.net/chengyuqiang/article/details/79054153

映射参数

对字段的映射进行参数设置,如字段的分词器,字段的权重,日期格式,检索模型的选择

1 Analyzer

自定文本字段的分词器,

安装head

  • 插件官网地址https://github.com/mobz/elasticsearch-head
  • 由于插件时托管在github中,要从github中直接下载所以需要先安装git
  • 安装插件时又使用到了npm,所以npm同时都安装上(注意:npm在epel源中)
yum install git npm                                             # npm在epel源中
git clone https://github.com/mobz/elasticsearch-head.git        # 安装过程需要连接互联网
cd elasticsearch-head                                           # git clone后会自动生成的一个目录
npm install
npm run start

如果想查询集群健康信息,那么需要在elasticsearch配置文件中授权

vim /etc/elasticsearch/elasticsearch.yml
http.cors.enabled: true                                     # elasticsearch中启用CORS
http.cors.allow-origin: "*"                                 # 允许访问的IP地址段,* 为所有IP都可以访问

猜你喜欢

转载自blog.csdn.net/strideahead/article/details/82343359