ElasticSearch常用操作命令(持續更新)

查看索引信息

# 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"
    }
}
'
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章