ElasticSearch的客戶端操作
1、客戶端介紹
官方文檔地址: https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
實際開發中,有多種方式操作Elasticsearch:
客戶端工具:發送http請求(RESTful風格)操作:9200端口
使用Postman發送請求直接操作
使用ElasticSearch-head-master圖形化界面插件操作
使用Elastic官方數據可視化的平臺Kibana進行操作【推薦】
Java代碼操作:9300端口
Elasticsearch提供的Java API 客戶端進行操作
Spring Data ElasticSearch 持久層框架進行操作
官網支持的客戶端訪問方式:
https://www.elastic.co/guide/en/elasticsearch/client/index.html
2、索引庫操作
使用Kibana進行以下實驗,進行Restful接口訪問
索引庫操作,完成對索引的增、刪、查操作
1. 創建索引庫(index)
發送請求:
# 在kibana中,不用寫地址和端口,/shopping是簡化寫法,真實請求地址是:http://127.0.0.1:9200/shopping
# 請求方法:PUT
PUT /shopping
響應結果:
#! Deprecation: the default number of shards will change from [5] to [1] in 7.0.0; if you wish to continue using the default of [5] shards, you must manage this on the create index request or with an index template
{"acknowledged" : true,"shards_acknowledged" : true,"index" : "shopping"
}
“acknowledged” : true, 代表操作成功
“shards_acknowledged” : true, 代表分片操作成功
“index” : “shopping” 表示創建的索引庫名稱
注意:創建索引庫的分片數默認5片,在7.0.0之后的ElasticSearch版本中,默認1片;
重復添加:報錯,已經存在
2. 查看所有索引(index)
發送請求:
# 請求方法:GET
GET /_cat/indices?v
響應結果:
表頭的含義(查看幫助信息:GET /_cat/indices?help)
health 當前服務器健康狀態:
green(集群完整) yellow(單點正常、集群不完整) red(單點不正常)
status 索引打開、關閉狀態
index 索引名
uuid 索引統一編號
pri 主分片數量
rep 副分片數量
docs.count 可用文檔數量
docs.deleted 文檔刪除狀態(邏輯刪除,段合并時被清理)
store.size 主分片和副分片整體占空間大小
pri.store.size 主分片占空間大小
3. 查看某個索引(index)
發送請求:
# 請求方法:GET
GET /shopping
響應結果:
{"shopping" : {"aliases" : { },"mappings" : { },"settings" : {"index" : {"creation_date" : "1586587411462","number_of_shards" : "5","number_of_replicas" : "1","uuid" : "VCl1hHsJQDe2p2dn46o0NA","version" : {"created" : "6080199"},"provided_name" : "shopping"}}}
}
內容解釋:
{"shopping【索引庫名】" : {"aliases【別名】" : { },"mappings【映射】" : { },"settings"【索引庫設置】 : {"index【索引】" : {"creation_date【創建時間】" : "1586587411462","number_of_shards【索引庫分片數】" : "5","number_of_replicas【索引庫副本數】" : "1","uuid【唯一標識】" : "VCl1hHsJQDe2p2dn46o0NA","version【版本】" : {"created" : "6080199"},"provided_name【索引庫名稱】" : "shopping"}}}
}
4. 刪除索引(index)
發送請求:
# 請求方法:DELETE
DELETE /shopping
響應結果:
{"acknowledged" : true
}
3、類型及映射操作
類型(type)及(mapping)操作
1. 創建類型映射
有了索引庫,等于有了數據庫中的database。
接下來就需要建索引庫(index)中的類型(type)了,類似于數據庫(database)中的表(table)。創建數據庫表需要設置字段名稱,類型,長度,約束等;索引庫也一樣,在創建索引庫的類型時,需要知道這個類型下有哪些字段,每個字段有哪些約束信息,這就叫做映射(mapping)。
給shopping這個索引庫添加了一個名為product的類型,并且在類型中設置了4個字段:
title:商品標題
subtitle: 商品子標題
images:商品圖片
price:商品價格
發送請求:
# 請求方法:PUT
PUT /shopping/product/_mapping
{"properties": {"title":{"type": "text","analyzer": "ik_max_word"},"subtitle":{"type": "text","analyzer": "ik_max_word"},"images":{"type": "keyword","index": false},"price":{"type": "float","index": true}}
} PUT /索引庫名/_mapping/類型名稱 或 索引庫名/類型名稱/_mapping
{"properties": {"字段名稱":{"type【類型】": "類型","index【是否索引】": true,"store【是否存儲】": false,"analyzer【分詞器】": "具體分詞器"}...}
}響應結果:
#! Deprecation: [types removal] Specifying types in put mapping requests is deprecated. To be compatible with 7.0, the mapping definition should not be nested under the type name, and the parameter include_type_name must be provided and set to false.
{"acknowledged" : true
}
說明:
#! 棄用:[類型刪除]不建議在放置映射請求中指定類型。 為了與7.0兼容,映射定義不應嵌套在類型名稱下,并且必須提供參數include_type_name并將其設置為false。
類型名稱:就是前面將的type的概念,類似于數據庫中的表
字段名:任意填寫,下面指定許多屬性,例如:title、subtitle、images、price
type:類型,Elasticsearch中支持的數據類型非常豐富,說幾個關鍵的:
①String類型,又分兩種:
text:可分詞
keyword:不可分詞,數據會作為完整字段進行匹配
②Numerical:數值類型,分兩類
基本數據類型:long、interger、short、byte、double、float、half_float
浮點數的高精度類型:scaled_float
③Date:日期類型
④Array:數組類型
⑤Object:對象
index:是否索引,默認為true,也就是說你不進行任何配置,所有字段都會被索引。
true:字段會被索引,則可以用來進行搜索
false:字段不會被索引,不能用來搜索
store:是否將數據進行獨立存儲,默認為false
原始的文本會存儲在_source里面,默認情況下其他提取出來的字段都不是獨立存儲的,是從_source里面提取出來的。當然你也可以獨立的存儲某個字段,只要設置"store": true即可,獲取獨立存儲的字段要比從_source中解析快得多,但是也會占用更多的空間,所以要根據實際業務需求來設置。
analyzer:分詞器,這里的ik_max_word即使用ik分詞器
2. 查看類型映射
發送請求:
# 請求方法:GET
GET /shopping/product/_mapping
響應結果:
{"shopping" : {"mappings" : {"product" : {"properties" : {"images" : {"type" : "keyword","index" : false},"price" : {"type" : "float"},"subtitle" : {"type" : "text","analyzer" : "ik_max_word"},"title" : {"type" : "text","analyzer" : "ik_max_word"}}}}}
}
3. 創建索引庫同時進行映射配置(常用)
發送請求:
# 請求方法:PUT
PUT /shopping2
{"settings": {},"mappings": {"product":{"properties": {"title":{"type": "text","analyzer": "ik_max_word"},"subtitle":{"type": "text","analyzer": "ik_max_word"},"images":{"type": "keyword","index": false},"price":{"type": "float","index": true}}}}
}
響應結果:
{"acknowledged" : true,"shards_acknowledged" : true,"index" : "shopping2"
}
4、【文檔操作】【基本CURD操作】
1. 新建文檔
發送請求:
# 請求方法:POST
POST /shopping/product
{"title":"小米手機","images":"http://www.gulixueyuan.com/xm.jpg","price":3999.00
}
響應結果:
{"_index" : "shopping","_type" : "product","_id" : "indGaHEB1ahbZ0SRrXt3","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1
}
響應結果解釋:
{"_index【索引庫】" : "shopping","_type【類型】" : "product","_id【主鍵id】" : "indGaHEB1ahbZ0SRrXt3","_version【版本】" : 1,"result【操作結果】" : "created","_shards【分片】" : {"total【總數】" : 2,"successful【成功】" : 1,"failed【失敗】" : 0},"_seq_no" : 0,"_primary_term" : 1
}
可以看到結果顯示為:created
,是創建成功了。
另外,需要注意的是,在響應結果中有個_id
字段,這個就是這條文檔數據的唯一標識
,以后的增刪改查都依賴這個id作為唯一標示。可以看到id的值為:indGaHEB1ahbZ0SRrXt3,這里我們新增時沒有指定id,所以是ES幫我們隨機生成的id。
多創建幾條數據:
POST /shopping/product/2
{"title":"華為手機","images":"http://www.gulixueyuan.com/hw.jpg","price":4999.00
}POST /shopping/product/3
{"title":"小米電視","images":"http://www.gulixueyuan.com/xmds.jpg","price":5999.00
}
2. 查看文檔
發送請求:
# 請求方法:GET
GET /shopping/product/indGaHEB1ahbZ0SRrXt3
響應結果:
{"_index" : "shopping","_type" : "product","_id" : "indGaHEB1ahbZ0SRrXt3","_version" : 1,"_seq_no" : 0,"_primary_term" : 1,"found" : true,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}
}
響應結果解釋:
{"_index【索引庫】" : "shopping","_type【類型】" : "product","_id【主鍵id】" : "indGaHEB1ahbZ0SRrXt3","_version【版本】" : 1,"_seq_no" : 0,"_primary_term" : 1,"found【查詢結果】" : true,"_source【源文檔信息】" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}
}
? _source:源文檔信息,所有的數據都在里面。
? _id:這條文檔的唯一標示
? found:查詢結果,返回true代表查到,false代表沒有
3. 自定義id新建文檔
發送請求:
# 請求方法:POST
POST /shopping/product/1
{"title":"小米手機","images":"http://www.gulixueyuan.com/xm.jpg","price":3999.00
}
響應結果:
{"_index" : "shopping","_type" : "product","_id" : "1","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 1,"_primary_term" : 1
}
? 主鍵id變為指定的id
4. 修改文檔(覆蓋方式)
請求url不變,請求體變化,會將原有數據內容覆蓋。
發送請求:
# 請求方法:POST
POST /shopping/product/1
{"title":"華為手機","images":"http://www.gulixueyuan.com/hw.jpg","price":4999.00
}
響應結果:
{"_index" : "shopping","_type" : "product","_id" : "1","_version" : 2,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 2,"_primary_term" : 1
}
可以看到result結果是:updated,使用GET /shopping/product/1查詢,發現數據被更新。
5. 根據id修改某一個字段
發送請求:
# 請求方法:POST
POST /shopping/product/1/_update
{ "doc": {"price":3000.00}
}
響應結果:
{"_index" : "shopping","_type" : "product","_id" : "1","_version" : 2,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 8,"_primary_term" : 1
}
可以看到result結果是:updated,使用GET /shopping/product/1查詢,發現數據被更新。
6. 刪除一條文檔
刪除一個文檔不會立即從磁盤上移除,它只是被標記成已刪除(邏輯刪除)。
Elasticsearch會在段合并時(磁盤碎片整理)進行刪除內容的清理。
發送請求:
# 請求方法:DELETE
DELETE /shopping/product/1
響應結果:
{"_index" : "shopping","_type" : "product","_id" : "1","_version" : 3,"result" : "deleted","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 3,"_primary_term" : 1
}
可以看到result結果是:deleted,數據被刪除。如果刪除不存在的文檔,result:not_found
例如:
DELETE /shopping/product/11主鍵不存在
{"_index" : "shopping","_type" : "product","_id" : "11","_version" : 1,"result" : "not_found","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1
}
7. 根據條件刪除文檔
發送請求:
# 請求方法:DELETE
POST /shopping/_delete_by_query
{"query":{"match":{"title":"手機"}}
}
響應結果:
{"took" : 33,"timed_out" : false,"total" : 2,"deleted" : 2,"batches" : 1,"version_conflicts" : 0,"noops" : 0,"retries" : {"bulk" : 0,"search" : 0},"throttled_millis" : 0,"requests_per_second" : -1.0,"throttled_until_millis" : 0,"failures" : [ ]
}
響應結果解釋:
{"took【耗時】" : 33,"timed_out【是否超時】" : false,"total【總數】" : 2,"deleted【刪除總數】" : 2,"batches" : 1,"version_conflicts" : 0,"noops" : 0,"retries" : {"bulk" : 0,"search" : 0},"throttled_millis" : 0,"requests_per_second" : -1.0,"throttled_until_millis" : 0,"failures" : [ ]
}
5、【請求體查詢】【基本查詢】
1. 請求體查詢
Elasticsearch基于JSON提供完整的查詢DSL來定義查詢。
DSL(Domain Specific Language):領域特定語言
2. 基礎數據
POST /shopping/product/1
{"title":"小米手機","images":"http://www.gulixueyuan.com/xm.jpg","price":3999.00
}POST /shopping/product/2
{"title":"華為手機","images":"http://www.gulixueyuan.com/hw.jpg","price":4999.00
}POST /shopping/product/3
{"title":"小米電視","images":"http://www.gulixueyuan.com/xmds.jpg","price":5999.00
}
3. 基本查詢
1) 查詢所有(match_all)
發送請求:
# 請求方法:GET
#請求地址:http://127.0.0.1:9200/索引庫名/_search
GET /shopping/_search
{"query": {"match_all": {}}
}
請求解釋:
GET /{索引庫}/_search
{"query":{"查詢類型":{"查詢條件":"查詢條件值"}}
}
"query":這里的query代表一個查詢對象,里面可以有不同的查詢屬性
"查詢類型":例如:match_all(代表查詢所有), match,term , range 等等
"查詢條件":查詢條件會根據類型的不同,寫法也有差異
響應結果:
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 3,"max_score" : 1.0,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "2","_score" : 1.0,"_source" : {"title" : "華為手機","images" : "http://www.gulixueyuan.com/hw.jpg","price" : 4999.0}},{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 1.0,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}},{"_index" : "shopping","_type" : "product","_id" : "3","_score" : 1.0,"_source" : {"title" : "小米電視","images" : "http://www.gulixueyuan.com/xmds.jpg","price" : 5999.0}}]}
}
響應結果解釋:
{"took【查詢花費時間,單位毫秒】" : 1,"timed_out【是否超時】" : false,"_shards【分片信息】" : {"total【總數】" : 5,"successful【成功】" : 5,"skipped【忽略】" : 0,"failed【失敗】" : 0},"hits【搜索命中結果】" : {"total【命中總數】" : 3,"max_score【所有查詢結果中,文檔的最高得分】" : 1.0,"hits【命中結果集合】" : [{"_index" : "shopping","_type" : "product","_id" : "2","_score" : 1.0,"_source" : {"title" : "華為手機","images" : "http://www.gulixueyuan.com/hw.jpg","price" : 4999.0}},。。。}]}
}
2) 匹配查詢(match)
match類型查詢,會把查詢條件進行分詞,然后進行查詢,多個詞條之間是or的關系
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"match": {"title": "小米手機"}}
}
響應結果:
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 3,"max_score" : 0.5753642,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 0.5753642,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}},{"_index" : "shopping","_type" : "product","_id" : "2","_score" : 0.2876821,"_source" : {"title" : "華為手機","images" : "http://www.gulixueyuan.com/hw.jpg","price" : 4999.0}},{"_index" : "shopping","_type" : "product","_id" : "3","_score" : 0.2876821,"_source" : {"title" : "小米電視","images" : "http://www.gulixueyuan.com/xmds.jpg","price" : 5999.0}}]}
}
在上面的案例中,不僅會查詢到電視,而且與小米相關的都會查詢到。
某些情況下,我們需要更精確查找,我們希望這個關系變成and,可以這樣做:
發送請求:
本例中,只有同時包含小米和手機的詞條才會被搜索到。
GET /shopping/_search
{"query": {"match": {"title": {"query": "小米手機","operator": "and"}}}
}
響應結果:
{"took" : 11,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 1,"max_score" : 0.5753642,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 0.5753642,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}}]}
}
3) 多字段匹配查詢(multi_match)
multi_match與match類似,不同的是它可以在多個字段中查詢。
發送請求:
# 請求方法:GET
#fields屬性:設置查詢的多個字段名稱
GET /shopping/_search
{"query": {"multi_match": {"query": "小米","fields": ["title","subtitle"]}}
}
響應結果:
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 2,"max_score" : 0.2876821,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 0.2876821,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}},{"_index" : "shopping","_type" : "product","_id" : "3","_score" : 0.2876821,"_source" : {"title" : "小米電視","images" : "http://www.gulixueyuan.com/xmds.jpg","price" : 5999.0}}]}
}
4) 關鍵詞精確查詢(term)
term查詢,精確的關鍵詞匹配查詢,不對查詢條件進行分詞。
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"term": {"title": {"value": "小米"}}}
}
響應結果:
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 2,"max_score" : 0.2876821,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 0.2876821,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}},{"_index" : "shopping","_type" : "product","_id" : "3","_score" : 0.2876821,"_source" : {"title" : "小米電視","images" : "http://www.gulixueyuan.com/xmds.jpg","price" : 5999.0}}]}
}
5) 多關鍵詞精確查詢(terms)
terms 查詢和 term 查詢一樣,但它允許你指定多值進行匹配。
如果這個字段包含了指定值中的任何一個值,那么這個文檔滿足條件,類似于mysql的in
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"terms": {"price": [3999,5999]}}
}
響應結果:
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 2,"max_score" : 1.0,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 1.0,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}},{"_index" : "shopping","_type" : "product","_id" : "3","_score" : 1.0,"_source" : {"title" : "小米電視","images" : "http://www.gulixueyuan.com/xmds.jpg","price" : 5999.0}}]}
}
6、【請求體查詢】【結果過濾】
1. 指定查詢字段
默認情況下,ElasticSearch在搜索的結果中,會把文檔中保存在_source的所有字段都返回。
如果我們只想獲取其中的部分字段,我們可以添加_source的過濾
發送請求:
# 請求方法:GET
GET /shopping/_search
{"_source": ["title","price"], "query": {"terms": {"price": [3999]}}
}
響應結果:
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 1,"max_score" : 1.0,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 1.0,"_source" : {"price" : 3999.0,"title" : "小米手機"}}]}
}
2. 過濾指定字段:includes和excludes
我們也可以通過:
? includes:來指定想要顯示的字段
? excludes:來指定不想要顯示的字段
二者都是可選的。
發送請求:
# 請求方法:GET
GET /shopping/_search
{"_source": {"includes": ["title","price"]}, "query": {"terms": {"price": [3999]}}
}GET /shopping/_search
{"_source": {"excludes": ["images"]}, "query": {"terms": {"price": [3999]}}
}
響應結果:
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 1,"max_score" : 1.0,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 1.0,"_source" : {"price" : 3999.0,"title" : "小米手機"}}]}
}
7、【請求體查詢】【高級查詢】
1. 布爾組合(bool)
bool
把各種其它查詢通過must
(必須 )、must_not
(必須不)、should
(應該)的方式進行組合
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"bool": {"must": [{"match": {"title": "小米"}}],"must_not": [{"match": {"title": "電視"}}],"should": [{"match": {"title": "手機"}}]}}
}
響應結果:
{"took" : 2,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 1,"max_score" : 0.5753642,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 0.5753642,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}}]}
}
2. 范圍查詢(range)
range 查詢找出那些落在指定區間內的數字或者時間。range查詢允許以下字符:
操作符 說明
gt == (greater than) 大于>
gte == (greater than equal) 大于等于>=
lt == (less than) 小于<
lte == (less than equal) 小于等于<=
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"range": {"price": {"gte": 2500,"lte": 4000}}}
}
響應結果:
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 1,"max_score" : 1.0,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 1.0,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0}}]}
}
3. 模糊查詢(fuzzy)
返回包含與搜索字詞相似的字詞的文檔。
編輯距離是將一個術語轉換為另一個術語所需的一個字符更改的次數。這些更改可以包括:
更改字符(box → fox)
刪除字符(black → lack)
插入字符(sic → sick)
轉置兩個相鄰字符(act → cat)
為了找到相似的術語,fuzzy查詢會在指定的編輯距離內創建一組搜索詞的所有可能的變體或擴展。然后查詢返回每個擴展的完全匹配。
通過fuzziness修改編輯距離。一般使用默認值AUTO,根據術語的長度生成編輯距離。
0…2
必須完全匹配
3…5
允許一次編輯
5
允許進行兩次編輯
POST /shopping/product/4
{"title":"apple手機","images":"http://www.gulixueyuan.com/apple.jpg","price":5999.00
}POST /shopping/product/5
{"title":"apple","images":"http://www.gulixueyuan.com/apple.jpg","price":4999.00
}
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"fuzzy": {"title": {"value": "ccple"}}}
} GET /shopping/_search
{"query": {"fuzzy": {"title": {"value": "ccple","fuzziness": 2}}}
}響應結果:
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 0,"max_score" : null,"hits" : [ ]}
} {"took" : 1,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 2,"max_score" : 0.41588834,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "4","_score" : 0.41588834,"_source" : {"title" : "apple手機","images" : "http://www.gulixueyuan.com/apple.jpg","price" : 5999.0}},{"_index" : "shopping","_type" : "product","_id" : "5","_score" : 0.17260925,"_source" : {"title" : "apple","images" : "http://www.gulixueyuan.com/apple.jpg","price" : 4999.0}}]}
}
8、【請求體查詢】【查詢排序】
1. 單字段排序
sort 可以讓我們按照不同的字段進行排序,并且通過order指定排序的方式。desc降序,asc升序。
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"match_all": {}},"sort": [{"price": {"order": "desc"}}]
}
響應結果:
{"took" : 4,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 5,"max_score" : null,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "4","_score" : null,"_source" : {"title" : "apple手機","images" : "http://www.gulixueyuan.com/apple.jpg","price" : 5999.0},"sort" : [5999.0]},{"_index" : "shopping","_type" : "product","_id" : "3","_score" : null,"_source" : {"title" : "小米電視","images" : "http://www.gulixueyuan.com/xmds.jpg","price" : 5999.0},"sort" : [5999.0]},{"_index" : "shopping","_type" : "product","_id" : "5","_score" : null,"_source" : {"title" : "apple","images" : "http://www.gulixueyuan.com/apple.jpg","price" : 4999.0},"sort" : [4999.0]},{"_index" : "shopping","_type" : "product","_id" : "2","_score" : null,"_source" : {"title" : "華為手機","images" : "http://www.gulixueyuan.com/hw.jpg","price" : 4999.0},"sort" : [4999.0]},{"_index" : "shopping","_type" : "product","_id" : "1","_score" : null,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0},"sort" : [3999.0]}]}
}
2. 多字段排序
假定我們想要結合使用 price和 _score(得分) 進行查詢,并且匹配的結果首先按照價格排序,然后按照相關性得分排序:
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"match_all": {}},"sort": [{"price": {"order": "desc"}},{"_score":{"order": "desc"}}]
}
響應結果:
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 5,"max_score" : null,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "4","_score" : 1.0,"_source" : {"title" : "apple手機","images" : "http://www.gulixueyuan.com/apple.jpg","price" : 5999.0},"sort" : [5999.0,1.0]},{"_index" : "shopping","_type" : "product","_id" : "3","_score" : 1.0,"_source" : {"title" : "小米電視","images" : "http://www.gulixueyuan.com/xmds.jpg","price" : 5999.0},"sort" : [5999.0,1.0]},{"_index" : "shopping","_type" : "product","_id" : "5","_score" : 1.0,"_source" : {"title" : "apple","images" : "http://www.gulixueyuan.com/apple.jpg","price" : 4999.0},"sort" : [4999.0,1.0]},{"_index" : "shopping","_type" : "product","_id" : "2","_score" : 1.0,"_source" : {"title" : "華為手機","images" : "http://www.gulixueyuan.com/hw.jpg","price" : 4999.0},"sort" : [4999.0,1.0]},{"_index" : "shopping","_type" : "product","_id" : "1","_score" : 1.0,"_source" : {"title" : "小米手機","images" : "http://www.gulixueyuan.com/xm.jpg","price" : 3999.0},"sort" : [3999.0,1.0]}]}
}
9、【請求體查詢】【高亮查詢】
在進行關鍵字搜索時,搜索出的內容中的關鍵字會顯示不同的顏色,稱之為高亮。
在百度搜索"京東"
在京東網站搜索“小米”
高亮顯示的html分析
通過開發者工具查看高亮數據的html代碼實現:
高亮查詢請求
ElasticSearch可以對查詢內容中的關鍵字部分,進行標簽和樣式(高亮)的設置。
在使用match查詢的同時,加上一個highlight屬性:
pre_tags:前置標簽
post_tags:后置標簽
fields:需要高亮的字段
title:這里聲明title字段需要高亮,后面可以為這個字段設置特有配置,也可以空
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"match": {"title": "華為"}},"highlight": {"pre_tags": "<font color='red'>","post_tags": "</font>","fields": {"title": {}}}
}
響應結果:
{"took" : 1,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 1,"max_score" : 0.6931472,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "2","_score" : 0.6931472,"_source" : {"title" : "華為手機","images" : "http://www.gulixueyuan.com/hw.jpg","price" : 4999.0},"highlight" : {"title" : ["<font color='red'>華為</font>手機"]}}]}
}
10、【請求體查詢】【分頁查詢】
from:當前頁的起始索引,默認從0開始。 from = (pageNum - 1) * size
size:每頁顯示多少條
發送請求:
# 請求方法:GET
GET /shopping/_search
{"query": {"match_all": {}},"sort": [{"price": {"order": "desc"}},{"_score":{"order": "desc"}}],"from": 0,"size": 2
}響應結果:
{"took" : 0,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"skipped" : 0,"failed" : 0},"hits" : {"total" : 5,"max_score" : null,"hits" : [{"_index" : "shopping","_type" : "product","_id" : "4","_score" : 1.0,"_source" : {"title" : "apple手機","images" : "http://www.gulixueyuan.com/apple.jpg","price" : 5999.0},"sort" : [5999.0,1.0]},{"_index" : "shopping","_type" : "product","_id" : "3","_score" : 1.0,"_source" : {"title" : "小米電視","images" : "http://www.gulixueyuan.com/xmds.jpg","price" : 5999.0},"sort" : [5999.0,1.0]}]}
}