elasticsearch 命令

es 2.3的相关的索引命令记录。
官方文档参考

索引相关

参考

创建索引

$ curl -XPUT 'http://localhost:9200/twitter/' -d '{
"settings" : {
"index" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}
}'
  • number_of_shards 主分片数 默认 5
  • number_of_replicas 每个主分片的副本数 1

简写:

$ curl -XPUT 'http://localhost:9200/twitter/' -d '{
"settings" : {
"number_of_shards" : 3,
"number_of_replicas" : 2
}
}'

可以创建索引时,同时指定mapping结构:


curl -XPOST localhost:9200/test -d '{
"settings" : {
"number_of_shards" : 1
},
"mappings" : {
"type1" : {
"properties" : {
"field1" : { "type" : "string", "index" : "not_analyzed" }
}
}
}
}'

properties.index说明:

  • analyzed (默认项)分析,将单词转为小写,并切分单词,用于搜索
  • not_analyzed 不分析,字符串作为一个整体分析,用于精确匹配
  • no 不用于索引,无法用于搜索。对不需要搜索只用于存储的字段设置为此,可以节省空间,缩短索引和搜索时间。

示例创建客户:

curl -X POST \
http://127.0.0.1:9200/customer \
-H 'cache-control: no-cache' \
-d '{
"settings":{
"number_of_shards": 7,
"number_of_replicas":1
},
"mappings" : {
"customer" : {
"properties" : {
"account" : {
"type" : "string",
"index" : "not_analyzed"
},
"name" : {
"type" : "string",
},
"phone" : {
"type" : "string",
"index" : "not_analyzed"
},
"createTime" : {
"type" : "string",
"index" : "no"
},
"notifyTime": {
"type": "string",
"index" : "no"
}
}
}
}
}'
curl -X POST \
http://127.0.0.1:9200/kmdoc \
-H 'Content-Type: application/json' \
-H 'cache-control: no-cache' \
-d '{
"mappings" : {
"kmdoc" : {
"properties" : {
"accountId" : {
"type" : "string",
"index" : "not_analyzed"
},
"title" : {
"type" : "string"
},
"content" : {
"type" : "string"
},
"cid" : {
"type" : "string",
"index" : "not_analyzed"
},
"kmType" : {
"type" : "string",
"index" : "not_analyzed"
},
"lastTime" : {
"type" : "string",
"format": "no"
}
}
}
}
}'

查看索引

$ curl -XGET 'http://localhost:9200/twitter/'
# 过滤字段,只返回setting和mapping
$ curl -XGET 'http://localhost:9200/twitter/_settings,_mappings'

删除索引

$ curl -XDELETE 'http://localhost:9200/twitter/'

PUT mapping

可以用这个语法来添加索引,给指定索引类型添加字段,添加类型,有下面三种形式

PUT twitter 
{
"mappings": {
"tweet": {
"properties": {
"message": {
"type": "string"
}
}
}
}
}

PUT twitter/_mapping/user
{
"properties": {
"name": {
"type": "string"
}
}
}

PUT twitter/_mapping/tweet
{
"properties": {
"user_name": {
"type": "string"
}
}
}

查看mapping

curl -XGET 'http://localhost:9200/twitter/_mapping/tweet'
host:port/{index}/_mapping/{type}
curl -XGET 'http://localhost:9200/_all/_mapping/tweet,book'

更新索引设置

更新路径是/_settings or {index}/_settings,

下面的例子设置了某个index的副本分片数:

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

# 修改刷新间隔
curl -XPUT localhost:9200/test/_settings -d '{
"index" : {
"refresh_interval" : "1s"
} }'

迁移时优化

  • 更新设置api可以实时的修改索引间隔“refresh_interval”:“ - 1”,设置为-1,关闭自动刷新;当迁移完成之后,恢复"refresh_interval" : "1s"
  • 在迁移前,可以设置分片数为0,加快迁移速度。

迁移前:

curl -XPUT localhost:9200/test/_settings -d '{
"index" : {
"refresh_interval" : "-1",
"number_of_replicas" : 0
} }'

curl -XPUT localhost:9200/test/_settings -d '{
"index" : {
"refresh_interval" : "1s",
"number_of_replicas" : 1
} }'

刷新

$ curl -XPOST 'http://localhost:9200/twitter/_refresh'
$ curl -XPOST 'http://localhost:9200/kimchy,elasticsearch/_refresh'

可以使用手动强制刷新,使索引马上可用于搜索。默认情况下,索引是根据refresh_interval控制的。

强制合并

force merge API允许通过API强制合并一个或多个索引。合并与Lucene索引在每个分片中保存的段数相关。强制合并操作允许通过合并它们来减少段的数量。

此调用将阻止,直到合并完成。如果http连接丢失,请求将在后台继续,并且任何新请求将阻塞,直到上一次强制合并完成。

$ curl -XPOST 'http://localhost:9200/twitter/_forcemerge'
  • max_num_segments 合并后的最大段文件数,设置为1完全合并。
  • only_expunge_deletes ,Should the merge process only expunge segments with deletes in it. In Lucene, a document is not deleted from a segment, just marked as deleted. During a merge process of segments, a new segment is created that does not have those deletes. This flag allows to only merge segments that have deletes. Defaults to false. Note that this won’t override the index.merge.policy.expunge_deletes_allowed threshold.
  • flush true 合并后是否执行刷新

优化

$ curl -XPOST ‘http://localhost:9200/twitter/_optimize

参考