Elasticsearch的增删改查

2,056 阅读4分钟

Elasticsearch的增删改查

在使用elasticsearch-head创建索引的时候就我们查看索引的时候可以看到mappings的关键字,该关键字是创建结构化索引的关键字。如果使用head来创建索引,那么该索引是非结结构化的。如果要创建结构化的索引,需要使用以下的方式

除此之外我们也可以使用postman去创建结构化的索引

在elasticsearch-head可以看到如下的信息:

此时我们就创建好了一个结构化的索引

插入数据

插入自定义id的文档

查看elasticsearch-head的数据如下:

elasticsearch分配id

elasticsearch同样可以看到我刚刚插入的数据

修改数据

直接修改文档

在elastisearch-head可以查看到

脚本修改文档

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

{
	"script":{
	    #ES内置的脚本
		"lang":"painless",
		#ctx是es的上下文,这里是将年龄+10
		"inline":"ctx._source.age+=10"
	}
}

返回结果

{
    "_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
        }
    }