ElasticSearch的客戶端操作

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]}]}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/42787.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/42787.shtml
英文地址,請注明出處:http://en.pswp.cn/news/42787.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

CF1195E OpenStreetMap 題解

很好的單調隊列題。 題目傳送門 題目意思&#xff1a; 給定一個 n m n\times m nm 的矩陣&#xff0c;求出所有大小為 a b a\times b ab 的子矩形中的最小值的和。 思路&#xff1a; 通過題目給的要求建立二維數組 h h h。通過單調隊列一行一行地掃&#xff0c;將掃出來…

Azure Blob存儲使用

創建存儲賬戶,性能選擇標準即可&#xff0c;冗余選擇本地冗余存儲即可 容器選擇類別選擇專用即可 可以上傳文件到blob中 打開文件可以看到文件的訪問路徑 4.編輯中可以修改文件 復制鏈接&#xff0c;嘗試訪問&#xff0c;可以看到沒有辦法訪問&#xff0c;因為創建容器的時候選…

spring(15) SpringBoot啟動過程

目錄 一、過程簡介二、過程流程圖三、源碼分析1、運行 SpringApplication.run() 方法2、確定應用程序類型3、加載所有的初始化器4、加載所有的監聽器5、設置程序運行的主類6、開啟計時器7、將 java.awt.headless 設置為 true8、獲取并啟用監聽器9、設置應用程序參數10、準備環境…

LeetCode450. 刪除二叉搜索樹中的節點

450. 刪除二叉搜索樹中的節點 文章目錄 [450. 刪除二叉搜索樹中的節點](https://leetcode.cn/problems/delete-node-in-a-bst/)一、題目二、題解方法一&#xff1a;遞歸&#xff08;一種麻煩的方法&#xff09;方法二&#xff1a;優化后的遞歸 一、題目 給定一個二叉搜索樹的根…

SpringBoot校驗,DTO文件中常用的注解應用案例.

在觀看本篇文章之前&#xff0c;可以先參考我之前寫的一篇文章 “ Spring5&#xff0c;Service層對DTO文件進行數據格式校驗. ” &#xff0c;這篇文章是介紹在 Service層 對DTO文件的校驗。 以下方的 CompanyDTO 文件為例&#xff0c;講解不同的注解使用場景&#xff0c;以及…

論文閱讀——Imperceptible Adversarial Attack via Invertible Neural Networks

Imperceptible Adversarial Attack via Invertible Neural Networks 作者&#xff1a;Zihan Chen, Ziyue Wang, Junjie Huang*, Wentao Zhao, Xiao Liu, Dejian Guan 解決的問題&#xff1a;雖然視覺不可感知性是對抗性示例的理想特性&#xff0c;但傳統的對抗性攻擊仍然會產…

每天一道leetcode:1129. 顏色交替的最短路徑(圖論中等廣度優先遍歷)

今日份題目&#xff1a; 給定一個整數 n&#xff0c;即有向圖中的節點數&#xff0c;其中節點標記為 0 到 n - 1。圖中的每條邊為紅色或者藍色&#xff0c;并且可能存在自環或平行邊。 給定兩個數組 redEdges 和 blueEdges&#xff0c;其中&#xff1a; redEdges[i] [ai, bi…

Dubbo Spring Boot Starter 開發微服務應用

環境要求 系統&#xff1a;Windows、Linux、MacOS JDK 8 及以上&#xff08;推薦使用 JDK17&#xff09; Git IntelliJ IDEA&#xff08;可選&#xff09; Docker &#xff08;可選&#xff09; 項目介紹 在本任務中&#xff0c;將分為 3 個子模塊進行獨立開發&#xff…

LINUX學習筆記_GIT操作命令

LINUX學習筆記 GIT操作命令 基本命令 git init&#xff1a;初始化倉庫git status&#xff1a;查看文件狀態git add&#xff1a;添加文件到暫存區&#xff08;index&#xff09;git commit -m “注釋”&#xff1a;提交文件到倉庫&#xff08;repository&#xff09;git log&a…

計算機組成與設計 Patterson Hennessy 筆記(一)MIPS 指令集

計算機的語言&#xff1a;匯編指令集 也就是指令集。本書主要介紹 MIPS 指令集。 匯編指令 算數運算&#xff1a; add a,b,c # abc sub a,b,c # ab-cMIPS 匯編的注釋是 # 號。 由于MIPS中寄存器大小32位&#xff0c;是基本訪問單位&#xff0c;因此也被稱為一個字 word。M…

Java Web常見面試題

1、JSP和Servlet有什么區別 jsp經過編譯后變成類Servlet&#xff08;JSP的本質就是Servelt&#xff0c;JVM只能識別java的類&#xff0c;不能識別jsp的代碼&#xff0c;于是web容器將jsp的代碼編譯成JVM能夠識別的java類&#xff0c;也就是servelt&#xff09;jsp更擅長表現于…

【2023年11月第四版教材】《第5章-信息系統工程之系統集成(第四部分)》

《第5章-信息系統工程之系統集成&#xff08;第四部分&#xff09;》 3 系統集成3.1網絡集成3.2 數據集成3.3 軟件集成3.4 應用集成3.5 安全工程 3 系統集成 3.1網絡集成 安全對策要點傳輸子系統1.常用的無線傳輸介質主要包括無線電波、微波、紅外線等2.常用的有線傳輸介質主…

webpack中常見的Loader

目錄 1.webpack中的loader是什么&#xff1f;配置方式 2. loader特性3.常見的loader 1.webpack中的loader是什么&#xff1f; loader 用于對模塊的"源代碼"進行轉換&#xff0c;在 import 或"加載"模塊時預處理文件 webpack做的事情&#xff0c;僅僅是分…

爬蟲逆向實戰(三)--天某云登錄

一、數據接口分析 主頁地址&#xff1a;天某云 1、抓包 通過抓包可以發現登錄接口是account/login 2、判斷是否有加密參數 請求參數是否加密&#xff1f; 通過“載荷”模塊可以發現password、comParam_signature、comParam_seqCode是加密的 請求頭是否加密&#xff1f; 無…

ElasticSearch 8.9.0 開發模式安裝

ElasticSearch 8.9.0 開發模式安裝 MacOS&#xff08;Apple芯片&#xff09;&#xff1a;https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.9.0-darwin-aarch64.tar.gz Linux&#xff1a;https://artifacts.elastic.co/downloads/elasticsearch/elasti…

git倉庫新建上傳記錄

新建git倉會出現版本分支問題&#xff0c;解決過程&#xff1a; 其他的前期綁定之類的傳送&#xff1a;https://blog.csdn.net/qq_37194189/article/details/130767397 大概思路&#xff1a;新建一個分支&#xff0c;上傳&#xff0c;合并&#xff0c;刪除分支 git branch …

4.2 C++ Boost 內存池管理庫

Boost 庫是一個由C/C語言的開發者創建并更新維護的開源類庫&#xff0c;其提供了許多功能強大的程序庫和工具&#xff0c;用于開發高質量、可移植、高效的C應用程序。Boost庫可以作為標準C庫的后備&#xff0c;通常被稱為準標準庫&#xff0c;是C標準化進程的重要開發引擎之一。…

cmake擴展(5)——file命令排除部分文件

在cmake中可以使用file命令獲取需要的文件&#xff0c;并且支持正則/通配符&#xff0c;使用起來還是很方便的。 #語法file({GLOB | GLOB_RECURSE} <out-var> [...] [<globbing-expr>...])#example file(GLOB_RECURSE SOURCES "src/*.h" "src/*.cp…

HTTP與HTTPS的區別

面試常見問題&#xff0c;HTTPS優化總結易記版&#xff1a; 1、HSTS重定向技術&#xff1a;將http自動轉換為https&#xff0c;減少301重定向 2、TLS握手優化&#xff1a;在TLS握手完成前客戶端就提前向服務器發送數據 3、會話標識符&#xff1a;服務器記錄下與某客戶端的會…

Mac鼠標增強工具Smooze Pro

Smooze Pro是一款Mac上的鼠標手勢增強工具&#xff0c;可以讓用戶使用鼠標手勢來控制應用程序和系統功能。 它支持多種手勢操作&#xff0c;包括單指、雙指、三指和四指手勢&#xff0c;并且可以自定義每種手勢的功能。例如&#xff0c;您可以使用單指向下滑動手勢來啟動Expos視…