文章目录
查看索引信息
# curl -X GET "172.17.81.43:9200/company_486470145429897216_v02?pretty=true"
{
"company_486470145429897216_v02" : {
"aliases" : {
"company_486470145429897216_alias" : { }
},
"mappings" : {
"_doc" : {
"properties" : {
"createDate" : {
"type" : "date"
},
"createPersonName" : {
"type" : "keyword"
},
"fileExtendName" : {
"type" : "keyword"
},
"fileFullName" : {
"type" : "text",
"fields" : {
"text" : {
"type" : "text",
"analyzer" : "ik_max_word"
}
},
"analyzer" : "like_analyzer"
},
"fileSize" : {
"type" : "long"
},
"fileType" : {
"type" : "integer"
},
"url" : {
"type" : "text",
"index" : false
}
}
}
},
"settings" : {
"index" : {
"number_of_shards" : "5",
"blocks" : {
"read_only_allow_delete" : null
},
"provided_name" : "company_486470145429897216_v02",
"creation_date" : "1536830152381",
"analysis" : {
"analyzer" : {
"like_analyzer" : {
"tokenizer" : "like_tokenizer"
}
},
"tokenizer" : {
"like_tokenizer" : {
"type" : "ngram",
"min_gram" : "1",
"max_gram" : "1"
}
}
},
"number_of_replicas" : "1",
"uuid" : "FdkP6yVaT6yf8XkjAYHBfQ",
"version" : {
"created" : "6020499"
}
}
}
}
}
查看索引别名和索引的映射关系
# curl -X GET "http://172.17.81.43:9200/_cat/aliases?v&s=index"
alias index filter routing.index routing.search
company_100085_alias company_100085_v02 - - -
company_100094_alias company_100094_v02 - - -
company_100138_alias company_100138_v02 - - -
company_100163_alias company_100163_v02 - - -
查看索引列表
# curl -X GET "http://172.17.81.43:9200/_cat/indices?v&h=i&s=i"
i
company_100085
company_100094
company_100138
company_100163
查看索引数据(文档)信息
curl -X GET "ip:9200/{index}/{type}/_search
index 索引,多个用逗号间隔
type 类型,多个用逗号间隔
# curl -X GET "192.168.3.162:9200/company_477498452494999552_v02/_search?pretty=true"
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [
{
"_index" : "company_477498452494999552_v02",
"_type" : "_doc",
"_id" : "1525594509312",
"_score" : 1.0,
"_source" : {
"fileExtendName" : "jpg",
"fileName" : "压力测试环境",
"updateDate" : 1533887744124,
"updatePersonName" : "15505413330",
"fileFullName" : "压力测试环境.jpg",
"remark" : "",
"createPersonId" : "1001",
"parentId" : "477498452494999552",
"url" : "2139054955251",
"fileSize" : 115518,
"id" : "1525594509312",
"createPersonName" : "15505413330",
"fileType" : 1,
"updatePersonId" : "1001",
"createDate" : 1533887744124
}
},
...
]
}
}
新建索引
curl -X PUT "192.168.3.162:9200/clusterlockexception_v02" -H 'Content-Type: application/json' -d'
{
"settings": {
"blocks": {
"read_only_allow_delete": null
},
"analysis": {
"analyzer": {
"like_analyzer": {
"tokenizer": "like_tokenizer"
}
},
"tokenizer": {
"like_tokenizer": {
"type": "ngram",
"min_gram": "1",
"max_gram": "1"
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"fileName": {
"type": "keyword"
},
"updateDate": {
"type": "date"
},
"fileFullName": {
"analyzer": "like_analyzer",
"search_analyzer": "like_analyzer",
"type": "text",
"fields": {
"text": {
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"type": "text"
}
}
},
"url": {
"index": false,
"type": "text"
}
}
}
}
}'
新建索引时定义索引别名
在新建索引的同时,定义索引的别名,可以定义多个别名,不同别名
curl -X PUT "192.168.3.162:9200/my_index" -H 'Content-Type: application/json' -d'
{
"settings": {
// 省略...
},
"mappings": {
// 省略...
},
"aliases" : {
"alias_1" : {},
"alias_2" : {
"filter" : {
"term" : {"key" : value }
}
}
}
}'
删除索引
curl -X DELETE "192.168.3.162:9200/company_100406_v02_v02"
上面的命令需要指定特定索引名或者通配符表达式,索引别名不能用来作为删除索引的参数,通配符表达式被解析为相匹配的具体索引。
支持多索引语法,用逗号间隔。
删除所有索引,使用 _all 或者 * 作为索引参数。
禁止使用通配符或者 _all 来删除索引,在配置中设置 action.destructive_requires_name 为 true 即可。该配置可以通过使用集群更新配置接口来修改。
添加索引别名
curl -X POST "192.168.3.162:9200/_aliases" -H 'Content-Type: application/json' -d'
{
"actions" : [
{ "add" : { "index" : "company_489521805720043520_v02", "alias" : "company_489521805720043520_alias" } }
]
}'
删除索引别名
curl -X POST "192.168.3.162:9200/_aliases" -H 'Content-Type: application/json' -d'
{
"actions" : [
{ "remove" : { "index" : "company_489516259906600960_v02", "alias" : "company_489516259906600960_v02_alias" } }}
]
}'
修改索引别名
该操作为原子操作
curl -X POST "192.168.3.162:9200/_aliases" -H 'Content-Type: application/json' -d'
{
"actions" : [
{ "remove" : { "index" : "company_489516259906600960_v02", "alias" : "company_489516259906600960_v02_alias" } },
{ "add" : { "index" : "company_489516259906600960_v02", "alias" : "company_489516259906600960_alias" } }
]
}'
复制索引
curl -X POST "192.168.3.162:9200/_reindex" -H 'Content-Type: application/json' -d'
{
"conflicts": "proceed",
"source": {
"index": "company_100406_alias"
},
"dest": {
"index": "company_100406_v02",
"op_type": "create",
"version_type": "external"
}
}'
刷新缓存中的索引到磁盘
curl -X POST "localhost:9200/_flush?pretty"
返回
{
"_shards" : {
"total" : 270,
"successful" : 135,
"failed" : 0
}
}
精确检索
实际需求:除了文件名模糊匹配之外,再添加按上传时间范围、上传人姓名、文件大小范围来检索文件信息
检索超时设置
全局超时设置,key为【search.default_search_timeout】,默认无设置
curl -X PUT "192.168.3.162:9200/_cluster/settings?pretty" -H 'Content-Type: application/json' -d'
{
"persistent" : {
"search.default_search_timeout" : "30s"
}
}
'