elasticsearch

ElasticSearch(二):Mapping

2018-11-21  本文已影响34人  采风JS

一 自定义mapping

##dynamic控制字段新增
##true:默认允许自动新增字段; false:不允许自动新增字段,文档可以正常写入,但是无法对该字段进行查询;strict:文档不能写入;
PUT my_index
{
  "mappings": {
    "doc": {
      "dynamic":false,
      "properties": {
        "title": {
          "type": "text"
        },
        "name": {
          "type": "keyword"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  }
}
GET my_index/_mapping
PUT my_index/doc/1
{
  "title":"hello,world",
  "desc":"nothing here" ##dynamic为false,可以添加文档,但是不能查询该字段;
}

二 常用参数设置

PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "first_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name" 
        },
        "full_name": {
          "type": "text"
        }
      }
    }
  }
}
PUT my_index/doc/1
{
  "first_name": "John",
  "last_name": "Smith"
}
GET my_index/_search
{
  "query": {
    "match": {
      "full_name": {  ##full_name中同时包含John和Smith
        "query": "John Smith",
        "operator": "and" 
      }
    }
  }
}
PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "cookie": {
          "type": "text",
          "index": false
        }
      }
    }
  }
}
PUT my_index/doc/1
{
  "cookie":"name=alfred"
}
GET my_index/_search
{
  "query":{
    "match": {
      "cookie": "name" ##Cannot search on field [cookie] since it is not indexed
    }
  }
}
docs: 记录doc id
freqs: 记录doc id 和 term frequencies
positions: 记录doc id/term frequencies/term position
offsets: 记录doc id/term frequencies/term position/character offsets
PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "status_code": {
          "type":       "keyword",
          "null_value": "NULL" 
        }
      }
    }
  }
}
PUT my_index/my_type/1
{
  "status_code": null
}
PUT my_index/my_type/2
{
  "status_code": [] 
}
GET my_index/_search
{
  "query": {
    "term": {
      "status_code": "NULL" 
    }
  }
}

三 数据类型

字符串型: text(分词) keyword
数值型: long integer short byte double float half_float scaled_float
日期类型: date
布尔类型: boolean
二进制类型: binary
范围类型: integer_range float_range long_range double_range date_range
 数组类型: array
 对象类型: object
 嵌套类型: nested object
geo_point
geo_shape
记录ip地址: ip
实现自动补全: completion
记录分词数: token_count
记录字符串hash值: murmur3
percolator
join
允许对用一个字段采用不同的配置,比如分词,常见例子如对任命实现拼音搜索,仅需再人名中增加pinyin子字段即可;

四 Dynamic Mapping

dynamic_date_formats:自定义日期类型
PUT my_index
{
  "mappings":{
    "my_type":{
      "dynamic_date_formats":["MM/dd/yyyy"]
    }
  }
}
不定义mapping,添加文档后的日期格式识别为text类型
numeric_detection: 开启字符串中数字自动识别
##将字符串中的数字识别为数值类型
PUT my_index
{
  "mappings":{
    "my_type":{
      "numeric_dectection":true
    }
  }
}
PUT my_index/my_type/1
{
  "my_float":"1.0",
  "my_integer":"1"
}
允许根据es自动识别的数据类型/字段名等来动态设定字段类型;
##match_mapping_type匹配自动识别字段类型,match,unmatch匹配字段名,path_match,path_unmath匹配路径
PUT my_product_index
{
  "mappings": {
    "doc": {
      "dynamic_templates": [
        {
          "strings": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword"
            }
          }
        }
      ]
}

五 Index Template

PUT _template/test_template
{
  "index_patterns": ["te*", "bar*"],
  "order":0,
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "doc": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "name": {
          "type": "keyword"
        }
      }
    }
  }
}
PUT _template/test_template2
{
  "index_patterns": ["test*"],
  "order":1,
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "doc": {
      "_source": {
        "enabled": true
      }
    }
  }
}
PUT test_index
GET test_index/ ##order大的会覆盖小的
上一篇 下一篇

猜你喜欢

热点阅读