Elasticsearch之索引簡單應用

2023-09-06 18:02:21

本篇所有操作都在 Kibana 上執行

建立第一個索引

PUT product
{
  // 索引設定
  "settings": {
    // 分片數量
    "number_of_shards": 3,
    // 副本數量
    "number_of_replicas": 1
  },
  // 索引欄位對映
  "mappings": {
    // 欄位屬性
    "properties": {
        // 商品名稱
        "name":{
          // 欄位型別為文字
          "type":"text"
        },
        // 商品標籤
        "label":{
          "type":"keyword"
        },
        "price":{
           "type": "scaled_float",
           // 比例因子設定為100 在ES中會按分儲存
           // 注意:scaling_factor屬性是隻針對scaled_float這個資料型別才有
            "scaling_factor": 100
        },
        // 商品狀態
        "status":{
          "type":"integer"
        },
        // 建立日期
        "create_date":{
          "type":"date"
        }
    }
  }
}

執行命令,我們會得到如下返回資訊,表示建立成功

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "product"
}

檢視指定索引結構

獲取索引全部資訊:

## 命令格式  GET {索引名稱}
GET product

返回結果:

{
  "product": {
    "aliases": {},
    "mappings": {
      "properties": {
        "create_date": {
          "type": "date"
        },
        "label": {
          "type": "keyword"
        },
        "name": {
          "type": "text"
        },
        "price": {
          "type": "scaled_float",
          "scaling_factor": 100
        },
        "status": {
          "type": "integer"
        }
      }
    },
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "3",
        "provided_name": "product",
        "creation_date": "1693981437123",
        "number_of_replicas": "1",
        "uuid": "awfrhothQaeoL2bHvufN5w",
        "version": {
          "created": "8090199"
        }
      }
    }
  }
}

檢視索引 Mapping 資訊

GET product/_mapping  

返回結果:

{
  "product": {
    "mappings": {
      "properties": {
        "create_date": {
          "type": "date"
        },
        "label": {
          "type": "keyword"
        },
        "name": {
          "type": "text"
        },
        "price": {
          "type": "scaled_float",
          "scaling_factor": 100
        },
        "status": {
          "type": "integer"
        }
      }
    }
  }
}

檢視索引 settings 資訊

GET product/_settings   

返回結果:

{
  "product": {
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "3",
        "provided_name": "product",
        "creation_date": "1693981437123",
        "number_of_replicas": "1",
        "uuid": "awfrhothQaeoL2bHvufN5w",
        "version": {
          "created": "8090199"
        }
      }
    }
}

檢視索引 aliases 資訊

GET product/_alias

返回結果:

{
  "product": {
    "aliases": {}
  }
}

目前我們沒設定別名,所以返回為空

新增索引資料--檔案

單個新增

語法:

PUT {索引名}/_doc/{檔案id}
{
  {欄位名}:{欄位值/]
}

範例:

PUT product/_doc/1
{
  "name":"籃球",
  "label":["運動","球類"],
  "price":33.5,
  "status":0,
  "create_date":"2023-08-08T13:00:00"
}

執行成功將返回:

{
  "_index": "product",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

批次新增

語法:

POST _bulk
{ action: { metadata }}
{ request body        }
{ action: { metadata }}
{ request body        }

注:bulk對 JSON 串的有著嚴格的要求。每個 JSON 串不能換行,只能放在同一行,同時,相鄰的 JSON 串之間必須要有換行(delete語法除外).

範例:

POST _bulk
{"create":{"_index":"product","_id":2}}
{"name":"足球","label":["運動","球類"],"price":60.3,"status":0,"create_date":"2023-08-09T13:00:00"}
{"create":{"_index":"product","_id":3}}
{"name":"華為手機","label":["數碼","手機"],"price":6999,"status":0,"create_date":"2023-08-31T13:00:00"}
{"create":{"_index":"product","_id":4}}
{"name":"蘋果手機","label":["數碼","手機"],"price":9999,"status":1,"create_date":"2023-08-31T13:00:00"}

返回結果:

{
  "took": 6,
  "errors": false,
  "items": [
    {
      "create": {
        "_index": "product",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 0,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "create": {
        "_index": "product",
        "_id": "3",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 1,
        "_primary_term": 1,
        "status": 201
      }
    },
    {
      "create": {
        "_index": "product",
        "_id": "4",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "_seq_no": 2,
        "_primary_term": 1,
        "status": 201
      }
    }
  ]
}

查詢索引資料--檔案

查詢所有

GET product/_search
{
    "query": {
        "match_all": {}
    }
}

返回

{
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [
      {
        "_index": "product",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "足球",
          "label": [
            "運動",
            "球類"
          ],
          "price": 60.3,
          "status": 0,
          "create_date": "2023-08-09T13:00:00"
        }
      },
      {
        "_index": "product",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "華為手機",
          "label": [
            "數碼",
            "手機"
          ],
          "price": 6999,
          "status": 0,
          "create_date": "2023-08-31T13:00:00"
        }
      },
      {
        "_index": "product",
        "_id": "4",
        "_score": 1,
        "_source": {
          "name": "蘋果手機",
          "label": [
            "數碼",
            "手機"
          ],
          "price": 9999,
          "status": 1,
          "create_date": "2023-09-01T13:00:00"
        }
      },
      {
        "_index": "product",
        "_id": "1",
        "_score": 1,
        "_source": {
          "name": "籃球",
          "label": [
            "運動",
            "球類"
          ],
          "price": 33.5,
          "status": 0,
          "create_date": "2023-08-08T13:00:00"
        }
      }
    ]
  }
}

響應的資料結果分為兩部分:

{
  ----------------first part:分片副本資訊--------------------
  "took": 9,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  ---------------second part:查詢的資料集---------------------
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": 1,
    "hits": [{}]
  }
}

查詢標籤帶手機的商品

GET product/_search
{
    "query": {
        "match":  {
          "label": "手機"
        }        
    }
}

返回:

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 0.5908618,
    "hits": [
      {
        "_index": "product",
        "_id": "3",
        "_score": 0.5908618,
        "_source": {
          "name": "華為手機",
          "label": [
            "數碼",
            "手機"
          ],
          "price": 6999,
          "status": 0,
          "create_date": "2023-08-31T13:00:00"
        }
      },
      {
        "_index": "product",
        "_id": "4",
        "_score": 0.5908618,
        "_source": {
          "name": "蘋果手機",
          "label": [
            "數碼",
            "手機"
          ],
          "price": 9999,
          "status": 1,
          "create_date": "2023-09-01T13:00:00"
        }
      }
    ]
  }
}

過濾狀態為0的

GET product/_search
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "label": "手機"
        }
      },
      "filter": {
        "term": {
          "status": 0
        }
      }
    }
  }
}

返回結果:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 0.5908618,
    "hits": [
      {
        "_index": "product",
        "_id": "3",
        "_score": 0.5908618,
        "_source": {
          "name": "華為手機",
          "label": [
            "數碼",
            "手機"
          ],
          "price": 6999,
          "status": 0,
          "create_date": "2023-08-31T13:00:00"
        }
      }
    ]
  }
}

查詢結果裡面都有一個 _score欄位,一般 Elasticsearch 根據相關評分排序,相關評分是根據檔案與語句的匹配度來得出, _score值越高說明匹配度越高。相關性(relevance)概念在Elasticsearch中非常重要,而這也是它與傳統關係型資料庫中記錄只有匹配和不匹配概念最大的不同。