【Elasticsearch】Indices APIs


一、Bulk API

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

The bulk API makes it possible to perform many index/delete operations in a single API call. This can greatly increate the indexing speed.


The REST API endpoint is /_bulk ,and it expects the following JSON structure:

actiion_and_meta_data\n
optional_source\n
actiion_and_meta_data\n
optional_source\n
...
...
actiion_and_meta_data\n
optional_source\n

The possible actions are index,create,delete and update.

二、Put Mapping

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

Put Mapping API 能够让你为存在的Index创建新的Type,或是为存在的Type创建新的Fields。(The Put mapping API allows you to add a new type on an existing index,or new fields to an existing type)

curl -XPUT 'http://localhost:9200/twitter' -d '{       
  "mappings": {
    "tweet": {
      "properties": {
        "message": {
          "type": "string"
        }
      }
    }
  }
}'                            ==> 创建Index:twitter,创建Type:tweet,创建Fields:message

curl -XPUT 'http://localhost:9200/twitter/_mapping/tweet' -d '{
  "properties": {
    "user_name": {
      "type": "string"
    }
  }
}'                            ==> 为Type:tweet,新增Fields:user_name

curl -XPUT 'http://localhost:9200/twitter/_mapping/user' -d '{
  "properties": {
    "name": {
      "type": "string"
    }
  }
}'                            ==> 为Index:twitter,新增Type:user,和新的Fields:name

Multi-Index:

curl -XPUT 'http://localhost:9200/my_index' -d '{
  "mappings": {
    "user": {
      "properties": {
        "name": {
          "properties": {
            "first": {
              "type": "string"
            }
          }
        },
        "user_id": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}'                ==>创建Index:my_index,并拥有一个Type:user。Type:user拥有两个Fields:name,first


curl -XPUT 'http://localhost:9200/my_index/_mapping/user' -d '{
  "properties": {
    "name": {
      "properties": {
        "last": {                ==> 在存在的Fields新增properties
          "type": "string"
        }
      }
    },
    "user_id": {
      "type": "string",
      "index": "not_analyzed",
      "ignore_above": 100        ==> 更新原有的ignore_above从0到100
    }
  }
}'               ==>

三、Index Templates

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-templates.html

Index Templates 当你定义了一个template。新的indx创建时候,只有匹配到template的pattern string。该template的settings,mappings,alias定义就会被应用到新创建的index上。(Index Templates allow you to define templates that will automatically be applied when new indices are created.The templates include both settings and mappings and a simple pattern template that controls whether the templates should be applied to the new index.)

Note: Templates are only applied at index creation time.Changing a template will have no impact on existsing indices.

curl -XPUT 'localhost:9200/_template/template_1' -d 
'{
  "template": "te*",        ==> pattern
  "mappings": {
    "type1": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "host_name": {
          "type": "string",
          "index": "not_analyzed"
        },
        "create_at": {
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z YYYY"
        }
      }
    }
  }
}'        ==> Define a template named template_1,with a template pattern of te*.The mappings will be applied to any index name that matches the te* template

Multiple Template Matching:

Multiple index templates can potentially match an index,in this case,both the settting and mappings are merged into the final ocnfiguration of the index.The order of the merging can be controlled useing order parameter,with lower order being applied first, and higher orders overriding them.

curl -XPUT localhost:9200/_template/template_1 -d '{
    "template" : "*",
    "order" : 0,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : false }
        }
    }}'

curl -XPUT localhost:9200/_template/template_2 -d '{
    "template" : "te*",
    "order" : 1,
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "type1" : {
            "_source" : { "enabled" : true }
        }
    }}'


转载于:https://my.oschina.net/u/204498/blog/656674

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值