【Elasticsearch】Indices APIs
博客专区 > HarryWu 的博客 > 博客详情
【Elasticsearch】Indices APIs
HarryWu 发表于2年前
【Elasticsearch】Indices APIs
  • 发表于 2年前
  • 阅读 29
  • 收藏 0
  • 点赞 1
  • 评论 0

【腾讯云】新注册用户域名抢购1元起>>>   


一、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 }
        }
    }}'


  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 5
博文 106
码字总数 47792
×
HarryWu
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: