基礎概念
索引(
Index
)
一個索引就是含有某些相似特性的文檔的集合。例如,你可以有一個用戶數據的索引,一個產品目錄的索引,還有其他的有規則數據的索引。一個索引被一個名稱(必須都是小寫)唯一標識,並且這個名稱被用於索引通過文檔去執行索引,搜索,更新和刪除操作。
在一個集羣中,你可以根據自己的需求定義任意多的索引。
索引對應數據庫中的庫
類型(
Type
)
一個類型是你的索引中的一個分類或者說是一個分區,它可以讓你在同一索引中存儲不同類型的文檔,例如,爲用戶建一個類型,爲博客文章建另一個類型。現在已不可能在同一個索引中創建多個類型,並且整個類型的概念將會在未來的版本中移除。查看“映射類型的移除”瞭解更多。
類型對應數據庫中的表
在6.0.0版本中已經不贊成使用
文檔(
Document
)
一個文檔是一個可被索引的數據的基礎單元。例如,你可以給一個單獨的用戶創建一個文檔,給單個產品創建一個文檔,以及其他的單獨的規則。這個文檔用JSON格式表現,JSON是一種普遍的網絡數據交換格式。
在一個索引或類型中,你可以根據自己的需求存儲任意多的文檔。注意,雖然一個文檔在物理存儲上屬於一個索引,但是文檔實際上必須指定一個在索引中的類型。
文檔對應數據庫中的數據
關係型數據庫 | Elasticsearch |
---|---|
數據庫 Database |
索引 Index ,支持全文檢索 |
表 Table |
類型 Type |
數據行 Row |
文檔 Document ,但不需要固定結構,不同文檔可以具有不同字段集合 |
數據列 Column |
字段 Field |
模式 Schema |
映像 Mapping |
檢索分類思維導圖
圖片來源: https://blog.csdn.net/laoyang360/article/details/77623013
Mapping 小實踐
創建索引 test
和定義 mapping
mapping
創建後不能修改
ES5.X版本以後, keyword
支持的最大長度爲 32766
個UTF-8字符, text
對字符長度沒有限制
設置 ignore_above
後, 超過給定長度後的數據將不被索引, 無法通過 term
精確匹配檢索返回結果
Mapping詳解: https://blog.csdn.net/napoay/article/details/73100110
# PUT /test
{
"settings":{
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"my_type": {
"properties": {
"id": {
"type": "integer"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"message": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"author": {
"type": "keyword",
"ignore_above": 256
},
"ip": {
"type": "ip"
},
"url": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 2048
}
}
},
"time": {
"type": "integer"
},
"rank": {
"type": "short"
},
"status": {
"type": "byte"
}
}
}
}
}
索引字段類型 Numeric datatypes
類型 | 範圍 |
---|---|
long | -2^63 ~ 2^63-1 |
integer | -2^31 ~ 2^31-1 |
short | -32768 ~ 32767 |
byte | -128 ~ 127 |
double | 雙精度 64-bit IEEE 754 浮點數 |
float | 單精度 32-bit IEEE 754 浮點數 |
byte => tinyint
short => smallint
int => integer
keyword不分詞, 需要分詞用text
索引遷移 (備份)
# 本地遷移 (備份)
POST /_reindex
{
"source": {
"index": "my_index"
},
"dest": {
"index": "my_index_1"
}
}
# 遠程遷移 (備份)
POST /_reindex
{
"source": {
"remote": {
"host": "http://10.10.10.102:9200",
"socket_timeout": "30s",
"connect_timeout": "30s"
},
"index": "my_index",
"size": 1000,
"query": {}
},
"dest": {
"index": "my_index"
}
}
參數解釋
source:{
host:源es的ip與端口
socket_timeout:讀取超時時間
connect_timeout:連接超時時間
index:源索引名字
size:批量抓取的size大小
(從遠程服務器重新編譯使用默認最大大小爲100MB的堆緩衝區, 如果遠程索引包含非常大的文檔, 則需要使用較小的批量)
query:查詢指定條件下的字段
}
檢索匹配
深入搜索 (官方): https://www.elastic.co/guide/cn/elasticsearch/guide/current/search-in-depth.html
檢索分類深入詳解: https://blog.csdn.net/laoyang360/article/details/77623013
must – 所有的語句都 必須(must)匹配,與 AND 等價
must_not – 所有的語句都 不能(must not)匹配, 與 NOT 等價
should – 至少有一個語句要匹配, 與 OR 等價
filter – 必須匹配, 運行在非評分&過濾模式
創建 PUT
修改 POST
刪除 DELETE
獲取 GET
{
"bool": {
"must": [],
"should": [],
"must_not": [],
"filter": []
}
}
例子:
GET /my_index/my_type/_search
{
"query": {
"bool": {
"must": {
"match": {
"text": "哈哈 測試"
}
}
}
},
"from": 0,
"size": 10,
"_source": ["title", "publish_date", "publisher"],
"sort": [{
"publish_date": {
"order": "desc"
}
},
{
"title": {
"order": "desc"
}
}
]
}
# filter 過濾查詢不會計算相關度(直接跳過了整個評分階段), 而且很容易被緩存, 所以速度快一些
GET /my_index/my_type/_search
{
"query": {
"bool": {
"must" : {
"multi_match": {
"query": "elasticsearch",
"fields": ["title","summary"]
}
},
"filter": {
"term": {
"title": "123AAA"
}
}
}
}
}
# 使用 constant_score 查詢以非評分模式來執行 term 查詢並以一作爲統一評分
GET /my_index/my_type/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"price": 20
}
}
}
}
}
1. 全文檢索
# 全文檢索 匹配多個字段 (title 和 test 字段)
GET /my_index/my_type/_search
{
"query": {
"multi_match": {
"query": "Quick brown fox",
"fields": [ "title", "text" ]
}
}
}
# 全文檢索 匹配分詞 (匹配123或AAA)
GET /my_index/my_type/_search
{
"query": {
"match": {
"title": "123AAA",
}
}
}
# 全文檢索 匹配短語 (匹配123 AAA 或 123 BBB AAA))
GET /my_index/my_type/_search
{
"query": {
"match_phrase": {
"title": "123 AAA",
}
}
}
{
"query": {
"multi_match" : {
"query": "search engine",
"fields": ["title", "summary"],
"type": "phrase",
"slop": 3 #偏離值(slop value), 該值指示在仍然考慮文檔匹配的情況下詞與詞之間的偏離值
}
}
}
# 全文檢索 語法查詢 (匹配 (123和AAA)或BBB )
GET /my_index/my_type/_search
{
"query": {
"query_string": {
"query": "(123 AND AAA) OR BBB",
}
}
}
# 全文檢索 全文搜索
GET /my_index/my_type/_search
{
"query": {
"multi_match" : {
"query" : "guide",
"fields" : ["_all"]
}
}
}
2. 結構化檢索
# 結構化檢索 精確查詢 (匹配title等於123AAA)
GET /my_index/my_type/_search
{
"query": {
"term": {
"title": "123AAA"
}
}
}
{
"query": {
"range": {
"age": {
"get": 10,
"lte": 20
}
}
}
}
# 結構化檢索 通配符查詢
*, 它匹配任何字符序列(包括空字符序列)
?, 它匹配任何單個字符
GET /my_index/my_type/_search
{
"query": {
"wildcard": {
"user": "ki*y"
}
}
}
# 結構化檢索 類型查詢 (type爲text的全部信息)
GET /my_index/my_type/_search
{
"query": {
"type": {
"value": "text"
}
}
}
# 結構化檢索 Ids查詢 (返回指定id的全部信息)
GET /my_index/my_type/_search
{
"query": {
"ids": {
"type": "xext",
"values": ["2", "4", "100"]
}
}
}
# 結構化檢索 正則搜索
GET /my_index/my_type/_search
{
"query": {
"regexp" : {
"authors" : "t[a-z]*y"
}
}
}