Elasticsearch的增刪改查

Elasticsearch的增刪改查

在使用elasticsearch-head建立索引的時候就咱們查看索引的時候能夠看到mappings的關鍵字,該關鍵字是建立結構化索引的關鍵字。若是使用head來建立索引,那麼該索引是非結結構化的。若是要建立結構化的索引,須要使用如下的方式app

除此以外咱們也能夠使用postman去建立結構化的索引less

在elasticsearch-head能夠看到以下的信息:elasticsearch

此時咱們就建立好了一個結構化的索引post

插入數據

插入自定義id的文檔

查看elasticsearch-head的數據以下:spa

elasticsearch分配id

elasticsearch一樣能夠看到我剛剛插入的數據3d

修改數據

直接修改文檔

在elastisearch-head能夠查看到code

腳本修改文檔

http://localhost:9200/people/_doc/1/_updatecdn

{
	"script":{
	    #ES內置的腳本
		"lang":"painless",
		#ctx是es的上下文,這裏是將年齡+10
		"inline":"ctx._source.age+=10"
	}
}
複製代碼

返回結果blog

{
    "_index": "people",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 3
}
在elasticsearch-head也能夠看到數據的變化
複製代碼

除了上面的方式更新以外,咱們還能夠把更新的數據做爲參數提出來索引

{
	"script":{
		"lang":"painless",
		"inline":"ctx._source.age=params.age",
		"params":{
		"age":30
	}
	}
}
複製代碼

在elasticsearch-head就能夠發現shuhu 變化

刪除

刪除一個文檔

此時elasticsearch-head已經沒有了這個文檔

刪除一個索引

再去elasticsearch-head查看發現已經不存在該索引以及該索引如下的數據了,索引刪除操做時一個比較危險的操做,必定要很當心

查詢

簡單查詢

咱們只須要http://localhost:9200/book/_doc/1發送請求就能夠獲取到id爲1的文檔
複製代碼

條件查詢

聚合查詢

須要注意的是Elasticsearch沒法對文本進行聚合

{
	"aggs":{
		"group_by_word_count":{
			"terms":{
				"field":"word_count"
			}
		},
		"group_by_publish_date":{
			"terms":{
				"field":"publish_date"
			}
		}
	}
	
}
複製代碼

返回結果以下:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 10,
            "relation": "eq"
        },
        "max_score": 1,
        "hits": [
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "5",
                "_score": 1,
                "_source": {
                    "author": "王八",
                    "word_count": 3000,
                    "title": "Python入門",
                    "publish_date": "2017-03-10"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "7",
                "_score": 1,
                "_source": {
                    "author": "石仔",
                    "word_count": 10000,
                    "title": "石仔的吃貨史",
                    "publish_date": "2018-06-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "author": "李四",
                    "word_count": 2000,
                    "title": "李啓明的成長記",
                    "publish_date": "2018-06-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "3",
                "_score": 1,
                "_source": {
                    "author": "王五",
                    "word_count": 2000,
                    "title": "Elasticsearch入門",
                    "publish_date": "2018-10-01"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "4",
                "_score": 1,
                "_source": {
                    "author": "趙六",
                    "word_count": 100000,
                    "title": "Elasticsearch進階",
                    "publish_date": "2017-11-11"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "10",
                "_score": 1,
                "_source": {
                    "author": "jack",
                    "word_count": 5000,
                    "title": "Elasticsearch的深刻理解",
                    "publish_date": "2018-05-09"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "1",
                "_score": 1,
                "_source": {
                    "author": "張三",
                    "word_count": 1000,
                    "title": "旺仔的成長記",
                    "publish_date": "2018-06-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "6",
                "_score": 1,
                "_source": {
                    "author": "老九",
                    "word_count": 3000,
                    "title": "老乾媽的回憶錄",
                    "publish_date": "2018-03-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "8",
                "_score": 1,
                "_source": {
                    "author": "李啓明",
                    "word_count": 2000,
                    "title": "旺仔回憶錄",
                    "publish_date": "2017-06-02"
                }
            },
            {
                "_index": "book",
                "_type": "_doc",
                "_id": "9",
                "_score": 1,
                "_source": {
                    "author": "旺仔",
                    "word_count": 2000,
                    "title": "李啓明回憶錄",
                    "publish_date": "2017-06-01"
                }
            }
        ]
    },
    "aggregations": {
        "group_by_publish_date": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 1527897600000,
                    "key_as_string": "2018-06-02 00:00:00",
                    "doc_count": 2
                },
                {
                    "key": 1489104000000,
                    "key_as_string": "2017-03-10 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1496275200000,
                    "key_as_string": "2017-06-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1496361600000,
                    "key_as_string": "2017-06-02 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1510358400000,
                    "key_as_string": "2017-11-11 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1519948800000,
                    "key_as_string": "2018-03-02 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1525824000000,
                    "key_as_string": "2018-05-09 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1527811200000,
                    "key_as_string": "2018-06-01 00:00:00",
                    "doc_count": 1
                },
                {
                    "key": 1538352000000,
                    "key_as_string": "2018-10-01 00:00:00",
                    "doc_count": 1
                }
            ]
        },
        "group_by_word_count": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 2000,
                    "doc_count": 4
                },
                {
                    "key": 3000,
                    "doc_count": 2
                },
                {
                    "key": 1000,
                    "doc_count": 1
                },
                {
                    "key": 5000,
                    "doc_count": 1
                },
                {
                    "key": 10000,
                    "doc_count": 1
                },
                {
                    "key": 100000,
                    "doc_count": 1
                }
            ]
        }
    }
}
複製代碼

還能夠這樣聚合

{
	"aggs":{
	
		"grade_word_count":{
			"stats":{
				"field":"word_count"
			}
		}
	}
}
複製代碼

獲得以下的聚合結果:

"aggregations": {
        "grade_word_count": {
            "count": 10,
            "min": 1000,
            "max": 100000,
            "avg": 13000,
            "sum": 130000
        }
    }
複製代碼
相關文章
相關標籤/搜索