日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

elasticsearch7使用指导

發布時間:2024/7/5 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 elasticsearch7使用指导 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄結構:

一、es概述

二、es安裝/head插件安裝/kibana安裝

三、es核心概念

四、IK分詞器

五、RestFul操作

六、CRUD(增刪改查)

七、Springboot集成es

---------------------------------------分割線:正文--------------------------------------------------------

一、es概述

1、Doug Cutting

(1)創建了Lucene,(es與solr基于Lucene), 開源且功能為一套信息檢索工具包,jar包

(2)實現了NDFS(分布式文件存儲系統)基于Google的GFS系統

(3)將NDFS與MapReduce升級改造,重新命名為Hadoop,hadoop之父

(4)基于Google的BigTable,實現了HBase

2、ElasticSearch概述?

(1)概念:開源的分布式全文檢索引擎

(2)優點:近乎實時存儲、檢索數據,擴展性好,可擴展上百臺服務器,處理PB級別的數據,基于Lucene核心,通過RestFul API使全文搜索變得簡單

(3)誰在使用:維基百科,搜狐新聞,Github等等

3、ES與solr的區別

(1)ES是RestFul API,而Solr是WebService API

(2)單純已有的數據搜索,solr更快

(3)建立索引后的數據,Solr會產生io阻塞,es有明顯的優勢

(4)數據量增加,Solr的搜索效率會變低,es不受影響

?

二、es安裝/head插件安裝/kibana安裝

?聲明:安裝條件,JDK1.8+

?1、es下載:

(1)es客戶端:https://www.elastic.co/cn/downloads/elasticsearch

(2)ik分詞器:https://github.com/medcl/elasticsearch-analysis-ik/releases

(3)kibana:https://www.elastic.co/cn/downloads/kibana

(4)head插件:https://github.com/mobz/elasticsearch-head/archive/master.zip

(5)chrome-es插件:https://github.com/mobz/elasticsearch-head/blob/master/crx/es-head.crx

2、es客戶端windows安裝

?(1)安裝步驟

elasticsearch-7.12.1-windows-x86_64.zip 解壓即可。

(2)相關文件

bin:啟動文件

config:配置文件

- - log4j2.properties:日志配置

- -?elasticsearch.yml:es配置文件,默認9200端口

- -?jvm.options:java虛擬機相關配置

lib:相關jar包

modules:功能模塊

plugins:插件!如ik分詞器等

logs:日志

(3)啟動es

雙擊bin目錄下elasticsearch.bat

?啟動成功,默認訪問9200端口

?瀏覽器訪問:http://127.0.0.1:9200/ 返回相關配置信息

3、安裝可視化界面:es head的插件

(1)解壓縮:elasticsearch-head-master.zip

(2)安裝node.js:https://nodejs.org/en/

cmd:node -v驗證安裝成功

(3)安裝cnpm:

cmd安裝淘寶鏡像:npm install -g cnpm -registry=https://registry.npm.taobao.org

?cnpm -v驗證安裝成功

(4)安裝依賴:

進入D:\elasticsearch\elasticsearch-head-master目錄;執行cnpm install;

?(5)運行npm run start啟動npm

?(6)瀏覽器訪問:http://localhost:9100

由于跨域無法訪問:

?(7)配置可跨域

配置conf下elasticsearch.yml新增配置

http.cors.enabled: true http.cors.allow-origin: "*"

重啟es服務,再次訪問,集群連接正常

4、Kibana安裝

(1)Kibana安裝的版本要與es版本對應:

(2)啟動測試:運行bin下kibana.bat并測試對應的接口

(3)開發工具?http://localhost:5601

?

?

?(4)配置中文:config下kibana.yml,新增配置并重啟服務

i18n.locale: "zh-CN"

?

?

三、es核心概念

?1、elasticsearch是面向文檔的(一切都是json),與關系型數據庫的對比

RelationDB? -> Elasticsearch

數據庫(database) -> 索引(indices)

表(tables) -> types(慢慢會被棄用)

行(rows) -> documents

字段(columns) -> fields

2、es物理設計:

elasticsearch在后臺把每個索引劃分成多個分片,每個分片在集群中的不同的服務間遷移。

默認的集群名就是elasticsearch

3、邏輯設計:

(1)文檔:最小單位,即一條條數據

(2)類型:text、date等類型

(3)索引:數據庫

4、es字段類型:

text:會被分詞器解析

keyword:不會被分詞器解析

?

四、IK分詞器

1、分詞:

把一段中文或別的劃分為一個個關鍵字,我們在搜索時候會把自己的信息進行分詞,默認的中文分詞器將每個字看成一個詞,我們需安裝使用中文分詞器ik來解決

2、安裝ik分詞器:

(1)下載:https://github.com/medcl/elasticsearch-analysis-ik/releases

(2)安裝:elasticsearch-analysis-ik-7.12.1.zip放入elasticsearch-7.12.1\plugins目錄下解壓縮即可使用:

?(3)重啟觀察ES,加載插件成功

[2021-05-12T23:00:05,420][INFO ][o.e.p.PluginsService ] [DESKTOP-GIEVO4Q] loaded plugin [analysis-ik] [2021-05-12T23:00:05,615][INFO ][o.e.e.NodeEnvironment ] [DESKTOP-GIEVO4Q] using [1] data paths, mounts [[杞歡 (D:)]], net usable_space [179.3gb], net total_space [300.7gb], types [NTFS]

(4)驗證通過:elasticsearch-plugin list

D:\elasticsearch\elasticsearch-7.12.1\bin>elasticsearch-plugin list "warning: usage of JAVA_HOME is deprecated, use ES_JAVA_HOME" Future versions of Elasticsearch will require Java 11; your Java version from [C:\Program Files\Java\jdk1.8.0_271\jre] does not meet this requirement. Consider switching to a distribution of Elasticsearch with a bundled JDK. If you are already using a distribution with a bundled JDK, ensure the JAVA_HOME environment variable is not set. ik

3、使用kibana進行測試

啟動kibana進入控制臺進行測試

查看不同的分詞器效果

(1)ik_smart

GET _analyze {"analyzer": "ik_smart","text":"軟件工程師" }

查看運行結果:

{"tokens" : [{"token" : "軟件","start_offset" : 0,"end_offset" : 2,"type" : "CN_WORD","position" : 0},{"token" : "工程師","start_offset" : 2,"end_offset" : 5,"type" : "CN_WORD","position" : 1}] }

(2)ik_max_word

GET _analyze {"analyzer": "ik_max_word","text":"軟件工程師" }

查看運行結果:

{"tokens" : [{"token" : "軟件工程","start_offset" : 0,"end_offset" : 4,"type" : "CN_WORD","position" : 0},{"token" : "軟件","start_offset" : 0,"end_offset" : 2,"type" : "CN_WORD","position" : 1},{"token" : "工程師","start_offset" : 2,"end_offset" : 5,"type" : "CN_WORD","position" : 2},{"token" : "工程","start_offset" : 2,"end_offset" : 4,"type" : "CN_WORD","position" : 3},{"token" : "師","start_offset" : 4,"end_offset" : 5,"type" : "CN_CHAR","position" : 4}] }

4、將加入自己需要的詞加到分詞器的字典中

打開:elasticsearch-7.12.1\plugins\ik\config\IKAnalyzer.cfg.xml,配置<entry key="ext_dict"></entry>內添加字段映射

?(1)配置前

GET _analyze {"analyzer": "ik_max_word","text":"我的媽" }

查看運行結果:

{"tokens" : [{"token" : "我","start_offset" : 0,"end_offset" : 1,"type" : "CN_CHAR","position" : 0},{"token" : "的","start_offset" : 1,"end_offset" : 2,"type" : "CN_CHAR","position" : 1},{"token" : "媽","start_offset" : 2,"end_offset" : 3,"type" : "CN_CHAR","position" : 2}] }

(2)配置

<entry key="ext_dict">my.dic</entry>

同目錄下新增my.dic -> 我的媽

(3)配置后重啟es,控制臺

{"tokens" : [{"token" : "我的媽","start_offset" : 0,"end_offset" : 3,"type" : "CN_WORD","position" : 0}] }

查看運行結果:

{"tokens" : [{"token" : "我的媽","start_offset" : 0,"end_offset" : 3,"type" : "CN_WORD","position" : 0}] }

?

五、RestFul索引基本操作

?1、基本Rest命令:

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/索引名稱/類型名稱/文檔id/_search? |? ?查詢所有文檔

2、基礎測試-創建索引

創建一個索引:PUT /索引名/~類型名~/文檔id

PUT /test1/type1/1 {"name":"小白學es","age":3 }

查看響應,完成自動增加了索引,類似數據庫

#! [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}). {"_index" : "test1","_type" : "type1","_id" : "1","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1 }

3、基本數據類型

(1)字符串類型:text、keyword

(2)數值類型:long、integer、short、byte、double、half float、scaled、float

(3)日期類型:date

(4)te布爾類型:boolean

(5)二進制類型:binary

4、基礎測試-創建并指定索引類型

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

查看運行結果

{"acknowledged" : true,"shards_acknowledged" : true,"index" : "test2" }

5、?基礎測試-創建/查看默認信息

(1)創建默認索引即文檔

PUT /test3/_doc/1 {"name":"小白學es","age":18,"bithday":"1997-07-07" }

查看運行結果

{"_index" : "test3","_type" : "_doc","_id" : "1","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1 }

插件查看

?(2)查看索引,默認配置字段類型!

GET test3

運行結果:

{"test3" : {"aliases" : { },"mappings" : {"properties" : {"age" : {"type" : "long"},"bithday" : {"type" : "date"},"name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}},"settings" : {"index" : {"routing" : {"allocation" : {"include" : {"_tier_preference" : "data_content"}}},"number_of_shards" : "1","provided_name" : "test3","creation_date" : "1621064937104","number_of_replicas" : "1","uuid" : "f_-oPx65RwOk846CysvHRQ","version" : {"created" : "7120199"}}}} }

6、擴展,查看集群/索引信息:

查看集群狀態:

GET _cat/health

運行結果:

1621065190 07:53:10 elasticsearch yellow 1 1 9 9 0 0 2 0 - 81.8%

查看索引版本信息等:

GET _cat/indices?v

運行結果:

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .kibana_7.12.1_001 aGaAAOZTSQyk_0ZU_lOXsg 1 0 54 23 4.2mb 4.2mb yellow open test2 3f9X5bcAQXKciY56Il5nfA 1 1 0 0 208b 208b yellow open test3 f_-oPx65RwOk846CysvHRQ 1 1 1 0 4.2kb 4.2kb green open .apm-custom-link ZyIwiavFSJSFTbh2cMd8Tg 1 0 0 0 208b 208b green open .apm-agent-configuration FD1qwkXwTTaCT6orvViPRg 1 0 0 0 208b 208b green open .kibana_task_manager_7.12.1_001 MIUHf-m7S_e8XrjZMduTpA 1 0 9 272 286.7kb 286.7kb green open .kibana-event-log-7.12.1-000001 qPBR8ea4Th24CnhfxKa3Sg 1 0 6 0 32.6kb 32.6kb green open .tasks uwcYsws9QkqK1CXKQiNI0Q 1 0 10 0 50kb 50kb

7、基礎測試-修改索引/文檔

(1)直接覆蓋:PUT

PUT /test3/_doc/1 {"name":"小白學es2","age":18,"bithday":"1997-07-07" }

運行結果:version+1,result為updated

{"_index" : "test3","_type" : "_doc","_id" : "1","_version" : 2,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 1,"_primary_term" : 1 }

插件查看結果:

?(2)update更新:POST

POST /test3/_doc/1/_update {"doc":{"name":"小白學es3"} }

運行結果:version+1,result為updated

#! [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead. {"_index" : "test3","_type" : "_doc","_id" : "1","_version" : 3,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 2,"_primary_term" : 1 }

插件查看結果:

8、基礎測試-刪除索引

(1)刪除索引

DELETE test1

查看運行結果:

{"acknowledged" : true }

(2)刪除索引中文檔

DELETE test3/_doc/1

查看運行結果:

{"_index" : "test3","_type" : "_doc","_id" : "1","_version" : 4,"result" : "deleted","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 3,"_primary_term" : 1 }

?

六、文檔CRUD(增刪改查)

1、添加數據:PUT

PUT /xiaobai/user/1 {"name":"小白","age":30,"desc":"一頓操作猛如虎,一看工資2500","tags":["技術宅","直男"] }

查看運行結果:

#! [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}). {"_index" : "xiaobai","_type" : "user","_id" : "1","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1 }

插件查看:

同樣的方法添加一些數據:

2、獲取數據:GET

GET xiaobai/user/1

查看運行結果:

#! [types removal] Specifying types in document get requests is deprecated, use the /{index}/_doc/{id} endpoint instead. {"_index" : "xiaobai","_type" : "user","_id" : "1","_version" : 1,"_seq_no" : 0,"_primary_term" : 1,"found" : true,"_source" : {"name" : "小白","age" : "30","desc" : "一頓操作猛如虎,一看工資2500","tags" : ["技術宅","直男"]} }

3、更新數據:PUT/POST

(1)PUT修改:全量字段更新

1號數據name:小白 -> 白神

PUT /xiaobai/user/1 {"name":"白神","age":"30","desc":"一頓操作猛如虎,一看工資2500","tags":["技術宅","直男"] }

查看運行結果:

#! [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}). {"_index" : "xiaobai","_type" : "user","_id" : "1","_version" : 2,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 5,"_primary_term" : 2 }

(2)POST _update:指定字段更新(推薦使用)

POST /xiaobai/user/1/_update {"doc":{"name":"小白java"} }

查看運行結果:

#! [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead. {"_index" : "xiaobai","_type" : "user","_id" : "1","_version" : 5,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 8,"_primary_term" : 2 }

4、簡單的搜索:GET _searcher

text類型可以根據字段分詞搜索,keyword關鍵詞不會處理分詞器

GET /xiaobai/user/_search?q=name:小白

查看運行結果:

#! [types removal] Specifying types in search requests is deprecated. {"took" : 32,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 1.9252907,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "1","_score" : 1.9252907,"_source" : {"name" : "小白","age" : "30","desc" : "一頓操作猛如虎,一看工資2500","tags" : ["技術宅","直男"]}},{"_index" : "xiaobai","_type" : "user","_id" : "2","_score" : 0.53899646,"_source" : {"name" : "小黑","age" : "32","desc" : "腹黑男豬腳","tags" : ["旅游","渣男","游戲宅"]}},{"_index" : "xiaobai","_type" : "user","_id" : "5","_score" : 0.53899646,"_source" : {"name" : "小王","age" : "32","desc" : "社會性死亡","tags" : ["技術宅","暖男","中年危機"]}}]} }

5、復雜操作搜索:?_searcher match - 分詞模糊匹配

{"query":{"match": {"name": "鉆石王老五"}} }

查看運行結果:

{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 4,"relation": "eq"},"max_score": 3.0311832,"hits": [{"_index": "xiaobai","_type": "user","_id": "6","_score": 3.0311832,"_source": {"name": "鉆石王老五","age": "32","desc": "社會性死亡","tags": ["技術宅","暖男","中年危機"]}},{"_index": "xiaobai","_type": "user","_id": "4","_score": 1.9277248,"_source": {"name": "王五","age": "30","desc": "正宗青子","tags": ["青年","吃貨","旅游","胖子"]}},{"_index": "xiaobai","_type": "user","_id": "7","_score": 1.6832076,"_source": {"name": "鉆石李老四","age": "32","desc": "社會性死亡","tags": ["技術宅","暖男","中年危機"]}},{"_index": "xiaobai","_type": "user","_id": "5","_score": 1.2623059,"_source": {"name": "鉆石123","age": "32","desc": "社會性死亡","tags": ["技術宅","暖男","中年危機"]}}]} }

6、復雜操作搜索:?_searcher match_phrase - 完整模糊匹配

GET /xiaobai/user/_search {"query":{"match_phrase": {"name": "鉆石王老五"}} }

查看運行結果:

#! [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" : 3.0311837,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "6","_score" : 3.0311837,"_source" : {"name" : "鉆石王老五","age" : "32","desc" : "社會性死亡","tags" : ["技術宅","暖男","中年危機"]}}]} }

7、復雜操作搜索:?_searcher term - 完成精確匹配

GET xiaobai/user/_search {"query":{"term": {"name.keyword": "鉆石王老五"}} }

查看運行結果:

#! [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.2039728,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "3","_score" : 1.2039728,"_source" : {"name" : "鉆石王老五","age" : "19","desc" : "社會性死亡","tags" : ["技術宅","暖男"]}}]} }

8、復雜操作搜索:?_searcher _source - 過濾展示結果

GET /xiaobai/user/_search {"query":{"match_phrase": {"name": "鉆石王老五"}},"_source":["name","desc"] }

查看運行結果:

#! [types removal] Specifying types in search requests is deprecated. {"took" : 11,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 3.0311837,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "6","_score" : 3.0311837,"_source" : {"name" : "鉆石王老五","desc" : "社會性死亡"}}]} }

9、復雜操作搜索:?_searcher sort - 排序(desc/asc)

GET xiaobai/user/_search {"query":{"match_phrase": {"name": "鉆石"}},"sort":[{"age":{"order":"desc"}}] }

查看運行結果:

#! [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" : "xiaobai","_type" : "user","_id" : "2","_score" : null,"_source" : {"name" : "鉆石李老四","age" : "32","desc" : "社會性死亡","tags" : ["技術宅","暖男","中年危機"]},"sort" : [32]},{"_index" : "xiaobai","_type" : "user","_id" : "4","_score" : null,"_source" : {"name" : "鉆石123","age" : "28","desc" : "廢物一個","tags" : ["技術宅"]},"sort" : [28]},{"_index" : "xiaobai","_type" : "user","_id" : "3","_score" : null,"_source" : {"name" : "鉆石王老五","age" : "19","desc" : "社會性死亡","tags" : ["技術宅","暖男"]},"sort" : [19]}]} }

10、復雜操作搜索:?_searcher from size - 分頁查詢

GET xiaobai/user/_search {"query":{"match_phrase": {"name": "鉆石"}},"from":0,"size":2 }

查看運行結果:

#! [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" : 0.77691567,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "4","_score" : 0.77691567,"_source" : {"name" : "鉆石123","age" : "28","desc" : "廢物一個","tags" : ["技術宅"]}},{"_index" : "xiaobai","_type" : "user","_id" : "2","_score" : 0.62774795,"_source" : {"name" : "鉆石李老四","age" : "32","desc" : "社會性死亡","tags" : ["技術宅","暖男","中年危機"]}}]} }

11、復雜操作搜索:?_searcher bool - 聯合查詢 -must - 所有條件均符合

GET xiaobai/user/_search {"query":{"bool": {"must":[{"match":{"name":"鉆石王老五"}},{"match":{"age":28}}]}} }

查看運行結果:

#! [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.7769157,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "4","_score" : 1.7769157,"_source" : {"name" : "鉆石123","age" : "28","desc" : "廢物一個","tags" : ["技術宅"]}}]} }

12、復雜操作搜索:_searcher bool - 聯合查詢 - should - 滿足任意一個條件

GET xiaobai/user/_search {"query":{"bool": {"should":[{"match":{"name":"小白"}},{"match":{"age":28}}]}} }

查看運行結果:

#! [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" : 2,"relation" : "eq"},"max_score" : 2.9761126,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "1","_score" : 2.9761126,"_source" : {"name" : "小白","age" : 30,"desc" : "一頓操作猛如虎,一看工資2500","tags" : ["技術宅","直男"]}},{"_index" : "xiaobai","_type" : "user","_id" : "4","_score" : 1.0,"_source" : {"name" : "鉆石123","age" : "28","desc" : "廢物一個","tags" : ["技術宅"]}}]} }

13、復雜操作搜索:_searcher bool - 聯合查詢 - must_not - 不滿足

GET xiaobai/user/_search {"query":{"bool": {"must_not":[{"match":{"name":"小白"}},{"match":{"age":28}}]}} }

查看運行結果:

#! [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" : 2,"relation" : "eq"},"max_score" : 0.0,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "2","_score" : 0.0,"_source" : {"name" : "鉆石李老四","age" : "32","desc" : "社會性死亡","tags" : ["技術宅","暖男","中年危機"]}},{"_index" : "xiaobai","_type" : "user","_id" : "3","_score" : 0.0,"_source" : {"name" : "鉆石王老五","age" : "19","desc" : "社會性死亡","tags" : ["技術宅","暖男"]}}]} }

14、復雜操作搜索:_searcher bool - 聯合查詢 - filter - 過濾

gt(>) gte(>=) lt(<) lte(<=)

GET xiaobai/user/_search {"query":{"bool": {"must":[{"match":{"name":"鉆石"}}],"filter": {"range": {"age": {"gte": 10,"lte": 30}}} }

}
}

查看運行結果:

#! [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" : 2,"relation" : "eq"},"max_score" : 0.77691567,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "4","_score" : 0.77691567,"_source" : {"name" : "鉆石123","age" : "28","desc" : "廢物一個","tags" : ["技術宅"]}},{"_index" : "xiaobai","_type" : "user","_id" : "3","_score" : 0.62774795,"_source" : {"name" : "鉆石王老五","age" : "19","desc" : "社會性死亡","tags" : ["技術宅","暖男"]}}]} }

15、復雜操作搜索:_searcher match -?匹配多個標簽分詞(空格隔開)

GET xiaobai/user/_search {"query":{"match": {"tags":"男 技術宅"}} }

查看運行結果:

#! [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" : 4,"relation" : "eq"},"max_score" : 0.6987428,"hits" : [{"_index" : "xiaobai","_type" : "user","_id" : "1","_score" : 0.6987428,"_source" : {"name" : "小白","age" : 30,"desc" : "一頓操作猛如虎,一看工資2500","tags" : ["技術宅","直男"]}},{"_index" : "xiaobai","_type" : "user","_id" : "3","_score" : 0.6987428,"_source" : {"name" : "鉆石王老五","age" : "19","desc" : "社會性死亡","tags" : ["技術宅","暖男"]}},{"_index" : "xiaobai","_type" : "user","_id" : "2","_score" : 0.5337937,"_source" : {"name" : "鉆石李老四","age" : "32","desc" : "社會性死亡","tags" : ["技術宅","暖男","中年危機"]}},{"_index" : "xiaobai","_type" : "user","_id" : "4","_score" : 0.38828292,

總結

以上是生活随笔為你收集整理的elasticsearch7使用指导的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。