适用于Java开发人员的Elasticsearch:命令行中的Elasticsearch
本文是我們學院課程的一部分,該課程的標題為Java開發人員的Elasticsearch教程 。
在本課程中,我們提供了一系列教程,以便您可以開發自己的基于Elasticsearch的應用程序。 我們涵蓋了從安裝和操作到Java API集成和報告的廣泛主題。 通過我們簡單易懂的教程,您將能夠在最短的時間內啟動并運行自己的項目。 在這里查看 !
1.簡介
通過本教程的前一部分,我們對Elasticsearch是什么,它的基本概念以及它可以帶給我們應用程序的搜索功能的功能有了很好的了解。 在本節中,我們將直接進入戰斗,并在實踐中運用我們的知識。 在本節中, curl和/或http將是我們將用來與Elasticsearch交朋友的唯一工具。
目錄
1.簡介 2.我的集群健康嗎? 3.關于指數的一切 4.文件,更多文件,… 5.如果我的映射類型不理想怎么辦 6.搜索時間 7.按查詢突變 8.更好地了解您的查詢 9.從搜索到見解 10.觀看集群呼吸 11.結論 12.接下來是什么綜上所述,我們已經完成了圖書catalog索引和映射類型的確定,因此我們將從此處進行選擇。 為了使事情盡可能接近現實,我們將使用三個節點的Elasticsearch集群(均作為Docker容器運行),而catalog索引將配置為兩個復制因子。
正如我們將要看到的,與獨立實例相比,使用Elasticsearch集群有很多微妙之處,最好準備好應對它們。 希望您仍然記得本教程的上一部分,如何啟動Elasticsearch,因為這將是唯一的先決條件:啟動并運行集群。 這樣,讓我們??開始吧!
2.我的集群健康嗎?
在對Elasticsearch集群進行任何處理之前,您需要了解的第一件事是其運行狀況。 有兩種收集這些信息的方法,但是可以說,最簡單,最方便的方法是使用群集API ,尤其是群集運行狀況端點 。
$ http http://localhost:9200/_cluster/healthHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"active_primary_shards": 0,"active_shards": 0,"active_shards_percent_as_number": 100.0,"cluster_name": "es-catalog","delayed_unassigned_shards": 0,"initializing_shards": 0,"number_of_data_nodes": 3,"number_of_in_flight_fetch": 0,"number_of_nodes": 3,"number_of_pending_tasks": 0,"relocating_shards": 0,"status": "green","task_max_waiting_in_queue_millis": 0,"timed_out": false,"unassigned_shards": 0 }在這些細節中,我們正在尋找應該設置為green status指示器,這意味著所有分片均已分配并且群集處于良好的運行狀態。
3.關于指數的一切
我們的Elasticsearch集群全是綠色的,可以搖擺不定 。 下一步的邏輯步驟是創建一個catalog索引,其中包含我們之前概述的映射類型和設置。 但是在此之前,讓我們檢查這次是否已經使用Indices API 創建了任何索引 。
$ http http://localhost:9200/_statsHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"_all": {"primaries": {},"total": {}},"_shards": {"failed": 0,"successful": 0,"total": 0},"indices": {} }正如預期的那樣,我們的集群尚無任何內容,因此我們很樂意為圖書目錄創建索引。 眾所周知, Elasticsearch說的是JSON,但要說的是從命令行使用或多或少復雜的JSON文檔,這有點麻煩。 讓我們更好地將catalog設置和映射存儲在catalog-index.json文檔中。
{ "settings": {"index" : {"number_of_shards" : 5, "number_of_replicas" : 2 }},"mappings": {"books": {"_source" : {"enabled": true},"properties": {"title": { "type": "text" },"categories" : {"type": "nested","properties" : {"name": { "type": "text" }}},"publisher": { "type": "keyword" },"description": { "type": "text" },"published_date": { "type": "date" },"isbn": { "type": "keyword" },"rating": { "type": "byte" }}},"authors": {"properties": {"first_name": { "type": "keyword" },"last_name": { "type": "keyword" }},"_parent": {"type": "books"}}} }并將此文檔用作創建索引API的輸入。
$ http PUT http://localhost:9200/catalog < catalog-index.jsonHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"acknowledged": true,"shards_acknowledged": true }對于大多數Elasticsearch API(尤其是應用突變的API)中acknowledged響應屬性的用法,應該說幾句話。 通常,此值僅表示操作是在超時之前完成( “true” )還是可能在不久的將來生效( “false” )。 稍后,我們將在不同的上下文中看到其用法的更多示例。
就是這樣,我們已經使catalog索引生效。 為了確保這一事實的真實性,我們可以要求Elasticsearch返回catalog 索引設置 。
$ http http://localhost:9200/catalog/_settingsHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"catalog": {"settings": {"index": {"creation_date": "1487428863824","number_of_replicas": "2","number_of_shards": "5","provided_name": "catalog","uuid": "-b63dCesROC5UawbHz8IYw","version": {"created": "5020099"}}}} }太好了,正是我們訂購的東西。 您可能想知道,如果我們嘗試通過增加分片的數量來更新索引設置 , Elasticsearch會如何反應(眾所周知,創建索引后,并非所有索引設置都可以更新)。
$ echo '{"index":{"number_of_shards":6}}' | http PUT http://localhost:9200/catalog/_settingsHTTP/1.1 400 Bad Request content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"error": {"reason": "can't change the number of shards for an index","root_cause": [...],"type": "illegal_argument_exception"},"status": 400 }錯誤響應不足為奇(請注意,已減少響應詳細信息僅出于說明目的)。 連同設置,很容易獲得特定索引的映射類型 ,例如:
$ http http://192.168.99.100:9200/catalog/_mappingHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"catalog": {"mappings": {"authors": {...},"books": {...}}} }總體而言,現有字段的索引映射無法更新; 但是該規則有一些例外 。 索引API的最大功能之一是能夠針對特定的索引映射類型和字段執行分析過程 ,而無需實際發送任何文檔。
$ http http://localhost:9200/catalog/_analyze field=books.title text="Elasticsearch: The Definitive Guide. A Distributed Real-Time Search and Analytics Engine"HTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"tokens": [{"end_offset": 13,"position": 0,"start_offset": 0,"token": "elasticsearch","type": ""},{"end_offset": 18,"position": 1,"start_offset": 15,"token": "the","type": ""},...{"end_offset": 88,"position": 11,"start_offset": 82,"token": "engine","type": ""}] }萬一您想在將大量數據投入Elasticsearch進行索引之前驗證映射類型的參數,此功能特別有用。
最后但并非最不重要的一點是,有關索引狀態的一個重要細節。 任何特定的索引都可以處于opened (完全可操作)或closed (阻塞以進行讀/寫操作,已歸檔將是一個很好的類比)狀態。 至于其他所有內容, Elasticsearch 為此提供了一個API 。
$ http POST http://localhost:9200/catalog/_openHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"acknowledged": true }4.文件,更多文件,…
沒有文檔的空索引不是很有用,因此讓我們將索引API的齒輪切換到另一個出色的文檔API上 。 我們將開始使用最簡單的單個文檔操作來探索它,它依賴于以下book.json文檔:
{"title": "Elasticsearch: The Definitive Guide. A Distributed Real-Time Search and Analytics Engine","categories": [{ "name": "analytics" },{ "name": "search" },{ "name": "database store" }],"publisher": "O'Reilly","description": "Whether you need full-text search or real-time analytics of structured data—or both—the Elasticsearch distributed search engine is an ideal way to put your data to work. This practical guide not only shows you how to search, analyze, and explore data with Elasticsearch, but also helps you deal with the complexities of human language, geolocation, and relationships.", "published_date": "2015-02-07","isbn": "978-1449358549","rating": 4 }在將此JSON發送到Elasticsearch之前,最好先討論一下文檔標識。 Elasticsearch中的每個文檔都有一個唯一的標識符,該標識符存儲在特殊的_id字段中。 您可以在將文檔上傳到Elasticsearch時提供一個(就像我們在下面的示例中使用isbn一樣,因為它是自然標識符的一個很好的例子),否則它會由Elasticsearch生成并分配。
$ http PUT http://localhost:9200/catalog/books/978-1449358549 < book.jsonHTTP/1.1 201 Created Location: /catalog/books/978-1449358549 content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"_id": "978-1449358549","_index": "catalog","_shards": {"failed": 0,"successful": 3,"total": 3},"_type": "books","_version": 1,"created": true,"result": "created" }我們的第一個文檔進入了books類型下的catalog索引。 但是,我們也有authors類型,與books之間存在父/子關系。 讓我們用authors.json文檔中的作者來補充這本書。
[{"first_name": "Clinton","last_name": "Gormley","_parent": "978-1449358549"},{"first_name": "Zachary","last_name": "Tong","_parent": "978-1449358549"} ]這本書有一位以上的作者,因此我們仍然可以通過逐個索引每個作者文檔來使用單個文檔API 。 但是,讓我們不要這樣做,而是切換到批量文檔API,然后將我們的authors.json文檔轉換為與批量文檔API格式兼容。
{ "index" : { "_index" : "catalog", "_type" : "authors", "_id": "1", "_parent": "978-1449358549" } } { "first_name": "Clinton", "last_name": "Gormley" } { "index" : { "_index" : "catalog", "_type" : "authors", "_id": "2", "_parent": "978-1449358549" } } { "first_name": "Zachary", "last_name": "Tong" }完成后,讓我們將該文檔另存為authors-bulk.json,并將其直接輸入到批量文檔API端點中。
$ http POST http://localhost:9200/_bulk < authors-bulk.jsonHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"errors": false,"items": [{"index": {"_id": "1","_index": "catalog","_shards": {"failed": 0,"successful": 3,"total": 3},"_type": "authors","_version": 5,"created": false,"result": "updated","status": 200}},{"index": {"_id": "2","_index": "catalog","_shards": {"failed": 0,"successful": 3,"total": 3},"_type": "authors","_version": 2,"created": true,"result": "created","status": 201}}],"took": 105 }而且,我們擁有書籍和作者文檔,是catalog索引的第一批公民! 現在是時候取回這些文件了。
$ http http://localhost:9200/catalog/books/978-1449358549HTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"_id": "978-1449358549","_index": "catalog","_source": {"categories": [{ "name": "analytics" },{ "name": "search"},{ "name": "database store" }],"description": "...","isbn": "978-1449358549","published_date": "2015-02-07","publisher": "O'Reilly","rating": 4,"title": "Elasticsearch: The Definitive Guide. A Distributed Real-Time Search and Analytics Engine"},"_type": "books","_version": 1,"found": true }簡單! 但是,要從authors集合中獲取文檔(它們是books集合中各自文檔的子代),我們必須提供父標識符以及該文檔自己的標識符,例如:
$ http http://localhost:9200/catalog/authors/1?parent=978-1449358549HTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"_id": "1","_index": "catalog","_parent": "978-1449358549","_routing": "978-1449358549","_source": {"first_name": "Clinton","last_name": "Gormley"},"_type": "authors","_version": 1,"found": true }這是在Elasticsearch中處理父子關系的細節之一。 正如已經提到的那樣,您可以以更簡單的方式對這種關系進行建模,但是如果您選擇在應用程序中采用這種方式,我們的目標是學習如何處理這種關系。
刪除和更新 API非常簡單,因此我們只介紹它們,請注意,適用于標識子文檔的相同規則。 您可能會感到驚訝,但是刪除父文檔并不會自動刪除其子文檔,因此請記住這一點。 稍后我們將看到如何解決該問題。
最后,讓我們看一下術語vectors API ,例如,該函數返回有關文檔字段中術語的所有詳細信息和統計信息(僅粘貼了響應的一小部分):
$ http http://localhost:9200/catalog/books/978-1449358549/_termvectors?fields=descriptionHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"_id": "978-1449358549","_index": "catalog","_type": "books","_version": 1,"found": true,"term_vectors": {"description": {"field_statistics": {"doc_count": 1,"sum_doc_freq": 46,"sum_ttf": 60},"terms": {"analyze": {"term_freq": 1,"tokens": [ ... ]},"and": {"term_freq": 2,"tokens": [ ... ]},"complexities": {"term_freq": 1,"tokens": [ ... ]},"data": {"term_freq": 3,"tokens": [ ... ]},...}}},"took": 5 }您可能不會經常使用術語vectors API,但是它是解決某些文檔為何未在搜索結果中彈出的絕佳工具。
5.如果我的映射類型不理想怎么辦
隨著時間的流逝,您可能經常發現映射類型可能不是最佳的,而是可能會變得更好。 但是, Elasticsearch僅支持對現有映射類型的有限修改。 幸運的是, Elasticsearch提供了專用的重新索引API ,例如:
$ echo '{"source": {"index": "catalog"}, "dest": {"index": "catalog-v2"}}' | http POST http://localhost:9200/_reindexHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"batches": 0,"created": 200,"deleted": 0,"failures": [],"noops": 0,"requests_per_second": -1.0,"retries": {"bulk": 0,"search": 0},"throttled_millis": 0,"throttled_until_millis": 0,"timed_out": false,"took": 265,"total": 200,"updated": 0,"version_conflicts": 0 }這里的竅門是創建一個具有更新的映射類型catalog-v2的新索引,而不是僅讓Elasticsearch從舊索引( catalog )中獲取所有文檔,然后將它們放入新索引( catalog-v2 ),最后交換索引。 請注意,它不僅適用于本地索引 ,還適用于遠程索引 。
盡管很簡單,但該API仍被認為是實驗性的,可能并不適合所有情況,例如,如果您的索引確實很大,或者您的Elasticsearch負載很大,并且應該優先處理應用程序請求。
6.搜索時間
我們已經學習了如何創建索引,映射類型和為文檔建立索引,這些都是重要但并非真正令人興奮的主題。 但是搜索絕對是Elasticsearch的心臟和靈魂,因此讓我們立即了解它。
為了演示不同的搜索功能,我們將需要更多文檔,請使用我們的好友批量文檔API將它們從books-and-authors-bulk.json上傳到您的Elasticsearch集群中。
$ http POST http://localhost:9200/_bulk < books-and-authors-bulk.json我們的收藏集中有一些文檔,我們可以開始使用最易訪問的搜索API形式對它們發出搜索查詢,該搜索API通過查詢字符串接受URI中的搜索條件。 例如,讓我們搜索術語engine (記住search engine短語)。
$ http POST http://localhost:9200/catalog/books/_search?q=engineHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"_shards": {"failed": 0,"successful": 5,"total": 5},"hits": {"hits": [{"_id": "978-1449358549","_index": "catalog","_score": 0.7503276,"_source": {"categories": [{ "name": "analytics },{ "name": "search" },{ "name": "database store" }],"description": " Whether you need full-text search or real-time ...","isbn": "978-1449358549","published_date": "2015-02-07","publisher": "O'Reilly","rating": 4,"title": " Elasticsearch: The Definitive Guide. ..."},"_type": "books"}],"max_score": 0.7503276,"total": 1},"timed_out": false,"took": 22 }確實,這是一個很好的起點,該API對于執行快速和淺層搜索非常有用,但是其功能非常有限。 使用請求正文API進行搜索是一種完全不同的野獸,它揭示了Elasticsearch的全部功能。 它建立在基于JSON的查詢DSL之上, 后者是簡潔而直觀的語言,可構造任意復雜的搜索查詢。
Query DSL允許描述很多查詢類型,每種都有自己的語法和參數。 但是,有一組通用參數,例如sort , from , size , stored_fields (實際上該列表確實很長 ),它們與查詢類型無關,并且可以應用于任何這些參數。
在接下來的幾節中,我們將從http切換到curl,因為在處理JSON負載時后者更加方便。
我們將使用Query DSL嘗試的第一種查詢類型是match all查詢 。 在某種程度上,它并不是真正的查詢,因為它只匹配所有文檔。 因此,它可能會返回很多結果,通常,請始終以合理的大小限制為查詢添加注釋,下面是一個示例:
$ curl –i http://localhost:9200/catalog/books/_search?pretty -d ' {"size": 10,"query": {"match_all" : {}} }'HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 3112 {"took" : 13,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 3,"max_score" : 1.0,"hits" : [{"_index" : "catalog","_type" : "books","_id" : "978-1449358549","_score" : 1.0,"_source" : {"title" : "Elasticsearch: The Definitive Guide ...","categories" : [{ "name" : "analytics" },{ "name" : "search" },{ "name" : "database store" }],"publisher" : "O'Reilly","description" : "Whether you need full-text ...","published_date" : "2015-02-07","isbn" : "978-1449358549","rating" : 4}},...]} }下一個是真實查詢類型,稱為一類全文查詢 ,它針對全文文檔字段(可能是使用最廣泛的字段)進行搜索。 它以基本形式針對單個文檔字段進行匹配,例如書的description 。
$ curl -i http://localhost:9200/catalog/books/_search?pretty -d ' {"query": {"match" : {"description" : "engine"}} }'HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 1271 {"took" : 17,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 1,"max_score" : 0.28004453,"hits" : [{"_index" : "catalog","_type" : "books","_id" : "978-1449358549","_score" : 0.28004453,"_source" : {"title" : "Elasticsearch: The Definitive Guide. ...","categories" : [{ "name" : "analytics" },{ "name" : "search" },{ "name" : "database store" }],"publisher" : "O'Reilly","description" : "Whether you need full-text ...","published_date" : "2015-02-07","isbn" : "978-1449358549","rating" : 4}}]} }但是, 全文查詢是非常強大的,有不少其他的變化,包括match_phrase , match_phrase_prefix , multi_match , common_terms , QUERY_STRING和simple_query_string 。
繼續前進,我們進入了術語級查詢的世界,這些術語按確切的術語進行操作,通常用于數字,日期和關鍵字等字段類型。 publisher圖書領域是嘗試的不錯選擇。
$ curl -i http://localhost:9200/catalog/books/_search?pretty -d ' {"size": 10,"_source": [ "title" ],"query": {"term" : {"publisher" : "Manning"}} }' HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 675{"took" : 21,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 2,"max_score" : 0.18232156,"hits" : [{"_index" : "catalog","_type" : "books","_id" : "978-1617291623","_score" : 0.18232156,"_source" : {"title" : "Elasticsearch in Action"}},{"_index" : "catalog","_type" : "books","_id" : "978-1617292774","_score" : 0.18232156,"_source" : {"title" : "Relevant Search: With applications ..."}}]} }請注意,我們如何限制文檔_source的屬性以僅返回title字段。 術語級別查詢的其他變體包括術語 , 范圍 , 存在 , 前綴 , 通配符 ,正則表達式 , 模糊 , 類型和ID 。
在我們的書catalog索引中, 連接查詢是非常有趣的查詢 。 這些查詢允許對具有父/子關系的嵌套對象或文檔執行搜索。 例如,讓我們找出analytics類別中的所有書籍。
$ curl -i http://localhost:9200/catalog/books/_search?pretty -d ' {"size": 10,"_source": [ "title", "categories" ],"query": {"nested": {"path": "categories","query" : {"match": {"categories.name" : "analytics"}}}} }'HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 1177{"took" : 45,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 2,"max_score" : 1.3112576,"hits" : [{"_index" : "catalog","_type" : "books","_id" : "978-1617291623","_score" : 1.3112576,"_source" : {"categories" : [{ "name" : "analytics" },{ "name" : "search" },{ "name" : "database store" }],"title" : "Elasticsearch in Action"}},{"_index" : "catalog","_type" : "books","_id" : "978-1449358549","_score" : 1.0925692,"_source" : {"categories" : [{ "name" : "analytics" },{ "name" : "search" },{ "name" : "database store" }],"title" : "Elasticsearch: The Definitive Guide ..."}}]} }同樣,我們可以搜索克林頓·戈姆利 ( Clinton Gormley)創作的所有書籍,從而利用books和authors集合之間的父子關系。
$ curl -i http://localhost:9200/catalog/books/_search?pretty -d ' {"size": 10,"_source": [ "title" ],"query": {"has_child" : {"type" : "authors","inner_hits" : {"size": 5},"query" : {"term" : {"last_name" : "Gormley"}}}} }'HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 1084{"took" : 38,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 1,"max_score" : 1.0,"hits" : [{"_index" : "catalog","_type" : "books","_id" : "978-1449358549","_score" : 1.0,"_source" : {"title" : "Elasticsearch: The Definitive Guide ..."},"inner_hits" : {"authors" : {"hits" : {"total" : 1,"max_score" : 0.6931472,"hits" : [{"_type" : "authors","_id" : "1","_score" : 0.6931472,"_routing" : "978-1449358549","_parent" : "978-1449358549","_source" : {"first_name" : "Clinton","last_name" : "Gormley"}}]}}}}]} }請注意inner_hits查詢參數的存在, 該參數使搜索結果包括與聯接條件匹配的內部文檔。
其他查詢類型(例如地理查詢 , 專用查詢和跨度查詢)的工作方式非常相似,因此我們將跳過它們并通過研究復合查詢來完成 。 到目前為止,我們看到的示例僅包含具有一種搜索條件的查詢,但是Query DSL也具有構造復合查詢的方式。 讓我們看一下使用布爾查詢的示例,它是我們已經看到的一些查詢類型的組成。
$ curl -i http://localhost:9200/catalog/books/_search?pretty -d ' {"size": 10,"_source": [ "title", "publisher" ],"query": {"bool" : {"must" : [{"range" : {"rating" : { "gte" : 4 }}},{"has_child" : {"type" : "authors","query" : {"term" : {"last_name" : "Gormley"}}}},{"nested": {"path": "categories","query" : {"match": {"categories.name" : "search"}}}}]}} }'HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 531{"took" : 79,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 1,"max_score" : 3.0925694,"hits" : [{"_index" : "catalog","_type" : "books","_id" : "978-1449358549","_score" : 3.0925694,"_source" : {"publisher" : "O'Reilly","title" : "Elasticsearch: The Definitive Guide. ..."}}]} }可以公平地說,由Query DSL支持的Elasticsearch的搜索API非常靈活,易于使用且具有表達力。 更重要的是,值得一提的是,除查詢外, 搜索API還支持過濾器的概念,該過濾器提供了另一種從搜索結果中排除文檔的選項。
7.按查詢突變
出乎意料的是(或沒有), Elasticsearch可以使用查詢對索引中的文檔執行諸如更新或刪除之類的突變。 例如,以下代碼段將刪除Manning出版的我們目錄中所有評分較低的圖書。
$ curl -i http://localhost:9200/catalog/books/_delete_by_query?pretty -d ' {"query": {"bool": {"must": [{ "range" : { "rating" : { "lt" : 3 } } }],"filter": [{ "term" : { "publisher" : "Manning" } }]}} }'HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 296{"took" : 12,"timed_out" : false,"total" : 0,"deleted" : 0,"batches" : 0,"version_conflicts" : 0,"noops" : 0,"retries" : {"bulk" : 0,"search" : 0},"throttled_millis" : 0,"requests_per_second" : -1.0,"throttled_until_millis" : 0,"failures" : [ ] }它使用相同的查詢DSL,并且出于說明如何使用過濾的目的,將過濾filter作為查詢的一部分。 但是,不返回匹配的文檔,而是應用更新或刪除修改。
查詢刪除API可用于克服常規刪除API的局限性,并在子文檔的父文檔被刪除的情況下刪除子文檔。
8.更好地了解您的查詢
有時,您可能會發現搜索查詢正在按您不期望的順序返回文檔,從而使某些文檔的排名高于其他文檔。 為了幫助您, Elasticsearch提供了兩個非常有用的API。 其中之一是explain API ,它計算查詢的分數說明(如果需要,還可以計算特定文檔)。
可以通過將explain參數指定為查詢的一部分來接收該explain :
$ curl -i http://localhost:9200/catalog/books/_search?pretty -d ' {"size": 10,"explain": true,"query": {"term" : {"publisher" : "Manning"}} }或使用專用的說明API端點和特定文檔,例如:
$ curl -i http://localhost:9200/catalog/books/978-1617292774/_explain?pretty -d ' {"query": {"term" : {"publisher" : "Manning"}} }'由于返回了大量有用的詳細信息,因此未有意將答復包括在內。 Elasticsearch的另一個非常有用的功能是驗證API ,該API允許在不實際執行查詢的情況下執行查詢的驗證,例如:
$ curl -i http://localhost:9200/catalog/books/_validate/query?pretty -d ' {"query": {"term" : {"publisher" : "Manning"}} }'HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 98{"valid" : true,"_shards" : {"total" : 1,"successful" : 1,"failed" : 0} }這兩種API都非常有用,可以解決相關性或分析潛在影響搜索查詢,而無需在實時Elasticsearch集群上執行它。
9.從搜索到見解
通常,您可能會發現自己處在搜索不足的情況下,您需要在匹配項之上進行某種匯總。 很好的例子是構面(或如Elasticsearch所說的術語聚合 ),其中搜索結果被分組到存儲桶中。
$ curl -i http://localhost:9200/catalog/books/_search?pretty -d ' {"query": {"match" : {"description" : "elasticsearch"}},"aggs" : {"publisher" : {"terms" : { "field" : "publisher" }}} }'HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 3447{"took" : 176,"timed_out" : false,"_shards" : {"total" : 5,"successful" : 5,"failed" : 0},"hits" : {"total" : 3,"max_score" : 0.38828257,"hits" : [{...}]},"aggregations" : {"publisher" : {"doc_count_error_upper_bound" : 0,"sum_other_doc_count" : 0,"buckets" : [{"key" : "Manning","doc_count" : 2},{"key" : "O'Reilly","doc_count" : 1}]}} }在此示例中,連同搜索查詢,我們已要求Elasticsearch按發布者對文檔進行計數。 總的來說,搜索查詢可以完全省略,并且僅聚合可以在請求正文中發送,例如:
$ curl -i http://localhost:9200/catalog/books/_search?pretty -d ' {"aggs" : {"authors": {"children": {"type" : "authors"},"aggs": {"top-authors": {"terms": {"script" : {"inline": "doc['first_name'].value + ' ' + doc['last_name'].value","lang": "painless"},"size": 10}}}}} }'HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 content-length: 1031 {"took": 381,"timed_out": false,"_shards": {"total": 5,"successful": 5,"failed": 0},"hits": {"total": 5,"max_score": 1,"hits": [...]},"aggregations": {"authors": {"doc_count": 6,"top-authors": {"doc_count_error_upper_bound": 0,"sum_other_doc_count": 0,"buckets": [{"key": "Clinton Gormley","doc_count": 1},{"key": "Doug Turnbull","doc_count": 1},{"key": "Matthew Lee Hinman","doc_count": 1},{"key": "Radu Gheorghe","doc_count": 1},{"key": "Roy Russo","doc_count": 1},{"key": "Zachary Tong","doc_count": 1}]}}} }在這個稍微復雜一些的示例中,我們使用Elasticsearch腳本支持將頂級作者分類,以用作者的名字和姓氏組成術語:
"script" : { "inline": "doc['first_name'].value + ' ' + doc['last_name'].value", "lang": "painless" }支持的聚合列表確實令人印象深刻,其中包括存儲桶聚合 (我們已經嘗試了其中的一些聚合 ), 指標聚合 , 管道聚合和矩陣聚合 。 僅涵蓋其中一門課程的人將需要自己的教程,因此請仔細閱讀它們以深入了解每一門課程的目的。
10.觀看集群呼吸
Elasticsearch集群是生命中的“生物”,應該對其進行密切監視和監視,以便主動發現任何問題并Swift做出反應。 我們之前見過的集群運行狀況端點是獲取集群總體高級狀態的最簡單方法。
$ http http://localhost:9200/_cluster/healthHTTP/1.1 200 OK content-encoding: gzip content-type: application/json; charset=UTF-8 transfer-encoding: chunked{"active_primary_shards": 5,"active_shards": 5,"active_shards_percent_as_number": 20.0,"cluster_name": "es-catalog","delayed_unassigned_shards": 0,"initializing_shards": 0,"number_of_data_nodes": 1,"number_of_in_flight_fetch": 0,"number_of_nodes": 1,"number_of_pending_tasks": 0,"relocating_shards": 0,"status": "red","task_max_waiting_in_queue_millis": 0,"timed_out": false,"unassigned_shards": 20 }如果群集red (如上述示例中所示),則肯定存在要解決的問題。 為了幫助您, Elasticsearch提供了集群統計信息API , 集群狀態API , 集群節點級別統計信息API和集群節點索引統計信息API 。
除了一點之外,還有另一組非常重要的API,即cat API 。 從某種意義上說,表示形式不是JSON ,而是基于文本,具有緊湊且對齊的輸出,適用于終端,它們在某種意義上是不同的。
11.結論
在本教程的這一部分中,我們通過僅使用命令行工具的RESTful API探索了Elasticsearch的許多功能。 總的來說,這只是Elasticsearch通過API提供的功能的一小部分,而且官方文檔是學習它們的好地方。 希望在這一點上,我們對Elasticsearch足夠滿意,并且知道如何使用它。
12.接下來是什么
在本教程的下一部分中,我們將學習Elasticsearch必須提供給Java / JVM開發人員的多種本地API。 這些API是任何利用Elasticsearch功能的Java / JVM應用程序的基本構建塊。
翻譯自: https://www.javacodegeeks.com/2017/02/elasticsearch-java-developers-elasticsearch-command-line.html
總結
以上是生活随笔為你收集整理的适用于Java开发人员的Elasticsearch:命令行中的Elasticsearch的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jndi ldap_什么是JNDI,SP
- 下一篇: 拼多多怎么撤回评价