• ElasticSearch基础(4)-索引


    一、ES API常用规则

    ES支持以Http协议的方式提供REST服务,以JSON格式发送请求返回响应。

    ES提供了大量的不管的数据操作,运维管理API,大量的api

    这海量的api有一些通用的功能特性。比如pretty格式化输出等等。

    1.1 多索引参数

    1. 支持多索引查询,就是同时可以查询多个索引中的数据,例如,参数test1,test2,test3,表示同时搜索test1,test2,test3三个索引中中的数据,或者用(_all全部索引)。

    2. 支持通配符的操作,例如test*,表示查询所有以test开头的索引。同时也支持排除操作,例如+test*,-test3表示查询所有test开头的索引,排除test3。

    3. 多索引查询还支持以下参数:

    ignore_unavailable:当索引不存在或者关闭的时候,是否忽略这些索引,值为true和false。
    allow_no_indices:当使用通配符查询时,当有索引不存在的时候是否返回查询失败。
    expand_wildcards :控制什么类型的索引被支持,值为open,close,none,all,open表示只支持open类型的索引,close表示只支持关闭状态的索引,none表示不可用,all表示同时支持open和close索引。

    注意:文档操作API和索引别名API不支持多索引参数。

    1.2  通用参数

    例如pretty,human,format=yaml,flatt_setting=true等等

    看语义自然知道其用法...不过注意,可能在客户端做实验的时候各种字符需要转义。

    1.3 filter_path

    可以通过filter_path来对返回内容进行过滤

    curl -XGET 'http://localhost:9200/bank/account/_search?pretty&filter_path=took,hits.hits._id' -d '{
      "query" : {"match_all" : {}}
    }'

    支持通配符*匹配字段名称,例如:

    curl -XGET 'http://localhost:9200/bank/account/_search?pretty&filter_path=took,hits.h*._id' -d '{
      "query" : {"match_all" : {}}
    }'

    再举个例子,可以用两个通配符**来匹配不确定名称的字段,例如我们可以返回版本的段信息:

    curl -XGET 'http://localhost:9200/_segments?pretty&filter_path=indices.**.version'

    可以结合_source字段和filter_path参数,例如:

    curl -XGET 'http://localhost:9200/_search?pretty&filter_path=hits.hits._source&_source=source_node'

    二、索引常用操作API

    2.1 创建及查看索引

    1. 创建索引

    迄今为止,我们简单的通过添加一个文档的方式创建了一个索引。这个索引使用默认设置,新的属性通过动态映射添加到分类中。现在我们需要对这个过程有更多的控制:我们需要确保索引被创建在适当数量的分片上,在索引数据_之前_设置好分析器和类型映射。

    为了达到目标,我们需要手动创建索引,在请求中加入所有设置和类型映射,如下所示:

    PUT /my_index
    {
        "settings": { ... any settings ... },
        "mappings": {
            "type_one": { ... any mappings ... },
            "type_two": { ... any mappings ... },
            ...
        }

    事实上,你可以通过在 config/elasticsearch.yml 中添加下面的配置来防止自动创建索引。

    action.auto_create_index: false

    NOTE

    今后,我们将介绍怎样用【索引模板】来自动预先配置索引。这在索引日志数据时尤其有效: 你将日志数据索引在一个以日期结尾的索引上,第二天,一个新的配置好的索引会自动创建好。

    下面是一个简单的例子:

    curl -XPOST 'http://localhost:9200/mytest3' -d '
    {
        "settings": {
            "number_of_shards": 3,
            "number_of_replicas": 2
        },
        "mappings": {
            "type1": {
                "properties": {
                    "field1": {
                        "type": "string",
                        "index": "not_analyzed"
                    }
                }
            }
        }
    }
    '

    2. 查看索引

    curl -XGET 'http://localhost:9200/mytest3?pretty'

    还可以加入过滤,支持:_settings,_mappings _warmers, _aliases ,如:

    curl -XGET 'http://localhost:9200/mytest3/_settings,_mappings?pretty'

    3. 检查索引是否存在

    curl -XHEAD -i 'http://localhost:9200/mytest3'

    4. 打开和关闭索引

    curl -XPOST 'http://localhost:9200/mytest/_close'
    curl -XPOST 'http://localhost:9200/mytest/_open'

    5. 删除索引

    使用以下的请求来删除索引:

    DELETE /my_index

    你也可以用下面的方式删除多个索引

    DELETE /index_one,index_two
    DELETE /index_*

    你甚至可以删除所有索引

    DELETE /_all

    2.2 Put Mapping API

    先创建一个官网上的索引

    curl -XPUT 'http://localhost:9200/twitter?pretty' -d '{
      "mappings": {
        "tweet": {
            "properties":{
              "message":{
                "type": "string"
              }
            }
            
        }
      }
    }
    '

    如何给已经存在的索引增加类型

    curl -XPUT 'http://localhost:9200/twitter/_mapping/user?pretty' -d '{
      "properties":{
          "name":{"type": "string"}
      }
    }
    '

    如何给已经存在的索引类型增加字段

    curl -XPUT 'http://localhost:9200/twitter/_mapping/tweet?pretty' -d '{
      "properties":{
          "username":{"type":"string"}
      }
    }
    '

    2.3 mapping API

    获取mapping

    curl -XGET 'http://localhost:9200/_mapping/tweet?pretty'
    curl -XGET 'http://localhost:9200/_all/_mapping/tweet,account?pretty'
    curl -XGET 'http://localhost:9200/_all/_mapping?pretty'
    curl -XGET 'http://localhost:9200/_mapping?pretty'

    获取字段的mapping

    curl -XGET 'http://localhost:9200/twitter/_mapping/tweet/field/username'
    curl -XGET 'http://localhost:9200/twitter,bank/_mapping/field/message'
    curl -XGET 'http://localhost:9200/_all/_mapping/tweet,book/field/message,user.id'
    curl -XGET 'http://localhost:9200/_all/_mapping/tw*/field/*.id'
    curl -XGET 'http://localhost:9200/_all/_mapping/*/field/*?pretty'

    检测类型是否存在

    curl -XHEAD -i 'http://localhost:9200/bank/account?pretty'

    2.4 修改操作

    更新索引replicas数量

    索引的主shards数目在创建索引之后不能改变,但是replicas数目支持修改

    curl -XPUT http://localhost:9200/mytest/_settings -d '
    {
        "index" : {
        "number_of_replicas" : 4
    }
    }

    修改索引Analyzer

    #1. 先要关闭索引
    curl -XPOST 'http://localhost:9200/mytest/_close'
    # 2. 修改
    curl -XPUT 'http://localhost:9200/mytest/_settings' -d '{
    "analysis" : {
    "analyzer":{
    "content":{
    "type":"custom",
    "tokenizer":"whitespace"
    }
    }
    }
    }'
    # 3. 打开索引
    curl -XPOST 'http://localhost:9200/mytest/_open'

    2.5 其他操作

    获取设置信息

    curl -XGET 'http://localhost:9200/twitter/_settings'
    curl -XGET 'http://localhost:9200/twitter,kimchy/_settings'
    curl -XGET 'http://localhost:9200/_all/_settings'
    curl -XGET 'http://localhost:9200/2016-*/_settings'

    查看分词器分词效果

    curl -XGET http://localhost:9200/_analyze?pretty -d '
    {
    "analyzer" : "standard",
    "text" : "this is a macbook pro"
    }'

    你可以通过api查看使用某种es现有的analyzer分词效果

    如果你安装了ik,可以试试ik

    text支持使用数组

    而且可以设置为更为精细的级别,例如:

    analyzer" : "standard",
    "tokenizer" : "keyword",
    "token_filter" : ["lowercase"],
    "char_filter" : ["html_strip"],

    三、索引维护API

    es还提供了大量的统计api和运维api,以方便进行索引的运维工作

    查看索引统计信息

    curl 'http://localhost:9200/_stats?pretty'
    curl 'http://localhost:9200/bank/_stats?pretty'

    索引Segment

    提供了低级的Lucene中索引段信息

    curl -XGET 'http://localhost:9200/bank/_segments?pretty'
    curl -XGET 'http://localhost:9200/mytest,mytest2/_segments'
    curl -XGET 'http://localhost:9200/_segments'


    如果要想查看更详细的信息,可以在url上添加?verbose=true

    索引Recovery

    提供索引的shard recovery恢复信息

    curl -XGET 'http://localhost:9200/mytest,mytest2/_recovery?pretty'
    curl -XGET 'http://localhost:9200/_recovery?pretty&human'


    还可以添加detailed=true的参数,来查看更详细的信息

    索引Shard Store

    提供查看索引分片的存储信息,例如:

    curl -XGET 'http://localhost:9200/mytest/_shard_stores?pretty'
    curl -XGET 'http://localhost:9200/mytest,test2/_shard_stores'
    curl -XGET 'http://localhost:9200/_shard_stores'

    清除索引缓存

    ES内部使用了大量缓存机制以提高查询速度,用来清除1到多个索引相关的缓存,例如:

    curl -XPOST 'http://localhost:9200/mytest/_cache/clear?pretty'
    curl -XPOST 'http://localhost:9200/mytest,mytest2/_cache/clear'
    curl -XPOST 'http://localhost:9200/_cache/clear'

    Flush

    跟所有的flush功能一样,内存flush到磁盘上去,功能是把索引在内存里面的数据,存储到具体的存储器上,并删除相应的内部事务日志。

    curl -XPOST 'http://localhost:9200/mytest/_flush?pretty'
    curl -XPOST 'http://localhost:9200/mytest,mytest2/_flush'

    还支持以下参数

    • wait_if_ongoing:缺省是false,如果设置为true,将会阻塞并等待其它正在执行flush的功能执行完成,然后再执行。
    • force:是否有必要强制执行,即使没有改变也要flush。
    • synced: 同步flush

    Refresh

    刷新索引,使得上次refresh后的操作引起的变化,都能够反映到查询上。例如:

    curl -XPOST 'http://localhost:9200/mytest/_refresh?pretty'
    curl -XPOST 'http://localhost:9200/mytest,mytest2/_refresh'
    curl -XPOST 'http://localhost:9200/_refresh'

    Force Merge

    提供强制让索引里面的lucene段进行合并的功能,例如:

    curl -XPOST 'http://localhost:9200/mytest/_forcemerge?pretty'
    curl -XPOST 'http://localhost:9200/mytest,mytest2/_forcemerge'
    curl -XPOST 'http://localhost:9200/_forcemerge'

    可以设置的参数有:

    (1)max_num_segments:合并段的最大数量
    (2)only_expunge_deletes:是否在合并的时候,抹去已经删除的段
    (3)flush:执行合并后是否执行flush,默认是true

    四、索引相关配置

    内存控制器

    indices.breaker.total.limit: 总的内存使用大小,默认为JVM堆内存大小的70%。

    field数据内存大小

      列数据内存大小是指,在ES系统中,系统会估计有多少数据被加载到内存中,如果估计超过这个阀值,它可以通过一个异常来防止该字段的数据加载。
    indices.breaker.fielddata.limit:列数据内存大小的限制,默认为JVM堆内存大小的60%。
    indices.breaker.fielddata.overhead:所有列估计的内存大小的乘积,默认是1.03.

    请求控制器

    防止Elasticsearch每个请求的数据结构超过一定的值:
    indices.breaker.request.limit:请求控制器的大小,默认为JVM堆内存大小的40%。
    indices.breaker.request.overhead:所有请求的乘积,默认为1。

    数据缓存

      数据缓存主要用于当排序或聚合操作的时候。它将所有的字段值加载到内存中以便提供快速访问文档中的这些值。

    indices.fielddata.cache.size:数据缓存的最大值,可以是一个节点的堆内存大小的比例,例如30%,也可以是一个绝对数字,比如12GB。默认是无限制,可以最大的利用内存。这个配置是静态的配置,必须在集群中的每个数据节点上启动前配置好。可以通过curl -XGET
    http://localhost:9200/_nodes/stats?pretty请求来监控节点的使用情况。

    节点查询缓存

      查询缓存是负责缓存查询的结果。每个节点都有一个查询缓存,这个缓存为这个节点下的所有分片服务。这个缓存采用最近最少使用算法; 当缓存满时,把最少使用的数据优先删掉。查询缓存只有使用过滤的时候才会起作用


    indices.queries.cache.size:可以是一个节点的堆内存大小的比例,例如5%,也可以是一个绝对数字,比如 512mb。默认为JVM堆内存大小的10%。

    索引缓冲区

      索引缓冲区用于存储新的索引文档。当缓冲区满后,缓冲区中的文件被写入磁盘上的一个段,它会在节点的所有分片上分离。它的设置是静态的,并且必须在群集中的每个数据节点上配置。

    indices.memory.index_buffer_size:一个节点索引缓冲区的大小,可以是一个节点的堆内存大
    小的比例获知是一个绝对数字。默认为JVM堆内存大小的10%。
    indices.memory.min_index_buffer_size:可以使用此设置指定最小的索引缓冲区大小。默认为48MB。
    indices.memory.max_index_buffer_size:可以使用此设置指定最大的索引缓冲区大小。默认为无限制。
    indices.memory.min_shard_index_buffer_size:分配给每个分片索引缓冲区的内存最小值,默认4MB。

    分片请求缓存

    当一个搜索请求是对一个索引或者多个索引的时候,每一个分片都是进行它自己内容的搜索然后把结果返回到协调节点,然后把这些结果合并到一起统一对外提供。分片缓存模块缓存了这个分片的搜索结果。这使得搜索频率高的请求会立即返回。
    注意:请求缓存只缓存查询条件 size=0的搜索,缓存的内容有hits.total, aggregations,
    suggestions,不缓存原始的hits。通过now查询的结果将不缓存。

    缓存说明

    只有在分片的数据实际上发生了变化的时候刷新分片缓存才会失效。刷新的时间间隔越长,缓存的数据越多,当缓存不够的时候,最少使用的数据将被删除。缓存过期可以手工设置,例如:

    localhost:9200/kimchy,elasticsearch/_cache/clear?request_cache=true

    默认情况下缓存未启用,但在创建新的索引时可启用,例如:

    PUT localhost:9200/my_index
    { "settings": { "index.requests.cache.enable": true }}

    当然也可以通过动态参数配置来进行设置:

    PUT localhost:9200/my_index/_settings -d'
    { "index.requests.cache.enable": true }'

    每次请求凑可以通过查询字符串参数request_cache可用于启用或禁用每个请求的缓存。例如:

    localhost:9200/my_index/_search?request_cache=true
    { "size": 0,
    "aggs": {
    "popular_colors": {
    "terms": {
    "field": "colors"
    } } }}

    1:如果结果是不确定的(例如,它使用一个随机函数或引用当前时间)应该设置request_cache为false禁用请求缓存。

    2:数据的缓存是整个JSON,这意味着如果JSON发生了变化 ,如顺序不同,缓存的内容将会不同。

    监控缓存使用

    可以通过localhost:9200/_stats/request_cache?pretty&human或者
    'localhost:9200/_nodes/stats/indices/request_cache?pretty&human来缓存监控,缓存的大小(以字节为单位)。

    索引恢复

    indices.recovery.concurrent_streams:默认为3
    indices.recovery.concurrent_small_file_streams:默认为2
    indices.recovery.file_chunk_size:默认为512KB
    indices.recovery.translog_ops:默认为1000
    indices.recovery.translog_size:默认为512KB
    indices.recovery.compress:默认为true
    indices.recovery.max_bytes_per_sec:默认为40MB

    TTL区间

    文档有个ttl值可以设置当过期的时候是否需要删除,设置如下:
    indices.ttl.interval:删除程序的运行时间。默认为60
    indices.ttl.bulk_size:删除处理与批量请求的数量,默认为10000

  • 相关阅读:
    设计模式(23)>解释器模式 小强斋
    设计模式(21)>享元模式 小强斋
    访问者模式与双分派 小强斋
    将BMP格式的灰度图像或24位真彩图像二值化
    如何使用自定义Title
    ACM. Digital Roots
    jpegEncode
    ACM.Ones
    剩余定理
    ACM.DNA Sorting
  • 原文地址:https://www.cnblogs.com/carl10086/p/6052841.html
Copyright © 2020-2023  润新知