首頁 > 軟體

Elasticsearch檔案索引基本操作增刪改查範例

2022-04-19 16:00:07

介面冪等性

  • 0 介面冪等性:數學概念,多次請求,相當於一次請求
  • get,put,delete都是冪等性的介面
  • post 存在冪等性的問題
  • 前端速度很快,點了兩次,會生成兩個訂單
  • 使用者在存取新增頁面時(提交訂單)---》介面返回一個唯一id,提交訂單,攜帶唯一id過來,後端判斷這個唯一id是否被用過---》沒用過,建立訂單
  • 你在專案中碰到的問題和如何解決(專案收穫)下訂單,經常重複訂單,點得快,冪等性問題,如何解決的

倒排索引

1.es介紹10個點

2.安裝

-jdk :java開發環境

  • 官網下載es相應的版本,解壓,到bin目錄下啟動elasticsearch.bat
  • 兩個使用者端(kibana:官方,傳送請求,資料統計,展示,elasticsearch-head:第三方用node寫的)
  • kibana跟es版本對應,bin路徑下,直接啟動
  • elasticsearch-head需要安裝node環境,npm install ---》npm run start
  • kibana組態檔設定(copy一下,kibana監聽的地址埠,隨便名一個名,連線es的地址)
  • 修改es設定:允許跨域(瀏覽器的同源策略),cors:跨域資源共用,實現跨域
  • es的倒排索引(擴充套件閱讀.md)
  • 把文章進行分詞,對每個詞建立索引

3 索引操作

# 索引操作---》資料庫操作---》新增,刪除,修改,查詢
# 1 新增索引:
PUT lqz2     # lqz2就是索引名字
{
  "settings": {
    "index":{
      "number_of_shards":5,
      "number_of_replicas":1
    }
  }
}
# 2 查詢索引
GET lqz2/_settings
返回結果
{
  "lqz2" : {
    "settings" : {
      "index" : {
        "creation_date" : "1588822389842",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "NBXIeVdHQ26vCuPn8_6uew",
        "version" : {
          "created" : "7050099"
        },
        "provided_name" : "lqz2"
      }
    }
  }
}
# 3 更新索引
PUT lqz2/_settings
{
  "number_of_replicas": 2
}
#4 刪除lqz2索引
DELETE lqz2

4 檔案基本增刪查改

#1  新增檔案
POST lqz2/_doc/1
{
  "title":"紅樓夢",
  "price":12,
  "publish_addr":{
    "province":"黑龍江",
    "city":"鶴崗"
  },
  "publish_date":"2013-11-11",
  "read_num":199,
  "tag":["古典","名著"]
}
POST lqz2/_doc/2
{
  "title":"西遊記",
  "price":22,
  "publish_addr":{
    "province":"上海",
    "city":"上海"
  },
  "publish_date":"2013-11-11",
  "read_num":66,
  "tag":["古典","小說"]
}
# 2 查詢檔案(更詳細的查詢,後面講)
GET lqz2/_doc/2
# 3 修改檔案
-覆蓋式(原來的欄位就沒有了)
PUT lqz/_doc/1
{
  "title":"xxxx",
  "price":333,
  "publish_addr":{
    "province":"黑龍江",
    "city":"福州"
  }
}
-增量式(只修改某個欄位)一定要注意包在doc中
POST lqz2/_doc/1/_update
{
  "doc":{
    "title":"xx"
  }
}
# 4 刪除檔案
DELETE lqz/_doc/1
# 5 批次獲取
GET _mget
{
  "docs":[
    {
      "_index":"lqz2",
      "_type":"_doc",
      "_id":2
    },
    {
      "_index":"lqz2",
      "_type":"_doc",
      "_id":1
    }
    ]
}
# 6 批次插入
PUT test/_doc/2/_create
{
  "field1" : "value22"
}
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

5 檔案查詢

# 測試資料
PUT lqz/doc/1
{
  "name":"顧老二",
  "age":30,
  "from": "gu",
  "desc": "面板黑、武器長、性格直",
  "tags": ["黑", "長", "直"]
}
PUT lqz/doc/2
{
  "name":"大娘子",
  "age":18,
  "from":"sheng",
  "desc":"膚白貌美,嬌憨可愛",
  "tags":["白", "富","美"]
}
PUT lqz/doc/3
{
  "name":"龍套偏房",
  "age":22,
  "from":"gu",
  "desc":"mmp,沒怎麼看,不知道怎麼形容",
  "tags":["造資料", "真","難"]
}
# 兩種方式
	-第一種(字串查詢)
  	GET lqz/doc/_search?q=from:gu
    GET lqz/doc/_search?q=age:22
  -查詢的第二種方式(結構化)
  	GET lqz/doc/_search
    {
      "query": {
        "match": {
          "from": "gu"
        }
      }
    }
    GET lqz/doc/_search
    {
      "query": {
        "match": {
          "age": 22
        }
      }
    }
   GET lqz/doc/_search
    {
      "query": {
        "match": {
          "desc": "形容"
        }
      }
    }
GET lqz/doc/_search
{
  "query": {
    "match": {
      "tags": "造資料"
    }
  }
}

以上就是Elasticsearch檔案索引基本操作增刪改查範例的詳細內容,更多關於Elasticsearch檔案索引增刪改查的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com