美文网首页
0x03.索引

0x03.索引

作者: 0x70e8 | 来源:发表于2018-07-06 11:15 被阅读0次

[TOC]

索引是映射类型的容器。ES的索引中存储数据和索引(反向索引)。索引实质上只是逻辑单元,实际的存储单元是shard(分片),索引切分成多个分片(创建时确定后期不可修改),分片又会有自己的副本。
索引的创建过程包含多个过程,包括分片数量,分片副本,字段映射策略,反向索引创建过程(分词等分析器),这些都可以在创建索引时显式指定,未指定则使用缺省设置。

自ES6.0不再支持一个index可以容纳多个type,一个索引更像一个数据库表。


添加索引

最简单索引添加是直接的使用PUT <index-name>,所有的索引设置都使用系统默认。且可以在添加文档时PUT <index-name>/<type-name>/<doc-id>{...}创建使用默认设置的索引。
默认的设置能应对一般的情况,但是有些情况需要自定义设置来达到目的。

设置shard

  • 查看默认的设置:
GET twitter/_settings
# result:
{
  "twitter": {
    "settings": {
      "index": {
        "creation_date": "1528793250200",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "_1JkIsgDSW6MD21Ct_UNdg",
        "version": {
          "created": "6020499"
        },
        "provided_name": "twitter"
      }
    }
  }
}
  • 设置shard:
PUT twitter # 添加index
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3,  # 指定分片数
            "number_of_replicas" : 2  #指定分片副本数
        }
    }
}
# 简单格式
PUT twitter
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}

设置Mapping(数据字段映射)

mapping实际上是type的属性,在设置是需要指定type。在6.0开始取消了多个type的特性,在7.0会完全移除type。
https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html

Mapping设置后,field的mapping定义不可修改,但是可以添加新的field

查看index的Mapping

如果创建索引时未指定mapping策略,会使用es的默认的mapping动态判断字段类型。

GET twitter/_mapping
# result 
{
  "twitter": {
    "mappings": {
      "_doc": { # type
        "properties": {
          "content": {
            "type": "text"   # text 支持全文检索的字段,可以给一个字段指定两个mapping策略
          },
          "email": {
            "type": "keyword" #不支持全文检索,需要精确匹配
          },
          "name": {
            "type": "text"
          },
          "tweeted_at": {
            "type": "date" # 精确匹配
          },
          "type": {
            "type": "keyword" # 精确匹配
          },
          "user_name": {
            "type": "keyword"
          }
        }
      }
    }
  }
}
# 查看字段的mapping
GET publications/_mapping/_doc/field/title
# 查看多个index的mapping
GET /twitter,kimchy/_mapping/field/message
GET /_all/_mapping/_doc,tweet,book/field/message,user.id
GET /_all/_mapping/_do*/field/*.id

字段可以设置两个映射策略,如:

"rtt_time": {
            "type": "text",
            "fields": {
              "keyword":{
                 "type": "keyword",
                "ignore_above": 256
              }
            }
          }

设置Mapping

PUT mysql_reindex/_mapping/statistics
{
  "settings":{},
  "properties": {
          "@timestamp": {
            "type": "date"
          },
          "@version": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "id": {
            "type": "long"
          },
          "info": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "rtt_date": {
           "type": "text",
            "fields": {
              "keyword":{
                 "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "rtt_time": {
            "type": "text",
            "fields": {
              "keyword":{
                 "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "rtt_value": {
            "type": "double" 
          }
        }
}
或者在请求体内指定type:
PUT users
{
  "settings": {
    "index.mapping.single_type": true
  },
  "mappings": {
    "_doc": { # type
      "properties": {
        "name": {
          "type": "text"
        }
      }
    }
  }
}

修改mapping

mapping添加后不能删除和修改,只能加新的。如果需要修改,只能舍弃整个索引重建一个新的。

数据类型

Core datatypes

类别 数据类型
string text (可全文检索,不可精确匹配),keyword(精确匹配)
Numeric datatypes long, integer, short, byte, double, float, half_float, scaled_float
Date datatype date
Boolean datatype boolean
Binary datatype binary
Range datatypes integer_range, float_range, long_range, double_range, date_range
字符串

string包含text和keyword.

通常可以用来过滤、排序和聚合,只能以精确值进行检索(不能全文索引)

可选参数:

* boost
* doc_values
* eager_global_ordinals
* fields
* ignore_above
* index
* index_options
* norms
* null_value
* store
* similarity
* normalizer 

支持全文索引的字符串,These fields are analyzed, that is they are passed through an analyzer to convert the string into a list of individual terms before being indexed.
Text fields are not used for sorting and seldom used for aggregations (although the significant text aggregation is a notable exception)

Sometimes it is useful to have both a full text (text) and a keyword (keyword) version of the same field: one for full text search and the other for aggregations and sorting. This can be achieved with multi-fields.

可选参数

* analyzer
* boost
* eager_global_ordinals
* fielddata
* fielddata_frequency_filter
* fields
* index
* index_options
* index_prefixes
* index_phrases
* norms
* position_increment_gap
* store
* search_analyzer
* search_quote_analyzer
* similarity
* term_vector 
数值
  • long:8字节有符号数(-263~263-1)
  • integer:4字节正数(-231~231-1)
  • short:2字节 -32,768 ~ 32,767
  • byte 1字节 -128~127
  • double: 64位双精度
  • float:32bit
  • half_float :16bit
  • scaled_float :一个由一个long支持的有限的浮点数,由一个固定的double因子缩放。

可选参数:

* coerce
* boost
* doc_values
* ignore_malformed
* index
* null_value
* store 
日期

JSON doesn’t have a date datatype, so dates in Elasticsearch can either be:

  • strings containing formatted dates, e.g. "2015-01-01" or "2015/01/01 12:10:30".
  • a long number representing milliseconds-since-the-epoch.
  • an integer representing seconds-since-the-epoch.

可选参数:

* boost
* doc_values
* format
* locale
* ignore_malformed
* index
* null_value
* store 

可以指定日期的格式。

boolean
  • False values : false, "false"
  • True values: true, "true"

可选参数

* boost
* doc_values
* index
* null_value
* store 
binary

The binary type accepts a binary value as a Base64 encoded string. The field is not stored by default and is not searchable.

PUT my_index/_doc/1
{
  "name": "Some binary blob",
  "blob": "U29tZSBiaW5hcnkgYmxvYg==" 
}

有两个可选参数:

* doc_values
* store
range
range type 描述
integer_range A range of signed 32-bit integers with a minimum value of -231 and maximum of 231-1.
float_range A range of single-precision 32-bit IEEE 754 floating point values.
long_range A range of signed 64-bit integers with a minimum value of -263 and maximum of 263-1.
double_range A range of double-precision 64-bit IEEE 754 floating point values.
date_range A range of date values represented as unsigned 64-bit integer milliseconds elapsed since system epoch.
ip_range A range of ip values supporting either IPv4 or IPv6 (or mixed) addresses.
  • 示例
// mapping
 "mappings": {
    "_doc": {
      "properties": {
        "expected_attendees": {
          "type": "integer_range"
        },
        "time_frame": {
          "type": "date_range", 
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        }
      }
    }
  }
// 数据:
{
  "expected_attendees" : { 
    "gte" : 10,
    "lte" : 20
  },
  "time_frame" : { 
    "gte" : "2015-10-31 12:00:00", 
    "lte" : "2015-11-01"
  }
}

可选参数:

* coerce
* boost
* index
* store 

Complex datatypes

类别 数据类型
Array datatype Array support does not require a dedicated type
Object datatype object for single JSON objects
Nested datatype nested for arrays of JSON objects
array

In Elasticsearch, there is no dedicated array type. Any field can contain zero or more values by default, however, all values in the array must be of the same datatype.

所有的数据域都支持同类型的多个值组成数组对象。没有专用的数组type。即直接在添加数据时添加多个即可,不需要在mapping中声明。

object

Json字符串本身就是对象,可内嵌子对象。所以对象类型的数据有一个properties属性:

PUT my_index
{
  "mappings": {
    "_doc": { 
      "properties": {
        "region": {
          "type": "keyword"
        },
        "manager": {   // manager就是一个对象类型,其内部封装了age name,name又是一个子对象
          "properties": {
            "age":  { "type": "integer" },
            "name": { 
              "properties": {
                "first": { "type": "text" },
                "last":  { "type": "text" }
              }
            }
          }
        }
      }
    }
  }
}

可选参数:

* dynamic
* enabled
* properties
nested type

嵌套的类型,是object类型的特殊形式,它支持对象数组的形式,内部有一个flat的过程。
使用此类型需要指定type为nested 。

PUT my_index/_doc/1
{
  "group" : "fans",
  "user" : [ 
    {
      "first" : "John",
      "last" :  "Smith"
    },
    {
      "first" : "Alice",
      "last" :  "White"
    }
  ]
}
// 被flat成:
{
  "group" :        "fans",
  "user.first" : [ "alice", "john" ],
  "user.last" :  [ "smith", "white" ]
}
// 检索
GET my_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "user.first": "Alice" }},
        { "match": { "user.last":  "Smith" }}
      ]
    }
  }
}

可选参数:

* dynamic
* properties

Geo datatypes

类别 数据类型
Geo-point datatype geo_point for lat/lon points :geo_point
Geo-Shape datatype geo_shape for complex shapes like polygons :geo_shape

Specialised datatypes

类别 数据类型
IP datatype ip for IPv4 and IPv6 addresses :ip
Completion datatype completion to provide auto-complete suggestions :completion
Token count datatype token_count to count the number of tokens in a string
mapper-murmur3 murmur3 to compute hashes of values at index-time and store them in the index
Percolator type Accepts queries from the query-dsl
join datatype Defines parent/child relation for documents within the same index

Multi-fields

一个field设置多个type,可以使一个field有多个特性,使用“fields”属性配置。

mapping的参数

The following mapping parameters are common to some or all field datatypes:

  • analyzer

指定字符串字段的分析器,用于分析字符串构建反向索引;

  • normalizer

指定keyword字段的分析器,类似字符串分析器,它生成单个token;

  • boost

在查询时,可以自动增强单个字段——更多地计算相关分数

  • coerce

强制转换成数字,默认开启;

  • copy_to

将字段拷贝到自定义的_all字段中,然后可以检索这个自定义字段;

  • doc_values

搜索需要回答“哪些文档包含这一项?”,而排序和聚合需要回答一个不同的问题:“这个字段对这个文档有什么价值?”。大多数字段都被默认索引,这使得它们可以搜索。但是,排序、聚合和访问脚本中的字段值需要与搜索不同的访问模式。doc_values引入此访问模式,它存储字段值到一个面向列的域中,提高排序和聚合的效率.几乎所有字段类型都支持doc_values,但被分析的字符串字段除外。默认开启;

  • dynamic

默认情况下,字段可以被动态地添加到文档中,或者在文档中添加内部对象,只需对包含新字段的文档进行索引即可。这一特性支持在文档中出现未设置的字段。

  • enabled

设置字段可检索,默认开启

  • fielddata

用于排序聚合和脚本中读取词项时,一般会使用doc_values来启动与检索不同的访问模式。但是文本字段不支持doc_values,文本字段使用内存中的查询时间数据结构fielddata。这个数据结构是在字段首次用于聚合、排序或脚本中时根据需要构建的。它是通过从磁盘每一段的整个反向索引来建立的,来反转词项↔︎文档的关系,并将结果存储在内存中(JVM堆)。默认关闭。Fielddata is disabled on text fields by default. Set fielddata=true on [your_field_name] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant *
memory.eager_global_ordinals

eager_global_ordinals是在doc-values之上的数据结构,它在字典顺序中为每个唯一的术语维护一个增量编号。

  • format

配置日期解析的格式

  • ignore_above

Strings longer than the ignore_above setting will not be indexed or stored.

  • ignore_malformed

试图将错误的数据类型索引到字段中,默认情况下会抛出异常,并拒绝整个文档。如果将ignore_malformed参数设置为true,则可以忽略异常。格式错误的字段不会被索引,但是文档中的其他字段被正常处理。

  • index_options

index_options参数控制将哪些信息添加到反向索引中,以便搜索和突出显示.支持选项:docs、freqs、positions、offsets

  • index

The index option controls whether field values are indexed. It accepts true or false and defaults to true. Fields that are not indexed are not queryable.

  • fields

以不同的方式为不同的目的索引相同的字段通常是有用的。这就是多领域的目的。例如,字符串字段可以被映射为全文搜索的文本字段,以及用于排序或聚合的关键字字段。(就是前面提到为一个字段添加多个类型)

  • norms

Norms store various normalization factors that are later used at query time in order to compute the score of a document relatively to a query.

  • null_value

A null value cannot be indexed or searched. When a field is set to null, (or an empty array or an array of null values) it is treated as though that field has no values.The null_value parameter allows you to replace explicit null values with the specified value so that it can be indexed and searched.(设置一个值替换null值)

分析文本字段将术语位置考虑在内,以便能够支持近距离或短语查询。当用多个值对文本字段进行索引时,在值之间添加一个“假”间隙,以防止大多数短语查询在值之间进行匹配。这个间隙的大小是使用位置增加间隙和默认值设置为100的。

  • properties

Type mappings, object fields and nested fields contain sub-fields, called properties.

  • search_analyzer

指定检索时使用的分析器,默认索引和检索使用同一个分析器

  • similarity

指定相似算法

  • store

By default, field values are indexed to make them searchable, but they are not stored. This means that the field can be queried, but the original field value cannot be retrieved.Usually this doesn’t matter. The field value is already part of the _source field, which is stored by default. If you only want to retrieve the value of a single field or of a few fields, instead of the whole _source, then this can be achieved with source filtering.In certain situations it can make sense to store a field. For instance, if you have a document with a title, a date, and a very large content field, you may want to retrieve just the title and the date without having to extract those fields from a large _source field.

  • term_vector

词项向量包含分析过程产生的术语的信息,包括:一个词项列表。每一项的位置(或顺序)。开始和结束字符偏移量将词项映射到原始字符串的起点。这些词项向量可以存储起来,以便可以为特定的文档检索它们。此参数用来设定词项想了保存的内容。

option means
no No term vectors are stored. (default)
yes Just the terms in the field are stored.
with_positions Terms and positions are stored.
with_offsets Terms and character offsets are stored.
with_positions_offsets Terms, positions, and character offsets are stored.

删除索引

删除索引即删除数据。
DELETE /<index-name>
不可以通过别名删除索引,可以使用通配符删除多个索引,或者使用_all删除所有。

获取索引

GET <index-name>

  • 查看索引是否存在
    HEAD <index-name>
  • 查看所有的索引
    GET _cat/indices

关闭和打开索引

封闭索引在集群上几乎没有开销(除了维护它的元数据),并且用于读/写操作被阻塞。一个封闭的索引可以被打开,然后通过正常的恢复过程

POST /my_index/_close

POST /my_index/_open

收缩索引

收缩索引API允许您使用较少的主碎片将现有索引收缩为新的索引。目标索引中请求的主碎片数量必须是源索引中碎片数量的一个因数。例如,一个有8个主碎片的索引可以被压缩成4、2或1个主碎片,或者一个有15个主碎片的索引可以缩进5、3或1。如果索引中的分片数是素数,则只能压缩为一个主分片。在收缩之前,索引中的每个碎片的(主或副本)副本必须出现在同一个节点上。

  • 准备
PUT /my_source_index/_settings
{
  "settings": {
    "index.routing.allocation.require._name": "shrink_node_name", 
    "index.blocks.write": true 
  }
}
  • 收缩
POST my_source_index/_shrink/my_target_index

切分索引

分割索引API允许您将现有索引分割为新的索引,其中每个原始的主碎片被分割为新索引中的两个或多个主碎片。

滚动索引

当现有索引被认为太大或太旧时,滚动索引API将一个别名转到一个新的索引。

索引别名

  • add

POST /_aliases    
{
    "actions" : [
        { "add" : { "index" : "test1", "alias" : "alias1" } }
    ]
}
  • remove
POST /_aliases
{
    "actions" : [
        { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
    ]
}
  • swap an index with an alias in one operation
PUT test     
PUT test_2   
POST /_aliases
{
    "actions" : [
        { "add":  { "index": "test_2", "alias": "test" } },
        { "remove_index": { "index": "test" } }  
    ]
}

设置别名

ES中的api在处理特定索引时接受索引名,在适用时接受多个索引名。索引别名API允许使用名称对索引进行别名,所有API都自动将别名名称转换为实际的索引名称。别名也可以映射到多个索引,当指定它时,别名将自动扩展到别名索引。别名还可以与搜索时自动应用的过滤器和路由值相关联。别名不能与索引具有相同的名称。

PUT test
{
    "aliases" : {
        "alias_1" : {},
        "alias_2" : {
            "filter" : {
                "term" : {"user" : "kimchy" }
            },
            "routing" : "kimchy"
        }
    }
}

# 简单形式
PUT test/_alias
{
    "alias":"testalias"
}
POST /_aliases
{
    "actions" : [
        { "remove" : { "index" : "test1", "alias" : "alias1" } },
        { "add" : { "index" : "test2", "alias" : "alias1" } }
    ]
}

# 查看所有别名
GET _cat/aliases
#查看索引别名
GET test/_alias

重新索引reindex

POST _reindex
{
   "source": {
     "index": "test"
   },
   "dest": {
     "index": "testreindex"
   }
   
}

参考资料

相关文章

  • 0x03.索引

    [TOC] 索引是映射类型的容器。ES的索引中存储数据和索引(反向索引)。索引实质上只是逻辑单元,实际的存储单元是...

  • CTF中zip总结

    隐写篇 0x01. 通过进制转换隐藏信息 0x02. 在图片中隐藏压缩包(图种) 加密篇 0x03. 伪加密 0x...

  • MySQL索引

    MySQL索引 索引介绍 索引原理与分析 组合索引 索引失效分析 索引介绍 什么是索引索引:包括聚集索引、覆盖索引...

  • Mysql优化

    一.索引科普 主键索引 唯一索引 普通索引 单列索引 多列索引 聚簇索引 非聚簇索引 前缀索引 全文索引 二.优化...

  • Oracle 索引学习

    创建索引 标准语法 唯一索引 组合索引 反向键索引 示例 删除索引 修改索引 重建索引 联机重建索引 合并索引

  • MySQL索引

    索引的作用 查看索引 创建索引 删除索引 索引类型 强制索引和禁止某个索引

  • Pandas数据操作

    Pandas数据操作 Series索引 行索引 切片索引 不连续索引 布尔索引 DataFrame索引 列索引 不...

  • 深入理解四种数据库索引类型(- 唯一索引/非唯一索引 - 主键索

    唯一索引/非唯一索引 主键索引(主索引) 聚集索引/非聚集索引 组合索引 唯一索引/非唯一索引 唯一索引 1.唯一...

  • MYSQL索引

    mysql的4种常用索引类型:唯一索引,主键索引,全文索引,以及普通索引。 普通索引(INDEX):普通索引为索引...

  • 索引类型

    索引类型有: 主键索引; 唯一索引; 普通索引; 全文索引; 多列索引;

网友评论

      本文标题:0x03.索引

      本文链接:https://www.haomeiwen.com/subject/rhlmeftx.html