Elasticsearch學習隨筆(二)-- Index 和 Doc 查詢新建API總結

  本文着重總結Elasticsearch的常見API了,進行分析。html

  • Index API
  1. 初始化Index,設置shards和replica
PUT http://localhost:9200/firewall_syslog/
    {
        "settings":{
            "index":{
                "number_of_shards":5,
                "number_of_replicas":0
             }

         }
    }

  能夠獲得建立成功的JSON返回:api

{
"acknowledged": true,
"shards_acknowledged": true
}

  2. 得到索引的詳細信息:ide

  獲取單個索引信息:ui

GET http://localhost:9200/firewall_syslog/_settings/

  返回JSON值:server

{
    "firewall_syslog": {
        "settings": {
            "index": {
                "creation_date": "1499588503266",
                "number_of_shards": "5",
                "number_of_replicas": "0",
                "uuid": "DTeXCyRcRGqhIMkBjupyLg",
                "version": {
                    "created": "5040399"
                },
                "provided_name": "firewall_syslog"
            }
        }
    }
}

  得到多個索引:htm

GET http://localhost:9200/server_syslog,firewall_syslog/_settings/

  可得到返回的JSON值:blog

{
    "server_syslog": {
        "settings": {
            "index": {
                "creation_date": "1499324705761",
                "number_of_shards": "5",
                "number_of_replicas": "0",
                "uuid": "x_ke_3yhR2ycMPumgrDEvw",
                "version": {
                    "created": "5040399"
                },
                "provided_name": "server_syslog"
            }
        }
    },
    "firewall_syslog": {
        "settings": {
            "index": {
                "creation_date": "1499588503266",
                "number_of_shards": "5",
                "number_of_replicas": "0",
                "uuid": "DTeXCyRcRGqhIMkBjupyLg",
                "version": {
                    "created": "5040399"
                },
                "provided_name": "firewall_syslog"
            }
        }
    }
}

  得到全部索引信息:索引

GET http://localhost:9200/_all/_settings/

  可得到返回JSON值:接口

{
    "server_syslog": {
        "settings": {
            "index": {
                "creation_date": "1499324705761",
                "number_of_shards": "5",
                "number_of_replicas": "0",
                "uuid": "x_ke_3yhR2ycMPumgrDEvw",
                "version": {
                    "created": "5040399"
                },
                "provided_name": "server_syslog"
            }
        }
    },
    "hardware_syslog": {
        "settings": {
            "index": {
                "creation_date": "1499324723964",
                "number_of_shards": "5",
                "number_of_replicas": "0",
                "uuid": "0Mmg81DJR0GWQ3JLTeyUbg",
                "version": {
                    "created": "5040399"
                },
                "provided_name": "hardware_syslog"
            }
        }
    },
    "firewall_syslog": {
        "settings": {
            "index": {
                "creation_date": "1499588503266",
                "number_of_shards": "5",
                "number_of_replicas": "0",
                "uuid": "DTeXCyRcRGqhIMkBjupyLg",
                "version": {
                    "created": "5040399"
                },
                "provided_name": "firewall_syslog"
            }
        }
    }
}

  3. 新建文檔與內容ip

  使用PUT來新建建Elasticsearch文檔內容:

PUT http://localhost:9200/firewall_syslog/name/1/
{
    "name": "cisco",
    "version": "1.7.1",
    "writer": {
        "first": "larry",
        "second": "tim"
    },
    "syslog": "1"
}

  返回的JSON信息爲:

{
    "_index": "firewall_syslog",
    "_type": "name",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    },
    "created": false
}

  4. 更新文檔中的字段(覆蓋更新與Update更新)

  使用POST方法覆蓋更新文檔關鍵內容:

POST http://localhost:9200/firewall_syslog/name/1/
{
    "name": "cisco",
    "version": "1.7.3",
    "writer": {
        "first": "larry",
        "second": "tim"
    },
    "syslog": "3"
}

  返回JSON關鍵字updated:

{
    "_index": "firewall_syslog",
    "_type": "name",
    "_id": "1",
    "_version": 11,
    "found": true,
    "_source": {
        "name": "cisco",
        "version": "1.7.3",
        "writer": {
            "first": "larry",
            "second": "tim"
        },
        "syslog": "3"
    }
}

  使用update接口更新文檔內容,修改name字段爲juniper:

POST http://localhost:9200/firewall_syslog/name/1/_update/
{
	"doc":{
		"name":"juniper"
	}
}

  返回JSON的值爲:

{
    "_index": "firewall_syslog",
    "_type": "name",
    "_id": "1",
    "_version": 12,
    "result": "updated",
    "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
    }
}

  5. 搜索doc中的關鍵字段:

  不過出了一些未知的小故障,題住用的是ELasticsearch 5.x版本。不知道爲什麼在head中調用api沒法實現如下內容。

GET http://localhost:9200/server_syslog/secure/1?_source=user/

  後面將總結mget與bulk接口。

相關文章
相關標籤/搜索