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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

ES的基本操作。

發布時間:2024/3/24 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ES的基本操作。 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一:安裝ES
注意:ES基于JAVA語言研發。環境依賴JDK。

下載路徑:https://www.elastic.co/cn/start
兩個節點:做es集群

ip節點
10.5.100.34node1
10.5.100.102node2

node1節點:

[root@node1 ~]# yum install java-1.8.0-openjdk.x86_64 -y [root@node1 ~]# vim /etc/profile.d/java.sh export JAVA_HOME=/usr [root@node1 ~]# java -version openjdk version "1.8.0_262" OpenJDK Runtime Environment (build 1.8.0_262-b10) OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode) [root@node1 ~]# [root@node1 ~]# yum install elasticsearch-7.9.0-x86_64.rpm -y 下載es的rpm包進行yum安裝 [root@node1 ~]# vim /etc/elasticsearch/elasticsearch.yml 編輯es配置文件。添加兩項 cluster.name: myes 定義的集群名稱,默認是my-application node.name: node1.shuo.com[root@node1 ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.5.100.34 node1 node1.shuo.com [root@node1 ~]# systemctl restart elasticsearch [root@node1 ~]# ss -tnl 默認監聽本機92009300 ES的默認端口; 1,參與集群的事務:9300/tcp transport.tcp.port 2,接受請求:9200/tcphttp.port State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 ::ffff:127.0.0.1:9200 :::* LISTEN 0 128 ::1:9200 :::* LISTEN 0 128 ::ffff:127.0.0.1:9300 :::* LISTEN 0 128 ::1:9300 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@node1 ~]# [root@node1 ~]# systemctl stop firewalld 關閉防火墻設置

Node2節點

[root@node2 ~]# yum install java-1.8.0-openjdk.x86_64 -y [root@node2 ~]# vim /etc/profile.d/java.sh export JAVA_HOME=/usr [root@node2 ~]# java -version openjdk version "1.8.0_262" OpenJDK Runtime Environment (build 1.8.0_262-b10) OpenJDK 64-Bit Server VM (build 25.262-b10, mixed mode) [root@node2 ~]# [root@node2 ~]# yum install elasticsearch-7.9.0-x86_64.rpm -y 下載es的rpm包進行yum安裝 [root@node2 ~]# vim /etc/elasticsearch/elasticsearch.yml 編輯es配置文件。添加兩項 cluster.name: myes 定義的集群名稱,默認是my-application node.name: node2.shuo.com[root@node2 ~]# vim /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.5.100.34 node1 node1.shuo.com [root@node2 ~]# systemctl restart elasticsearch [root@node2 ~]# ss -tnl 默認監聽本機92009300 State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 ::ffff:127.0.0.1:9200 :::* LISTEN 0 128 ::1:9200 :::* LISTEN 0 128 ::ffff:127.0.0.1:9300 :::* LISTEN 0 128 ::1:9300 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* [root@node2 ~]# [root@node2 ~]# systemctl stop firewalld

二:ES的基礎操作。

[root@node1 ~]# curl -X GET 'http://localhost:9200/_cluster/health?pretty' 檢查集群的健康狀態。pretty以優美的格式顯示 {"cluster_name" : "myes", 集群名稱"status" : "green", 集群狀態,green表示正常。"timed_out" : false,"number_of_nodes" : 1,"number_of_data_nodes" : 1,"active_primary_shards" : 0,"active_shards" : 0,"relocating_shards" : 0,"initializing_shards" : 0,"unassigned_shards" : 0,"delayed_unassigned_shards" : 0,"number_of_pending_tasks" : 0,"number_of_in_flight_fetch" : 0,"task_max_waiting_in_queue_millis" : 0,"active_shards_percent_as_number" : 100.0 } [root@node1 ~]# curl -X GET 'http://localhost:9200/_cluster/state/version?pretty' 查看集群的版本,及名稱, {"cluster_name" : "myes","cluster_uuid" : "bGFEa9ZWTraeINTGo4VY3w","version" : 73,"state_uuid" : "GjC8P9gbRJe4erB3_LC0zg" } [root@node1 ~]# [root@node1 ~]# curl -X GET 'http://localhost:9200/_cluster/stats/?pretty' 查看集群的所有狀態信息。 {"_nodes" : {"total" : 1,"successful" : 1,"failed" : 0},"cluster_name" : "myes","cluster_uuid" : "bGFEa9ZWTraeINTGo4VY3w","timestamp" : 1601224533923,"status" : "green","indices" : {"count" : 0,"shards" : { },"docs" : {"count" : 0,"deleted" : 0},[root@node1 ~]#curl -X GET 'http://localhost:9200/_nodes/stats/?pretty' 查看集群個節點的相關信息。

三:ES的插件應用及CRUD。

(1)創建文檔: [root@node1 ~]# curl -H "Content-Type: application/json" -X PUT 'localhost:9200/students/class1/1?pretty' -d ' students:表示索引,class1表示文檔。 id:表示索引,文檔類型。 -H:指明頭部 -d:表示指明文檔。 > { > "first_name": "jing", > "last_name": "Guo", > "gender": "Male", > "age": 25, > "courses": "xianglong shiba zhang" > }' {"_index" : "students","_type" : "class1","_id" : "1","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1 } [root@node1 ~]# (2)獲取文檔: [root@node2 ~]# curl -XGET 'localhost:9200/students/class1/1?pretty' {"_index" : "students","_type" : "class1","_id" : "1","_version" : 1,"_seq_no" : 0,"_primary_term" : 1,"found" : true,"_source" : {"first_name" : "jing","last_name" : "Guo","gender" : "Male","age" : 25,"courses" : "xianglong shiba zhang"} }(3)更新文檔: [root@node1 ~]# curl -H "Content-Type: application/json" -XPOST 'localhost:9200/students/class1/1/_update?pretty' -d' { "doc" : {"age": 22 } }' {"_index" : "students","_type" : "class1","_id" : "1","_version" : 2,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 1,"_primary_term" : 1 } [root@node1 ~]# [root@node1 ~]# curl -XGET 'localhost:9200/students/class1/1?pretty' 查看年齡是否修改 {"_index" : "students","_type" : "class1","_id" : "1","_version" : 2,"_seq_no" : 1,"_primary_term" : 1,"found" : true,"_source" : {"first_name" : "jing","last_name" : "Guo","gender" : "Male","age" : 22,"courses" : "xianglong shiba zhang"} } [root@node1 ~]# [root@node1 ~]# curl -XDELETE 'localhost:9200/students/class1/1?pretty' 刪除文檔類型1 {"_index" : "students","_type" : "class1","_id" : "1","_version" : 3,"result" : "deleted","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 2,"_primary_term" : 1 } [root@node1 ~]# curl -XGET 'localhost:9200/students/class1/1?pretty' {"_index" : "students","_type" : "class1","_id" : "1","found" : false 這里得false表示沒有找到該文檔類型 } [root@node1 ~]# [root@node1 ~]# curl -XGET 'localhost:9200/_cat/indices?v' 查看索引的相關信息 health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open students EJQ-7K5YTgCJlEOwN-01gA 1 1 0 0 228b 228b [root@node1 ~]# [root@node2 ~]# curl -XDELETE 'localhost:9200/students?' 刪除索引,文檔隨之也刪除。 [root@node2 ~]# curl -XGET 'localhost:9200/_cat/indices?v' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size

四:ES的查詢。

ES的查詢階段操作執行分為兩個階段;分散階段:合并階段: [root@node1 ~]# curl -X GET 'localhost:9200/students/_search?pretty' {"took" : 11, 查詢所需時長"timed_out" : false, 查詢是否超時"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : { "total" : { 命中的文檔"value" : 0,"relation" : "eq"},"max_score" : null, 最大命中率"hits" : [ ] 這個hits是個數組,本應該包括命中的文檔,現在為空,說明還沒有命中。} } [root@node1 ~]# [root@node1 ~]# curl -H "Content-Type: application/json" -X GET 'localhost:9200/students/_search?pretty' -d' 另一種查詢方式。如果要查詢某個字段的 要在serarch后面指明查詢條件。 > { > "query": { "match_all": {} } > }' {"took" : 292,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 0,"relation" : "eq"},"max_score" : null,"hits" : [ ]} } 多索引,多類型查詢; /_search:查詢所有索引的所有文檔、 /index_name/_search:單索引 /index1,index2/_search:多索引; /s* ,t*/_search: /students/class1/_search:單類型搜索 /students/class1,class2/_search:多類型搜索Mapping和AnalysisES:對每一個文檔,會取得其所有域的所有值,生成一個名為all的域:執行查詢時,如果在query_string未指定查詢的域,則在_all域上執行查詢操作。GET /_search?q='xianglong'GET /_search?q='xianglong%20shiba%20zhang' %都是精確匹配。GET /_search?q=courses: 'xianglong%20shiba%20zhang'GET /_search?q=courses: 'xianglong'前兩個:表示在all域上搜索; 后兩個:在指定的域上搜索:

四:ES內置的分析器

ES中搜索的數據廣義上可被理解為兩類。 type:exact full-text:用于引用文本中數據,判斷文檔在多大程序上匹配查詢請求,即評估文檔與用戶請求查詢的相關度; 為了完成full-text搜索;es必須首先分析文本,并創建出倒排索引,倒排索引中的數據還需進行正規化為標準格式 分詞,正規化,即分析分析需要由分析器進行:analyzer 分析器由三部分組件構成:字符過濾器,分詞器,分詞過濾器ES內置的分析器: standard analyzer: 標準分析器。 simple analyzer language analyzer分析器不僅在創建索引時用到;在構建查詢時也會用到。request body: 分成兩類: query dsl:執行full-text查詢時,基于相關度來評判匹配結果;查詢執行過程復雜,且不會被緩存。 filter dsl:執行exact查詢時,基于其結果為 "yes""no" 進行評判;速度快,且結果緩存。filter dsl實例: [root@node1 ~]# curl -H "Content-Type: application/json" -XGET 'localhost:9200/students/_search?pretty' -d' term表示單項查找。 {"query": {"term": {"age": 23 }} }' {"took" : 49,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "students","_type" : "class1","_id" : "3","_score" : 1.0,"_source" : {"first_name" : "Rong","last_name" : "huang","gender" : "female","age" : 23,"courses" : "luoying shenjian"}}]} }term filter:用于多值精確匹配; terms表示多項查找{ "terms": { "name" : ["guo", "rong"] }[root@node2 ~]# curl -H "Content-Type: application/json" -X GET 'localhost:9200/students/_search?pretty' -d' {"query": {"terms": {"first_name": ["Rong","guo"]}} }' {"took" : 4,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 0,"relation" : "eq"},"max_score" : null,"hits" : [ ]} } [root@node2 ~]# query dsl:match_all query:用于匹配所有文檔,沒有指定任何query,默認即為match_all query{ "match_all": {} } match query: 在幾乎任何域上執行full-text或exact-value查詢;如果執行full-text查詢;首先對查詢時的語句做分析;{ "match": {"students": "Guo" }}如果執行exact-value查詢;搜索精確值;此時,建議使用過濾,而非查詢multi_match query:用于在多個域上執行相同的查詢;{ "multi_match":"query": full-text serach"field": {'field1', 'field2' }}{ "multi_match": 這表示多域進行查詢,表示在name和描述字段查找guo"query": {"students": "guo"}"field":{"name","description"}} bool query:基于boolean邏輯合并多個查詢語句;與boolfilter不同的是,查詢字句不是返回yes 或 no 而是其計算出的匹配度分值,因此,query會為各子句合并其score查詢語句語法檢查: GET /index/_validate/query?explain&pretty {..... }

總結

以上是生活随笔為你收集整理的ES的基本操作。的全部內容,希望文章能夠幫你解決所遇到的問題。

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