1. 程式人生 > 實用技巧 >ES查詢底層原理

ES查詢底層原理

一、正排索引

1、倒排索引的優勢在於查詢包含某個項的文件,如果用它確定哪些項是否存在文件中那麼就使用正排索引

2、區別正排索引和倒排索引

3、doc_value與fielddata

二、基於mget批量查詢以及基於bulk的批量增刪改

1、mget:批量查詢

GET /<index>/_mget

GET /product/_mget
{
  "docs":[
    {
      "_id":2,
      "_source":{
        "include":["name"],
        "exclude":["price"]
      }
    }
    ]
}

2、bulk:批量增刪改 no-query

POST /<index>/_bulk

{"action": {"metadata"}}

{"data"}

Operate

①create:PUT /index/_create/id/,強制建立(是否制定id)

  如果不存在則報錯,如果存在則報錯

指定id:
PUT /product2/_create/1/ { "name":"xiaomi", "desc":"shouji", "price":3999 }

  自動生成id:

  POST /product2/_doc
  {
    "name":"xiaomi",
    "desc":"shouji",

    "price":3999
  }

  建立

  POST /_bulk
  {"create":{"_index":"product2","_id":"1"}}
  {"name":"_buld create1"}
  {"create":{"_index":"product2","_id":"11"}}
  {"name":"_buld create1"}

②delete:刪除(lazy delete原理)

POST /_bulk
{"delete":{"_index":"product2","_id":"11"}}

③index:可以是建立,也可以是全量替換

POST /_bulk
{
"index":{"_index":"product2","_id":"1"}} {"doc":{"name":"bulk name1"}} {"index":{"_index":"product2","_id":"11"}} {"doc":{"name":"bulk name2"}}

④update:執行partial update(全量替換,部分替換)

POST /_bulk
{"update":{"_index":"product2","_id":"1","retry_on_conflict":"3"}}
{"doc":{"name":"bulk name"}}

三、ES併發衝突問題(悲觀鎖和樂觀鎖)

(1)悲觀鎖:各種情況,都加鎖,讀寫鎖、行級鎖、表級鎖。使用簡單,但是併發能力很低

(2)樂觀鎖:併發能力高,操作麻煩,每次no-query操作都需要比對version