Elasticsearch从小牛到老牛(3)---花式增删改

ElasticSearch 花式增删改查

Elasticsearch在6.X以后每个index只能有一个type,预计在7.X以后去掉type,之所以去掉type,官方给出的解释大概是

    Elasticsearch是基于Lucene开发的搜索引擎,而ES中不同type下名称相同的filed最终在Lucene中的处理方式是一样的。举个例子,两个不同type下的两个user_name,在ES同一个索引下其实被认为是同一个filed,你必须在两个不同的type中定义相同的filed映射。否则,不同type中的相同字段名称就会在处理中出现冲突的情况,导致Lucene处理效率下降。
    去掉type能够使数据存储在独立的index中,这样即使有相同的字段名称也不会出现冲突,就像ElasticSearch出现的第一句话一样“你知道的,为了搜索····”,去掉type就是为了提高ES处理数据的效率。
    除此之外,在同一个索引的不同type下存储字段数不一样的实体会导致存储中出现稀疏数据,影响Lucene压缩文档的能力,导致ES查询效率的降低。


新增操作

Elasticsearch的新增操作可以使用PUT 和 POST两种方式进行,但是这两种方式又有所不同:
PUT是幂等方法,而POST并不是。使用PUT方式必须指定documentId

PUT用于更新操作,POST用于新增操作比较合适。
PUT,DELETE操作是幂等的,所谓幂等就是指不管进行多少次操作,结果都一样。比如,我用PUT修改一篇文章,然后在做同样的操作,每次操作后的结果并没有不同,DELETE也是一样。
POST操作不是幂等,比如常见的POST重复加载问题:当我们多次发出同样的POST请求后,其结果是创建出了若干的资源。

POST新增

POST /user/student
{
  "name":"shenjg",
  "age":18,
  "no":201371060116,
  "address":"甘肃省陇南市",
  "mobile":18993897779,
  "tags":["baskteball","study"]
}

result1

{
  "_index": "user",  ## 索引名
  "_type": "student",  ## 类型
  "_id": "5rA0EGcB_SXuKuzTx2-o",    ## post方式提交自动生成id
  "_version": 1,   ## 记录更改次数
  "result": "created",
  "_shards": {
    "total": 2,  ## 这里的shards为2是因为Elasticsearch默认有一个shard和一个副本,因此为2
    "successful": 1,  ## 因为我们只启动了一个es进程,所以这里为1
    "failed": 0
  },
  "_seq_no": 4,
  "_primary_term": 1
}

当我们再一次提交的时候又会新增一条记录,因此我们说post请求不是幂等的
result2

{
  "_index": "user",
  "_type": "student",
  "_id": "57A3EGcB_SXuKuzTVG_K",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 5,
  "_primary_term": 1
}

再看一下使用PUT提交:

PUT /user/student/
{
  "name":"shenjg",
  "age":18,
  "no":201371060116,
  "address":"甘肃省陇南市",
  "mobile":18993897779,
  "tags":["baskteball","study"]
}

result

{
  "error": "Incorrect HTTP method for uri [/user/student/] and method [PUT], allowed: [POST]",
  "status": 405
}

可以看出PUT创建一条记录的时候,如果没有指定documentid会报错,加上id后再提交一次试试

PUT /user/student/46
{
  "name":"shenjg",
  "age":18,
  "no":201371060116,
  "address":"甘肃省陇南市",
  "mobile":18993897779,
  "tags":["baskteball","study"]
}

result3

{
  "_index": "user",
  "_type": "student",
  "_id": "46",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 17,
  "_primary_term": 1
}

修改操作

同样的修改操作也可以使用post和put进行,但是修改操作时POST必须加documentId,否则就会重新创建一条数据。修改后document的——version加一。

PUT /user/student/4
{
  "name":"old horse",
  "age":23,
  "no":201371060120,
  "address":"甘肃省兰州市皋兰县",
  "mobile":18809461493,
  "tags":["sleep","music","movie","drink"]
}

result

{
  "_index": "user",
  "_type": "student",
  "_id": "4",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 7,
  "_primary_term": 1
}

删除操作

删除时使用DELETE

DELETE /user/student/46

result

{
  "_index": "user",
  "_type": "student",
  "_id": "46",
  "_version": 4,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 20,
  "_primary_term": 1
}

查询操作

Elasticsearch提供了很多查询操作,我们将单独进行讲解,这里简单介绍一种查询方式

GET /user/student/_search

result

{
  "took": 2,     ## 花费的时间
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 7,   ## 命中的文档数
    "max_score": 1,
    "hits": [   ##命中的文档数组
      {
        "_index": "user",
        "_type": "student",
        "_id": "44",
        "_score": 1,
        "_source": {
          "name": "shenjg",
          "age": 18,
          "no": 201371060116,
          "address": "甘肃省陇南市",
          "mobile": 18993897779,
          "tags": [
            "baskteball",
            "study"
          ]
        }
      },
      {
        "_index": "user",
        "_type": "student",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "tianzh",
          "age": 17,
          "no": 201371060117,
          "address": "甘肃省陇南市",
          "mobile": 15390664567,
          "tags": [
            "women",
            "game"
          ]
        }
      },
      {
        "_index": "user",
        "_type": "student",
        "_id": "5rA0EGcB_SXuKuzTx2-o",
        "_score": 1,
        "_source": {
          "name": "shenjg",
          "age": 18,
          "no": 201371060116,
          "address": "甘肃省陇南市",
          "mobile": 18993897779,
          "tags": [
            "baskteball",
            "study"
          ]
        }
      },
      {
        "_index": "user",
        "_type": "student",
        "_id": "57A3EGcB_SXuKuzTVG_K",
        "_score": 1,
        "_source": {
          "name": "shenjg",
          "age": 18,
          "no": 201371060116
        }
      },
      {
        "_index": "user",
        "_type": "student",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "old horse",
          "age": 23,
          "no": 201371060120,
          "address": "甘肃省兰州市皋兰县",
          "mobile": 18809461493,
          "tags": [
            "sleep",
            "music",
            "movie",
            "drink"
          ]
        }
      },
      {
        "_index": "user",
        "_type": "student",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "shenjg",
          "age": 18,
          "no": 201371060116,
          "address": "甘肃省陇南市",
          "mobile": 18993897779,
          "tags": [
            "baskteball",
            "study"
          ]
        }
      },
      {
        "_index": "user",
        "_type": "student",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "tangxh",
          "age": 19,
          "no": 201371060118,
          "address": "甘肃省嘉峪关",
          "mobile": 18993889989,
          "tags": [
            "game",
            "drink"
          ]
        }
      }
    ]
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章