美文网首页
elasticsearch 映射

elasticsearch 映射

作者: liujianhuiouc | 来源:发表于2018-07-22 21:39 被阅读0次

    映射

    查询mapping映射关系

    GET /megacorp/_mapping/employee
    

    测试分词器

    GET /_analyze
    {
      "analyzer": "standard",
      "text": "Text to analyze"
    }
    

    分词器

    1. 标准分词器:先通过空白或者标点符号进行分词,然后转换为小写进行索引存储 standard
    2. 空白分词器:通过空格进行词语切割,whitespace
    3. 英文分词器:english

    字符串索引类型

    1. no: 无索引,该字段不能被检索
    2. anlayzed: 全文检索
    3. not_analyzed 不进行分词参与全文检索,只支持精确匹配

    创建映射

    {
      "mappings": {
        "tweet" : {
          "properties" : {
            "tweet" : {
              "type" :    "string",
              "analyzer": "english"
            },
            "date" : {
              "type" :   "date"
            },
            "name" : {
              "type" :   "string"
            },
            "user_id" : {
              "type" :   "long"
            }
          }
        }
      }
    }
    
    

    添加新域的映射

    PUT /gb/_mapping/tweet
    {
      "properties" : {
        "tag" : {
          "type" :    "string",
          "index":    "not_analyzed"
        }
      }
    }
    

    测试分词器

    GET /gb/_analyze
    {
    "field":"tag"
    "text":"hello world"
    }
    

    索引部分

    索引创建
    PUT /my_index
    
    {
    "settings": {...any settings...},
    "type_one": {...mappings...},
    "type_two": {...mappings...}
    }
    
    GET /my_index
    {
    "my_index": {
    "aliases": {},
    "mappings": {},
    "settings": {
    "index": {
    "creation_date": "1532185148306",
    "number_of_shards": "5",
    "number_of_replicas": "1",
    "uuid": "rttbjijgTM2vvySvkyXxLA",
    "version": {
    "created": "6030199"
    },-
    "provided_name": "my_index"
    }-
    }-
    }-
    }
    
    索引删除
    DELETE /my_index
    DELETE /index1,index2
    DELETE /index_*
    DELETE /_all
    DELETE /*
    
    索引配置的修改
    PUT /my_temp_index/_settings
    {
        "number_of_replicas": 1 //副本数设置修改为1
    }
    

    ######设置不存储_source字段

    PUT /my_index
    {
    "mappings":{
    "my_type":{
    "_source":{
    "enable":false
    }
    }
    }
    }
    

    不开启_all字段

    PUT /my_index/my_type/_mapping
    {
        "my_type": {
            "_all": { "enabled": false }
        }
    }
    

    新字段时处理策略
    设置类型的dynamic字段属性:

    取值 含义
    true 遇到新字段时自动识别
    false 忽略掉新字段
    strict 遇到新字段抛异常StrictDynamicMappingException
    PUT http://localhost:9201/liujianhui/my_type/_mapping
    {
            "my_type": {
                "dynamic":"strict"
        }
    }
    
    GET /liujianhui/my_type/_mapping
    {
    "liujianhui": {
    "mappings": {
    "my_type": {
    "dynamic": "strict",
    "_source": {
    "enabled": false
    },-
    "properties": {
    "hello": {
    "type": "text",
    "fields": {
    "keyword": {
    "type": "keyword",
    "ignore_above": 256
    }-
    }-
    },-
    "my_type": {
    "properties": {
    "dynamic": {
    "type": "text",
    "fields": {
    "keyword": {
    "type": "keyword",
    "ignore_above": 256
    }-
    }-
    }-
    }-
    }-
    }-
    }-
    }-
    }-
    }
    

    dynamic字段支持动态更新配置,false字段也会被保留在_source中,只是不支持索引,内容还是会保持在json体中

    索引别名
    POST /_aliases
    
    {
        "actions": [{
                "add": {
                    "index": "liujianhui",
                    "alias": "my_index"
                }
            },
            {
                "add": {
                    "index": "megacorp",
                    "alias": "my_index"
                }
            }
        ]
    }
    
    有哪些索引的别名叫做my_index
    GET /*/_alias/my_index
    
    索引拥有哪些别名
    GET /megacorp/_alias/*
    

    只查询指定分片或者某几个分片
    http://localhost:9201/megacorp/employee/_search?preference=_shards:0,1
    取值:_primary, _primary_first, _local, _only_node:xyz, _prefer_node:xyz

    更新

    设置索引的更新时间,这一段时间内会造成数据丢失

    PUT /blogs
    {
    "settings":{
    "refresh_interval": "30s"
    }
    }
    
    PUT /blogs/_settings
    {
    "refresh_interval":"1s"
    }
    
    关闭刷新
    PUT /blogs/_settigs
    {
    "refresh_interval":"1s"
    }
    
    PUT /my_index/_settings
    {
        "index.translog.durability": "async",
        "index.translog.sync_interval": "5s"
    }
    "index.translog.durability": "request" 同步请求
    

    合并段请求
    POST /logstash-2014-10/_optimize?max_num_segments=1

    相关文章

      网友评论

          本文标题:elasticsearch 映射

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