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"
          ]
        }
      }
    ]
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章