elasticsearch 7版本 基礎操作
首先我們瀏覽器http://localhost:5601/進入 kibana里的Console中輸入
首先讓我們在 Console 中輸入:
PUT t1/type1/1
{
"name":"春生",
"age":16
}
返回結果 (是以REST ful 風格返回的 ):
{
"_index" : "t1",
"_type" : "type1",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
我們來簡要的說一下命令欄和返回結果欄都是表示什么意思:
命令欄 | 描述 | 結果欄 | 描述 |
---|---|---|---|
PUT | 創建命令 | _index | 索引 |
t1 | 索引 | _type | 類型 |
type1 | 類型 | _id | id |
1 | id | _version | 版本 |
name | 屬性 | result | 操作類型 |
age | 屬性 | _shards | 分片信息 |
method | url地址 | 描述 |
---|---|---|
PUT | localhost:9200/索引名稱/類型名稱/文檔id | 創建文檔(指定文檔id) |
POST | localhost:9200/索引名稱/類型名稱 | 創建文檔(隨機文檔id) |
POST | localhost:9200/索引名稱/類型名稱/文檔id/_update | 修改文檔 |
DELETE | localhost:9200/索引名稱/類型名稱/文檔id | 刪除文檔 |
GET | localhost:9200/索引名稱/類型名稱/文檔id | 查詢文檔通過文檔id |
POST | localhost:9200/索引名稱/類型名稱/_search | 查詢所有數據 |
字段類型指定:
PUT t1/type1/1
{
"name":"春生",
"age":16
}
那么 name這個字段 用不用指定類型呢。畢竟我們關系型數據庫 是需要指定類型的啊
- 字符串類型
text 、 keyword - 數值類型
long, integer, short, byte, double, float, half_float, scaled_float - 日期類型
date - te布爾值類型
boolean - 二進制類型
binary - 范圍類型
integer_range , float_range, long_range, double_range, date_range
指定字段類型:
PUT db
{
"mappings": {
"properties": {
"name":{
"type":"text"
},
"age":{
"type": "long"
},
"birthday":{
"type":"date"
}
}
}
}
查看一下索引字段:
GET db/
返回結果:
{
"db" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"birthday" : {
"type" : "date"
},
"name" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1572247546224",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "g-QMzIELQL2bOU1d4pDaKA",
"version" : {
"created" : "7030299"
},
"provided_name" : "db"
}
}
}
}
我們看上列中 字段類型是我自己定義的 那么 我們不定義類型 會是什么情況呢。
默認字段類型
PUT test3/_doc/1
{
"name":"春生",
"age":13,
"ddd":"1997-3-3"
}
查看一下test3索引:
GET test3
返回結果:
{
"test3" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"ddd" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1572248744742",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "ebwFzh6-R32SOhZSj1S0pQ",
"version" : {
"created" : "7030299"
},
"provided_name" : "test3"
}
}
}
}
我們看上列沒有給字段指定類型 那么es就會默認給我配置 如字段類型 分片 以及id 就會默認給我們配置 。
對比關系型數據庫 :
PUT t1/type1/1 : 索引t1 相當於關系型數據庫的 庫,類型type1就相當於表 ,1 代表數據中的主鍵id
補充(注意):
這里需要補充的是 ,在elastisearch5版本前,一個索引下可以創建多個類型,但是在elastisearch5后,一個索引只能對應一個類型,而id相當於關系型數據庫的主鍵id若果不指定就會默認生成一個20位的uuid,屬性相當關系型數據庫的column(列)。
而結果中的 result 則是操作類型,現在是 created ,表示第一次創建。如果我們再次點擊執行該命令,那么result 則會是 updated 。我們細心則會發現 _version 開始是1,現在你每點擊一次就會增加一次。表示第幾次更改。
我們在來學一條命令(elasticsearch 中的索引的情況):
GET _cat/indices?v
返回結果:

查看我們所有索引的狀態健康情況 分片,數據儲存大小等等。
那么怎么刪除一條索引呢(庫)呢?
DELETE /t1
返回結果:
{
"acknowledged" : true
}
# 表示刪除成功了
基礎增刪改查
創建數據PUT
第一條數據:
PUT test/chunsheng/1
{
"name":"春生",
"age":18,
"from":"gu",
"desc":"皮膚黑,武器長,性格直",
"tags":["黑","長","直"]
}
# 注意 逗號 一定是英文的 ,
第二條數據:
PUT test/chunsheng/2
{
"name":"大娘子",
"age":18,
"from":"sheng",
"desc":"膚白貌美,就是腿長",
"tags":["白","富","美"]
}
第三條數據:
PUT test/chunsheng/3
{
"name":"龍套偏房",
"age":22,
"from":"gu",
"desc":"mmp,沒看怎么看,不知道怎么形容",
"tags":["造數據", "真","難"]
}
注意⚠️:當執行 命令時,如果數據不存在,則新增該條數據,如果數據存在則修改該條數據。
咱們通過 GET 命令查詢一下:
GET test/chunsheng/1
返回結果:
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黑,武器長,性格直",
"tags" : [
"黑",
"長",
"直"
]
}
}
如果你想更新數據 可以覆蓋這條數據:
PUT test/chunsheng/1
{
"name":"春生",
"age":18,
"from":"gu",
"desc":"皮膚黃,武器長,性格直",
"tags":["黃","長","直"]
}
返回結果:
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 3,
"_primary_term" : 1
}
查看結果:
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_version" : 2,
"_seq_no" : 3,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黃,武器長,性格直",
"tags" : [
"黃",
"長",
"直"
]
}
}
已經修改了 那么 PUT 可以更新數據但是。麻煩的是 原數據你還要重寫一遍要 這不符合我們規矩。
更新數據POST:
POST test/chunsheng/1/_update # 加了個_update
{
"doc": {
"desc":"皮膚黃,富二代,就有錢",
"tags":["黃","富","錢"]
}
}
返回結果:
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_version" : 3,
"_seq_no" : 4,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黃,富二代,就有錢",
"tags" : [
"黃",
"富",
"錢"
]
}
}
上例中,我們使用 POST 命令,在 id 后面跟 _update ,要修改的內容放到 doc 文檔(屬性)中即可。
查詢GET
簡單的查詢,我們上面已經不知不覺的使用熟悉了:
GEt test/chunsheng/1
條件查詢 _search?q=
GET test/chunsheng/_search?q=from:gu
通過 _serarch?q = from:gu 查詢條件是from屬性是gu有那些數據。
別忘 了 _search 和 from 屬性中間的分隔符 ? 。
返回結果:
{
"took" : 19,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2, # 一共兩條數據
"relation" : "eq"
},
"max_score" : 0.35667494,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : 0.35667494,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒看怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 0.35667494,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黃,富二代,就有錢",
"tags" : [
"黃",
"富",
"錢"
]
}
}
]
}
}
我們看一下結果 返回並不是 數據本身,是給我們了一個 hits ,還有 _score得分,就是根據算法算出和查詢條件匹配度高得分就搞。后面我們單獨機會講這個算法。
構建查詢
GET test/chunsheng/_search
{
"query": {
"match": {
"from": "gu"
}
}
}
上例,查詢條件是一步步構建出來的,將查詢條件添加到 match 中即可。
返回結果還是一樣的:
{
"took" : 19,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2, # 一共兩條數據
"relation" : "eq"
},
"max_score" : 0.35667494,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : 0.35667494,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒看怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 0.35667494,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黃,富二代,就有錢",
"tags" : [
"黃",
"富",
"錢"
]
}
}
]
}
}
除此之外,我們還可以查詢跟顧老二相關的所有數據,那就是查詢全部:
GET test/chunsheng/_search #這是一個 但是沒有條件
GET test/chunsheng/_search #查詢所有的數據
{
"query": {
"match_all": {}
}
}
match_all的值為空,表示沒有查詢條件,就像select * from table_name一樣。
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "膚白貌美,就是腿長",
"tags" : [
"白",
"富",
"美"
]
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒看怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黃,富二代,就有錢",
"tags" : [
"黃",
"富",
"錢"
]
}
}
]
}
}
如果有個需求,我們僅是需要查看 name 和 desc 兩個屬性,其他的不要怎么辦?
GET test/chunsheng/_search
{
"query": {
"match_all": {}
},
"_source": ["name","age"]
}
如上例所示,在查詢中,通過 _source 來控制僅返回 name 和 age 屬性。
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "大娘子",
"age" : 18
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "龍套偏房",
"age" : 22
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "春生",
"age" : 18
}
}
]
}
}
Python推薦
一般的,我們推薦使用構建查詢,以后在與Python交互時的查詢等也是使用構建查詢方式處理查詢條件,因為該方 式可以構建更加復雜的查詢條件,也更加一目了然
排序查詢
我們說到排序 有人就會想到:正序 或 倒序 那么我們先來倒序:
倒序
GET test/chunsheng/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
上例,在條件查詢的基礎上,我們又通過 sort 來做排序,排序對象是 age , order 是 desc 降序。
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 21,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒看怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
},
"sort" : [
22
]
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "膚白貌美,就是腿長",
"tags" : [
"白",
"富",
"美"
]
},
"sort" : [
18
]
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黃,富二代,就有錢",
"tags" : [
"黃",
"富",
"錢"
]
},
"sort" : [
18
]
}
]
}
}
正序
就是 desc 換成了 asc
GET test/chunsheng/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "膚白貌美,就是腿長",
"tags" : [
"白",
"富",
"美"
]
},
"sort" : [
18
]
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : null,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黃,富二代,就有錢",
"tags" : [
"黃",
"富",
"錢"
]
},
"sort" : [
18
]
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : null,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒看怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
},
"sort" : [
22
]
}
]
}
}
注意:在排序的過程中,只能使用可排序的屬性進行排序。那么可以排序的屬性有哪些呢?
- 數字
- 日期
- ID
其他都不行
分頁查詢
GET test/chunsheng/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "asc"
}
}
],
"from": 0, # 從第n條開始
"size": 1 # 返回n條數據
}
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "2",
"_score" : null,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "膚白貌美,就是腿長",
"tags" : [
"白",
"富",
"美"
]
},
"sort" : [
18
]
}
]
}
}
就返回了一條數據 是從第0條開始的返回一條數據 。
學到這里,我們也可以看到,我們的查詢條件越來越多,開始僅是簡單查詢,慢慢增加條件查詢,增加排序,對返回 結果進行限制。所以,我們可以說:對elasticsearch於 來說,所有的查詢條件都是可插拔的,彼此之間用 分 割。比如說,我們在查詢中,僅對返回結果進行限制:
GET test/chunsheng/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 2
}
返回結果:
#! 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" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "膚白貌美,就是腿長",
"tags" : [
"白",
"富",
"美"
]
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒看怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
}
}
]
}
}
布爾查詢
must (and)
我要查詢所有from屬性為“gu“的數據:must
GET test/chunsheng/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"from": "gu"
}
},
{
"match": {
"age": 18
}
}
]
}
}
}
我們通過在 bool 屬性內使用 must 來作為查詢條件,那么條件是什么呢 age :18 ,from:gu 結果就有一條數據。
是不是 有點像 and 的感覺
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3566749,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 1.3566749,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黃,富二代,就有錢",
"tags" : [
"黃",
"富",
"錢"
]
}
}
]
}
}
should (or)
那么我要查詢from為gu,和 age 為18的 的條件
GET test/chunsheng/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"from": "gu"
}
},
{
"match": {
"age": 18
}
}
]
}
}
}
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.3566749,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 1.3566749,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黃,富二代,就有錢",
"tags" : [
"黃",
"富",
"錢"
]
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "膚白貌美,就是腿長",
"tags" : [
"白",
"富",
"美"
]
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : 0.35667494,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒看怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
}
}
]
}
}
我們的返回結果 是不是 出現了一個 age :22的 說明 我們 查出了。符合age 是18 和 from 是gu的 都行了
是不是有點像 **or ** 呢
must_not (not)
我想要查詢 年齡不是 18 的 數據
GET test/chunsheng/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"age": 18
}
}
]
}
}
}
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 8,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.0,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : 0.0,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒看怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
}
}
]
}
}
Fitter
我要查詢 from為gu的,age大於18的數據
GET test/chunsheng/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"from": "gu"
}
}
],
"filter": {
"range": {
"age": {
"gt": 18
}
}
}
}
}
}
這里就用到了 filter 條件過濾查詢,過濾條件的范圍用 range 表示, gt 表示大於,大於多少呢?是18。 結果如下:
-
gt 表示大於
-
gte 表示大於等於
-
lt 表示小於
-
lte 表示小於等於
返回結果:
#! 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" : 0.47000363,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "3",
"_score" : 0.47000363,
"_source" : {
"name" : "龍套偏房",
"age" : 22,
"from" : "gu",
"desc" : "mmp,沒看怎么看,不知道怎么形容",
"tags" : [
"造數據",
"真",
"難"
]
}
}
]
}
}
要查詢 from 是 gu , age 在 25~30 之間的怎么查?
GET test/chunsheng/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"from": "gu"
}
}
],
"filter": {
"range": {
"age": {
"gte": 25,
"lte": 30
}
}
}
}
}
}
*如果在filter過濾條件中建議用must代替
短語檢索
我要查詢 tags為黑的數據
GET test/chunsheng/_search
{
"query":{
"match": {
"tags": "黑"
}
}
}
返回結果:
#! 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.0596458,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 1.0596458,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黑,武器長,性格直",
"tags" : [
"黑",
"長",
"直"
]
}
}
]
}
}
既然按照標簽檢索,那么,能不能寫多個標簽呢?又該怎么寫呢?
GET test/chunsheng/_search
{
"query":{
"match": {
"tags": "黑 白"
}
}
}
多個標簽要空格分開
返回結果:
#! 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" : 2,
"relation" : "eq"
},
"max_score" : 1.0596458,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 1.0596458,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黑,武器長,性格直",
"tags" : [
"黑",
"長",
"直"
]
}
},
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "2",
"_score" : 1.0596458,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "膚白貌美,就是腿長",
"tags" : [
"白",
"富",
"美"
]
}
}
]
}
}
上列中我發現 只要含有這個標簽就給我返回這個數據了。
那現在 我要查詢。如 “腿長” 有就直接返回 沒有不要了 不需要包含
match_phrase
GET test/chunsheng/_search
{
"query":{
"match_phrase": {
"desc": "腿 長"
}
}
}
返回結果:
#! 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.580115,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "2",
"_score" : 1.580115,
"_source" : {
"name" : "大娘子",
"age" : 18,
"from" : "sheng",
"desc" : "膚白貌美,就是腿長",
"tags" : [
"白",
"富",
"美"
]
}
}
]
}
}
term查詢精確查詢
term
查詢是直接通過倒排索引指定的 詞條,也就是精確查找。
term和match的區別:
- match是經過分析(analyer)的,也就是說,文檔是先被分析器處理了,根據不同的分析器,分析出的結果也會不同,在會根據分詞 結果進行匹配。
- term是不經過分詞的,直接去倒排索引查找精確的值。
term與match的區別
注意 ⚠️:我們現在 用的es7版本 所以我們用 mappings properties 去給多個字段(fields)指定類型的時候 不能給我們的 索引制定類型:
PUT db_test
{
"mappings": {
"properties": {
"name":{
"type":"text"
},
"hoy":{
"type": "keyword"
}
}
}
}
# 插入數據
PUT db_test/_doc/1
{
"name":"我是 name",
"hoy":"我是 hoy"
}
上述中db_test索引中,字段name在被查詢時會被分析器進行分析后匹配查詢。而屬於keyword類型不會被分析器處理。
我們來驗證一下:
GET _analyze
{
"analyzer": "keyword",
"text": "我是 name"
}
結果:
{
"tokens" : [
{
"token" : "我是 name",
"start_offset" : 0,
"end_offset" : 7,
"type" : "word",
"position" : 0
}
]
}
是不是沒有被分析啊。就是簡單的一個字符串啊。
GET _analyze
{
"analyzer": "standard",
"text": "我是 name"
}
結果:
{
"tokens" : [
{
"token" : "我",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "是",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "name",
"start_offset" : 3,
"end_offset" : 7,
"type" : "<ALPHANUM>",
"position" : 2
}
]
}
那么我們看一下 們字符串是不是被分析了啊。
總結:
- keyword 字段類型不會被分析器分析
現在我們來查詢一下:
GET db_test/_search # 是通過 被分析器分析 查詢
{
"query": {
"term": {
"name":"我"
}
}
}
GET db_test/_search # keyword 不回被分析所以直接查詢
{
"query": {
"match": {
"hoy":"我是 hoy"
}
}
}
查找多個精確值(terms)
PUT db_test/_doc/2
{
"t1": "20",
"t2": "2019-4-16"
}
PUT db_test/_doc/3
{
"t1": "30",
"t2": "2019-4-17"
}
# 查詢 精確查找多個值
GET db_test/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"t1": "20"
}
},
{
"term": {
"t1": "30"
}
}
]
}
}
}
除了bool查詢之外
GET db_test/_doc/_search
{
"query": {
"terms": {
"t1": ["20", "30"]
}
} }
GET db_test/_doc/_search
{
"query": {
"terms": {
"t2": ["2019-4-16", "2019-4-17"]
}
} }
官網:see also:Term Query | 查找多個精確值
高亮顯示
GET test/chunsheng/_search
{
"query":{
"match": {
"name": "春生"
}
},
"highlight" :{
"fields": {
"name":{}
}
}
}
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 41,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.271394,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 2.271394,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黑,武器長,性格直",
"tags" : [
"黑",
"長",
"直"
]
},
"highlight" : {
"name" : [
"<em>春</em><em>生</em>"
]
}
}
]
}
}
我們可以看到 "<em>春</em><em>生</em>"
已經幫我們加上了一個<em>
標簽
這是es幫我們加的標簽。那我·也可以自己自定義樣式
自定義高亮顯示
GET test/chunsheng/_search
{
"query":{
"match": {
"name": "春生"
}
},
"highlight" :{
"pre_tags": "<b class='key' style='color:red'>",
"post_tags": "</b>",
"fields": {
"name":{}
}
}
}
返回結果:
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.271394,
"hits" : [
{
"_index" : "test",
"_type" : "chunsheng",
"_id" : "1",
"_score" : 2.271394,
"_source" : {
"name" : "春生",
"age" : 18,
"from" : "gu",
"desc" : "皮膚黑,武器長,性格直",
"tags" : [
"黑",
"長",
"直"
]
},
"highlight" : {
"name" : [
"<b class='key' style='color:red'>春</b><b class='key' style='color:red'>生</b>"
]
}
}
]
}
}
需要注意的是:自定義標簽中屬性或樣式中的逗號一律用英文狀態的單引號表示,應該與外部 es 語法 的雙引號區分開。
Deprecation
注意 elasticsearch 在第一個版本的開始 每個文檔都儲存在一個索引中,並分配一個 映射類型,映射類型用於表示被索引的文檔或者實體的類型,這樣帶來了一些問題 (詳情:https://www.cnblogs.com/Neeo/articles/10393961.html#important)導致后來在 elasticsearch6.0.0 版本中一個文檔只能包含一個映射類型,而在 7.0.0 中,映 射類型則將被棄用,到了 8.0.0 中則將完全被刪除。
解釋一下警告信息:
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}).
#!Deprecation: [types removal]不支持在文檔索引請求中指定類型,而是使用無類型的端點(/{index}/_doc/{id}, /{index}/_doc,或/{index}/_create/{id})。
#! Deprecation: [types removal] Specifying types in search requests is deprecated.
#!Deprecation: [types removal]不贊成在搜索請求中指定類型
我們 7 版本 兼容了 6版本 但是 已經被棄用了 我們的類型還是能用的。在8版本 就完全刪除了。