美文网首页工作ES
Elastic Search的简单增删改查

Elastic Search的简单增删改查

作者: emmmm大师 | 来源:发表于2018-08-12 11:14 被阅读2次

1.新建文档(类似mysql insert插入操作)

(1)指定id删除

put 127.0.0.1:9200/people/man/1 

{
    "name": "zcq",
    "country": "China",
    "age": 30,
    "date": "1987-03-07"
    
}

成功后如下

{
    "_index": "people",
    "_type": "man",
    "_id": "2",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

(2)自动产生文档id插入

post 127.0.0.1:9200/people/man/
{
    "name": "zcq",
    "country": "China",
    "age": 30,
    "date": "1987-03-07"
    
}

成功后如下,然后会自动设置一个字符串为id

{
    "_index": "people",
    "_type": "man",
    "_id": "AWUt2cwiC_7ajORvRXWG",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "created": true
}

2.更新文档(类似mysql update操作)

(1)直接修改文档

post 127.0.0.1:9200/people/man/1/_update
//关键词doc
{
"doc":{
"name":"谁是瓦力"
}
}

成功返回

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

(2)通过脚本修改文档

post 127.0.0.1:9200/people/man/1/_update

{
"script":{
"lang":"painless", //es内置的语言
"inline":"ctx._source.age = params.age" //es单前的文档
"params":{
"age" : 100
}
}
}

成功返回

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 5,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

3.删除文档(类似mysql delete操作)

(1)删除文档

DELETE 127.0.0.1:9200/people/man/1/

成功返回

{
    "found": true,
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 6,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    }
}

4.检索文档(类似mysql search 搜索select*操作)

(1)简单查询

GET 127.0.0.1:9200/people/man/2/

成功返回

{
    "_index": "people",
    "_type": "man",
    "_id": "2",
    "_version": 1,
    "found": true,
    "_source": {
        "name": "zcq",
        "country": "China",
        "age": 30,
        "date": "1987-03-07"
    }
}

(2)条件查询

POST 127.0.0.1:9200/people/_search

//查询所有数据,以query开头
{
    "query":{
        "match_all":{
            
        }
              //条件查询 名字为zcq的
              “match”{
          "name":"zcq"              
}
//从哪里返回
      "from":1,
//返回几条
      "size":1,
      //排序,
    "sort":[ {"id":{"order":"desc"}]
    }
}

成功返回

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": 1,
        "hits": [
            {
                "_index": "people",
                "_type": "man",
                "_id": "AWUqNFDCRY0ZTXlf31hf",
                "_score": 1,
                "_source": {
                    "name": "zzzzcq",
                    "country": "China",
                    "age": 50,
                    "date": "1977-03-07"
                }
            },
            {
                "_index": "people",
                "_type": "man",
                "_id": "2",
                "_score": 1,
                "_source": {
                    "name": "zcq",
                    "country": "China",
                    "age": 30,
                    "date": "1987-03-07"
                }
            },
            {
                "_index": "people",
                "_type": "man",
                "_id": "AWUt2cwiC_7ajORvRXWG",
                "_score": 1,
                "_source": {
                    "name": "zcq",
                    "country": "China",
                    "age": 30,
                    "date": "1987-03-07"
                }
            }
        ]
    }
}

(3)聚合查询

POST 127.0.0.1:9200/people/_search

//聚合查询关键词aggs
{
"aggs":{
"group_by_word_count":{
"terms":{
//聚合信息
"field":"word_count"
}
},
"group_by_publish_date":{
"terms": {
//聚合信息
"field":"publish_date"
}
}
}
}

相关文章

网友评论

    本文标题:Elastic Search的简单增删改查

    本文链接:https://www.haomeiwen.com/subject/qkhsbftx.html