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
        }
    }
复制代码
相关文章
相关标签/搜索