Elasticsearch入門學習(二)

ES的基本概念

  1. 索引
  2. 字段類型
  3. 文檔

我們可以用關係型數據庫來做個類比:
在這裏插入圖片描述
而ES則是面向文檔的,文檔是最小的單位,文檔又有幾個主要屬性:

  • 自我包含,一篇文檔同時包含字段和對應的值,也就是同時包含 key:value!
  • 可以是層次型的,一個文檔中包含自文檔,複雜的邏輯實體就是這麼來的! {就是一個json對象!
    fastjson進行自動轉換!}
  • 靈活的結構,文檔不依賴預先定義的模式,我們知道關係型數據庫中,要提前定義字段才能使用,
    在elasticsearch中,對於字段是非常靈活的,有時候,我們可以忽略該字段,或者動態的添加一個
    新的字段。

還有一個就是分片操作:就是在創建索引的是時候會將其分成多個分片,然後在集羣的時候就會存入到不同的節點裏面,這樣就可以保證在某個節點崩潰了的時候,保證其他節點數據不會丟失。

倒排索引: 看個表格就懂
在這裏插入圖片描述

ES的基本操作

es的基本的rest的操作:
在這裏插入圖片描述
es的api的操作:
創建一個索引:

PUT /test1/type1/1
{
  "name": "wcx",
  "age": 18
}

這個返回:
在這裏插入圖片描述

這個時候也可以在head裏面查看:
在這裏插入圖片描述
當然也可以在head裏面創建索引,自己可以嘗試一下,然後其他api:

設置類型:

PUT /test2
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "long"
      },
      "birthday": {
        "type": "date"
      }
    }
  }
}

更新操作:

POST /test3/_doc/1/_update
{
  "doc": {
    "name": "zyy"
  }
}

獲取索引的詳細信息:
GET _cat/indices

在這裏插入圖片描述
查找Api:

GET /test4/user/1
GET /test4/user/_search?q=name:張三

上面的可以查詢文檔id爲1的文檔信息
下面的就是名字叫張三的

test4是索引名—user是type,相當於數據庫表–_search操作

較爲複雜的查詢:

GET /test4/user/_search
{
  "query": {
    "match": {
      "name": "趙倩"
    }
  },
  "_source": ["name", "age"],
  "sort": [
    {
      "age": {
        "order": "asc"
      }
    }
  ],
  "from": 0,
  "size": 1
}

query:表示查詢
match:表示匹配規則
name:表示需要匹配的字段
_source:表示需要返回的字段
sort:排序(都跟數據庫很像)
from:表示分頁操作裏面的當前頁(currentPage)
size:表示分頁操作裏面的當前頁記錄條數(pageSize)

可以看下結果:
在這裏插入圖片描述
接下來是bool判斷:

GET /test4/user/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "倩"
          }
        },
        {
          "match": {
            "age": "18"
          }
        }
      ]
    }
  }
}

返回結果:

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.7261541,
    "hits" : [
      {
        "_index" : "test4",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.7261541,
        "_source" : {
          "name" : "吳倩",
          "age" : 18,
          "tags" : [
            "漂亮",
            "喜感",
            "唱歌"
          ]
        }
      }
    ]
  }
}

可以看到這裏的分數也有了,這個就是需要滿足上面兩個條件才能返回!!(and操作)–》(對應的是 must)
接下來看個should(or)操作:

GET /test4/user/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "趙倩"
          }
        },
        {
          "match": {
            "age": "18"
          }
        }
      ]
    }
  }
}

返回值:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.7261541,
    "hits" : [
      {
        "_index" : "test4",
        "_type" : "user",
        "_id" : "3",
        "_score" : 1.7261541,
        "_source" : {
          "name" : "吳倩",
          "age" : 18,
          "tags" : [
            "漂亮",
            "喜感",
            "唱歌"
          ]
        }
      },
      {
        "_index" : "test4",
        "_type" : "user",
        "_id" : "4",
        "_score" : 0.60996956,
        "_source" : {
          "name" : "吳倩123",
          "age" : 2,
          "tags" : [
            "漂亮",
            "喜感",
            "跳舞"
          ]
        }
      }
    ]
  }
}

多關鍵字匹配和高亮顯示:

GET /test4/user/_search
{
  "query": {
    "match": {
      "tags": "漂 棒"
    }
  }
}
GET /test4/user/_search
{
  "query": {
    "match": {
      "name": "趙倩"
    }
  },
  "highlight": {
    "pre_tags": "<p class='key', style='color:red'>",
    "post_tags": "</p>", 
    "fields": {
      "name": {}
    }
  }
}

ES其他基本查詢:

=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates

好了,這是es的部分Api的講解,更多的可以參閱官網,但是大部分都是常有的,接下來回將集成到SpringBoot。
感謝大家閱讀!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章