本文介紹在Kibana中對Elasticsearch Serverless應用中的數據進行全文檢索、多語言檢索和地理位置查詢。
背景信息
Elasticsearch Serverless服務是圍繞Elasticsearch打造的云原生Serverless服務化產品,用戶無需管理集群和配置資源即可實現業務負載與資源動態匹配。支持彈性擴縮,在業務高峰期也能滿足需求。保留用戶原有使用習慣,支持平滑切換和使用,助力業務快速上云。
準備工作
全文檢索
Elasticsearch Serverless服務是一個基于Lucene的實時的分布式搜索和分析引擎,用于云計算中能夠實現實時、穩定、可靠、快速的搜索和分析。
創建product_info索引并插入數據。
創建索引。
PUT /product_info { "mappings": { "properties": { "productName": { "type": "text", "analyzer": "ik_smart" }, "annual_rate":{ "type":"keyword" }, "describe": { "type": "text", "analyzer": "ik_smart" } } } }
使用bulk方式在索引中插入數據。
POST /product_info/_doc/_bulk {"index":{}} {"productName":"理財產品A","annual_rate":"3.2200%","describe":"180天定期理財,最低20000起投,收益穩定,可以自助選擇消息推送"} {"index":{}} {"productName":"理財產品B","annual_rate":"3.1100%","describe":"90天定投產品,最低10000起投,每天收益到賬消息推送"} {"index":{}} {"productName":"理財產品C","annual_rate":"3.3500%","describe":"270天定投產品,最低40000起投,每天收益立即到賬消息推送"} {"index":{}} {"productName":"理財產品D","annual_rate":"3.1200%","describe":"90天定投產品,最低12000起投,每天收益到賬消息推送"} {"index":{}} {"productName":"理財產品E","annual_rate":"3.0100%","describe":"30天定投產品推薦,最低8000起投,每天收益會消息推送"} {"index":{}} {"productName":"理財產品F","annual_rate":"2.7500%","describe":"熱門短期產品,3天短期,無須任何手續費用,最低500起投,通過短信提示獲取收益消息"}
查詢數據
搜索產品信息文檔中包含
每天收益到賬消息推送
的所有產品。GET /product_info/_doc/_search { "query": { "match": { "describe": "每天收益到賬消息推送" } } }
返回結果如下:
{ "took" : 8, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 6, "relation" : "eq" }, "max_score" : 1.87463, "hits" : [ { "_index" : "product_info", "_type" : "_doc", "_id" : "mEWajIoBaH-7DHixr9am", "_score" : 1.87463, "_source" : { "productName" : "理財產品E", "annual_rate" : "3.0100%", "describe" : "30天定投產品推薦,最低8000起投,每天收益會消息推送" } }, { "_index" : "product_info", "_type" : "_doc", "_id" : "lUWajIoBaH-7DHixr9am", "_score" : 1.8293141, "_source" : { "productName" : "理財產品B", "annual_rate" : "3.1100%", "describe" : "90天定投產品,最低10000起投,每天收益到賬消息推送" } }, { "_index" : "product_info", "_type" : "_doc", "_id" : "lkWajIoBaH-7DHixr9am", "_score" : 1.7743123, "_source" : { "productName" : "理財產品C", "annual_rate" : "3.3500%", "describe" : "270天定投產品,最低40000起投,每天收益立即到賬消息推送" } }, { "_index" : "product_info", "_type" : "_doc", "_id" : "l0WajIoBaH-7DHixr9am", "_score" : 1.7260926, "_source" : { "productName" : "理財產品D", "annual_rate" : "3.1200%", "describe" : "90天定投產品,最低12000起投,每天收益到賬消息推送" } }, { "_index" : "product_info", "_type" : "_doc", "_id" : "lEWajIoBaH-7DHixr9am", "_score" : 0.40473348, "_source" : { "productName" : "理財產品A", "annual_rate" : "3.2200%", "describe" : "180天定期理財,最低20000起投,收益穩定,可以自助選擇消息推送" } }, { "_index" : "product_info", "_type" : "_doc", "_id" : "mUWajIoBaH-7DHixr9am", "_score" : 0.3420724, "_source" : { "productName" : "理財產品F", "annual_rate" : "2.7500%", "describe" : "熱門短期產品,3天短期,無須任何手續費用,最低500起投,通過短信提示獲取收益消息" } } ] } }
篩查年利率在3.0000%到3.1000%之間的產品。
GET /product_info/_doc/_search { "query": { "range": { "annual_rate": { "gte": "3.0000%", "lte": "3.1000%" } } } }
返回結果如下:
{ "took" : 8, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "product_info", "_type" : "_doc", "_id" : "mEWajIoBaH-7DHixr9am", "_score" : 1.0, "_source" : { "productName" : "理財產品E", "annual_rate" : "3.0100%", "describe" : "30天定投產品推薦,最低8000起投,每天收益會消息推送" } } ] } }
多語言檢索
多語言檢索在Elasticsearch Serverless服務上應用很廣泛。
英文全文搜索
創建english_index索引并插入數據。
創建索引。
PUT /english_index { "mappings": { "properties": { "text": { "type": "text" } } } }
在索引中插入數據。
POST /_bulk { "index": { "_index": "english_index","_id": "1" }} { "text" : "Hangzhou, referred to as \"Hangzhou\", formerly known as Lin 'an and Qiantang, is a prefecture-level city, provincial capital, sub-provincial city, mega-city of Zhejiang Province, the core city of Hangzhou metropolitan area, the economic, cultural, scientific and educational center of Zhejiang Province approved by The State Council, and one of the central cities of the Yangtze River Delta. As of 2019, the city has jurisdiction over 10 municipal districts, 2 counties and 1 county-level city, with a total area of 16,850 square kilometers and a built-up area of 648.46 square kilometers. By the end of 2021, Hangzhou had a permanent population of 12.204 million. In 2021, the city's GDP reached 1,810.9 billion yuan." } { "index": { "_index": "english_index","_id": "2" }} { "text" : "Hangzhou is one of the first national historical and cultural cities, known as the \"Southeast county\" in the world. Excavations at the Trans-Lake Bridge site show that humans lived here more than 8,000 years ago. The Liangzhu Culture dating back more than 5,000 years is known as the \"dawn of Chinese civilization\". Hangzhou has a history of more than 2,200 years since it was established as a county in the Qin Dynasty. It was once the capital of the Wu Yue State and the Southern Song Dynasty." } { "index": { "_index": "english_index","_id": "3" }} { "text" : "Hangzhou is located in East China, the lower reaches of Qiantang River, the southeast coast, the north of Zhejiang Province, and the southern end of the Beijing-Hangzhou Grand Canal. The geographical coordinates are between 29°11 '-30 °34' N and 118°20 '-120 °37' E. Hangzhou is the core city of the Hangzhou Bay Greater Bay Area and the center city of the G60 Science and Innovation Corridor. Hangzhou has numerous cultural and historical sites. There are a large number of natural and cultural landscape relics in and around the West Lake, representative of which are West Lake culture, Liangzhu culture, silk culture and tea culture." } { "index": { "_index": "english_index","_id": "4" }} { "text" : "Hangzhou is known as \"heaven on earth\" because of its beautiful scenery. Thanks to the convenience of the Beijing-Hangzhou Grand Canal and treaty ports, as well as its own developed silk and grain industries, Hangzhou was historically an important commercial distribution center. Since the new century, with the drive of Alibaba and other high-tech enterprises, the Internet economy has become a new economic growth point in Hangzhou. Hangzhou hosted the 2016 G20 Summit, the 2018 World Short Course Swimming Championships and the 2022 Asian Games. Hangzhou ranked 5th in China's Top 100 Cities list in 2020." }
搜索數據并查看搜索結果。
搜索示例一:
GET english_index/_search { "query": { "match": { "text": "The GDP of Hangzhou is increasing gradually" } } }
返回結果如下所示,包含搜索內容的
_id
為1的文件在返回結果的最前面。{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 2.1957722, "hits" : [ { "_index" : "english_index", "_type" : "_doc", "_id" : "1", "_score" : 2.1957722, "_source" : { "text" : """Hangzhou, referred to as "Hangzhou", formerly known as Lin 'an and Qiantang, is a prefecture-level city, provincial capital, sub-provincial city, mega-city of Zhejiang Province, the core city of Hangzhou metropolitan area, the economic, cultural, scientific and educational center of Zhejiang Province approved by The State Council, and one of the central cities of the Yangtze River Delta. As of 2019, the city has jurisdiction over 10 municipal districts, 2 counties and 1 county-level city, with a total area of 16,850 square kilometers and a built-up area of 648.46 square kilometers. By the end of 2021, Hangzhou had a permanent population of 12.204 million. In 2021, the city's GDP reached 1,810.9 billion yuan.""" } }, { "_index" : "english_index", "_type" : "_doc", "_id" : "3", "_score" : 0.92763716, "_source" : { "text" : "Hangzhou is located in East China, the lower reaches of Qiantang River, the southeast coast, the north of Zhejiang Province, and the southern end of the Beijing-Hangzhou Grand Canal. The geographical coordinates are between 29°11 '-30 °34' N and 118°20 '-120 °37' E. Hangzhou is the core city of the Hangzhou Bay Greater Bay Area and the center city of the G60 Science and Innovation Corridor. Hangzhou has numerous cultural and historical sites. There are a large number of natural and cultural landscape relics in and around the West Lake, representative of which are West Lake culture, Liangzhu culture, silk culture and tea culture." } }, { "_index" : "english_index", "_type" : "_doc", "_id" : "2", "_score" : 0.87203753, "_source" : { "text" : """Hangzhou is one of the first national historical and cultural cities, known as the "Southeast county" in the world. Excavations at the Trans-Lake Bridge site show that humans lived here more than 8,000 years ago. The Liangzhu Culture dating back more than 5,000 years is known as the "dawn of Chinese civilization". Hangzhou has a history of more than 2,200 years since it was established as a county in the Qin Dynasty. It was once the capital of the Wu Yue State and the Southern Song Dynasty.""" } }, { "_index" : "english_index", "_type" : "_doc", "_id" : "4", "_score" : 0.8462847, "_source" : { "text" : """Hangzhou is known as "heaven on earth" because of its beautiful scenery. Thanks to the convenience of the Beijing-Hangzhou Grand Canal and treaty ports, as well as its own developed silk and grain industries, Hangzhou was historically an important commercial distribution center. Since the new century, with the drive of Alibaba and other high-tech enterprises, the Internet economy has become a new economic growth point in Hangzhou. Hangzhou hosted the 2016 G20 Summit, the 2018 World Short Course Swimming Championships and the 2022 Asian Games. Hangzhou ranked 5th in China's Top 100 Cities list in 2020.""" } } ] } }
搜索示例二:
GET english_index/_search { "query": { "match": { "text": "Hangzhou is one of the top 100 cities" } } }
返回結果如下所示,包含搜索內容的
_id
為4的文件在返回結果的最前面。{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 3.2950416, "hits" : [ { "_index" : "english_index", "_type" : "_doc", "_id" : "4", "_score" : 3.2950416, "_source" : { "text" : """Hangzhou is known as "heaven on earth" because of its beautiful scenery. Thanks to the convenience of the Beijing-Hangzhou Grand Canal and treaty ports, as well as its own developed silk and grain industries, Hangzhou was historically an important commercial distribution center. Since the new century, with the drive of Alibaba and other high-tech enterprises, the Internet economy has become a new economic growth point in Hangzhou. Hangzhou hosted the 2016 G20 Summit, the 2018 World Short Course Swimming Championships and the 2022 Asian Games. Hangzhou ranked 5th in China's Top 100 Cities list in 2020.""" } }, { "_index" : "english_index", "_type" : "_doc", "_id" : "1", "_score" : 2.488573, "_source" : { "text" : """Hangzhou, referred to as "Hangzhou", formerly known as Lin 'an and Qiantang, is a prefecture-level city, provincial capital, sub-provincial city, mega-city of Zhejiang Province, the core city of Hangzhou metropolitan area, the economic, cultural, scientific and educational center of Zhejiang Province approved by The State Council, and one of the central cities of the Yangtze River Delta. As of 2019, the city has jurisdiction over 10 municipal districts, 2 counties and 1 county-level city, with a total area of 16,850 square kilometers and a built-up area of 648.46 square kilometers. By the end of 2021, Hangzhou had a permanent population of 12.204 million. In 2021, the city's GDP reached 1,810.9 billion yuan.""" } }, { "_index" : "english_index", "_type" : "_doc", "_id" : "2", "_score" : 2.3840938, "_source" : { "text" : """Hangzhou is one of the first national historical and cultural cities, known as the "Southeast county" in the world. Excavations at the Trans-Lake Bridge site show that humans lived here more than 8,000 years ago. The Liangzhu Culture dating back more than 5,000 years is known as the "dawn of Chinese civilization". Hangzhou has a history of more than 2,200 years since it was established as a county in the Qin Dynasty. It was once the capital of the Wu Yue State and the Southern Song Dynasty.""" } }, { "_index" : "english_index", "_type" : "_doc", "_id" : "3", "_score" : 0.92763716, "_source" : { "text" : "Hangzhou is located in East China, the lower reaches of Qiantang River, the southeast coast, the north of Zhejiang Province, and the southern end of the Beijing-Hangzhou Grand Canal. The geographical coordinates are between 29°11 '-30 °34' N and 118°20 '-120 °37' E. Hangzhou is the core city of the Hangzhou Bay Greater Bay Area and the center city of the G60 Science and Innovation Corridor. Hangzhou has numerous cultural and historical sites. There are a large number of natural and cultural landscape relics in and around the West Lake, representative of which are West Lake culture, Liangzhu culture, silk culture and tea culture." } } ] } }
搜索示例三:
GET english_index/_search { "query": { "match": { "text": "Hangzhou is one of the first national historical and cultural city" } } }
返回結果如下所示,包含搜索內容的
_id
為2的文件在返回結果的最前面。{ "took" : 5, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 5.107232, "hits" : [ { "_index" : "english_index", "_type" : "_doc", "_id" : "2", "_score" : 5.107232, "_source" : { "text" : """Hangzhou is one of the first national historical and cultural cities, known as the "Southeast county" in the world. Excavations at the Trans-Lake Bridge site show that humans lived here more than 8,000 years ago. The Liangzhu Culture dating back more than 5,000 years is known as the "dawn of Chinese civilization". Hangzhou has a history of more than 2,200 years since it was established as a county in the Qin Dynasty. It was once the capital of the Wu Yue State and the Southern Song Dynasty.""" } }, { "_index" : "english_index", "_type" : "_doc", "_id" : "3", "_score" : 3.5984995, "_source" : { "text" : "Hangzhou is located in East China, the lower reaches of Qiantang River, the southeast coast, the north of Zhejiang Province, and the southern end of the Beijing-Hangzhou Grand Canal. The geographical coordinates are between 29°11 '-30 °34' N and 118°20 '-120 °37' E. Hangzhou is the core city of the Hangzhou Bay Greater Bay Area and the center city of the G60 Science and Innovation Corridor. Hangzhou has numerous cultural and historical sites. There are a large number of natural and cultural landscape relics in and around the West Lake, representative of which are West Lake culture, Liangzhu culture, silk culture and tea culture." } }, { "_index" : "english_index", "_type" : "_doc", "_id" : "1", "_score" : 3.5324125, "_source" : { "text" : """Hangzhou, referred to as "Hangzhou", formerly known as Lin 'an and Qiantang, is a prefecture-level city, provincial capital, sub-provincial city, mega-city of Zhejiang Province, the core city of Hangzhou metropolitan area, the economic, cultural, scientific and educational center of Zhejiang Province approved by The State Council, and one of the central cities of the Yangtze River Delta. As of 2019, the city has jurisdiction over 10 municipal districts, 2 counties and 1 county-level city, with a total area of 16,850 square kilometers and a built-up area of 648.46 square kilometers. By the end of 2021, Hangzhou had a permanent population of 12.204 million. In 2021, the city's GDP reached 1,810.9 billion yuan.""" } }, { "_index" : "english_index", "_type" : "_doc", "_id" : "4", "_score" : 1.0729303, "_source" : { "text" : """Hangzhou is known as "heaven on earth" because of its beautiful scenery. Thanks to the convenience of the Beijing-Hangzhou Grand Canal and treaty ports, as well as its own developed silk and grain industries, Hangzhou was historically an important commercial distribution center. Since the new century, with the drive of Alibaba and other high-tech enterprises, the Internet economy has become a new economic growth point in Hangzhou. Hangzhou hosted the 2016 G20 Summit, the 2018 World Short Course Swimming Championships and the 2022 Asian Games. Hangzhou ranked 5th in China's Top 100 Cities list in 2020.""" } } ] } }
法文全文搜索
創建french_index索引并插入數據。
創建索引。
PUT /french_index { "mappings": { "properties": { "text": { "type": "text", "analyzer": "french" } } } }
在索引中插入數據。
POST /_bulk { "index": { "_index": "french_index","_id": "1" }} { "text" : "Hangzhou, appelé ?hang?, anciennement connu sous le nom de linan et qiantang, la ville de la province de zhejiang, la capitale de la province, la ville vice-provinciale, la mégapole, la ville du noyau de la zone métropolitaine de hangzhou, le conseil d’état a approuvé et identifié le centre économique, culturel, scientifique et éducatif de la province de zhejiang, l’une des villes du centre du delta du yangtze. à partir de 2019, la ville est administrée par 10 districts municipaux, 2 comtés et une municipalité au niveau de comté. La superficie totale de la ville est de 16850 kilomètres carrés et la zone batie de 648,46 kilomètres carrés. à la fin de 2021, la ville de hangzhou a une population permanente de 12 204 000 personnes. En 2021, la ville a réalisé un produit régional brut de 1810,9 milliards de yuans." } { "index": { "_index": "french_index","_id": "2" }} { "text" : "Hangzhou est l’une des premières villes historiques et culturelles nationales, célèbre pour son comté célèbre du sud-est. Les fouilles sur le site du pont trans-lac ont révélé que des humains y ont peuplé il y a plus de 8000 ans. La culture liangzhu, qui date de plus de 5000 ans, est appelée ?l’aube de la civilisation chinoise?. Hangzhou a une histoire de plus de 2200 ans depuis la création du comté sous la dynastie qin. Hangzhou a été la capitale de wuyue et de song du sud." } { "index": { "_index": "french_index","_id": "3" }} { "text" : "Hangzhou est située dans la région de l’est de la Chine, en aval de la rivière qiantang, sur la c?te sud-est, au nord du zhejiang et à l’extrémité sud du grand canal de pékin hangzhou. Ses coordonnées géographiques se situent entre 29° 11’ - 30° 34’ de latitude nord et 118° 20’ - 120° 37’ de longitude est. Hangzhou est la ville centrale de la grande baie de la baie de hangzhou et la ville centrale du corridor G60 kochuang. Hangzhou a de nombreux monuments humanistes. Il y a un grand nombre de vestiges de paysages naturels et humains dans et autour de xihu. Les représentants sont la culture xihu, la culture liangzhu, la culture de la soie et la culture du thé." } { "index": { "_index": "french_index","_id": "4" }} { "text" : "Hangzhou a la réputation de ?paradis sur terre? en raison de ses paysages magnifiques. Bénéficiant de la commodité du grand canal de pékin et des ports commerciaux, ainsi que de ses propres industries bien développées de la soie et des céréales, hangzhou a été historiquement un important centre de distribution commerciale. Depuis le nouveau siècle, avec alibaba et d’autres entreprises de haute technologie, l’économie d’internet est devenue le nouveau point de croissance économique de hangzhou. Le sommet du G20 2016, le championnat du monde de natation en piscine courte 2018 et les jeux asiatiques 2022 ont eu lieu à hangzhou. Top 100 des villes chinoises en 2020. Hangzhou est la 5ème." }
搜索數據并查看搜索結果。
GET french_index/_search { "query": { "match": { "text": "Hangzhou est top 100 des villes chinoises en 2020." } } }
返回結果如下所示,包含搜索內容的
_id
為4的文件在返回結果的最前面。{ "took" : 11, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 4, "relation" : "eq" }, "max_score" : 3.7113173, "hits" : [ { "_index" : "french_index", "_type" : "_doc", "_id" : "4", "_score" : 3.7113173, "_source" : { "text" : "Hangzhou a la réputation de ?paradis sur terre? en raison de ses paysages magnifiques. Bénéficiant de la commodité du grand canal de pékin et des ports commerciaux, ainsi que de ses propres industries bien développées de la soie et des céréales, hangzhou a été historiquement un important centre de distribution commerciale. Depuis le nouveau siècle, avec alibaba et d’autres entreprises de haute technologie, l’économie d’internet est devenue le nouveau point de croissance économique de hangzhou. Le sommet du G20 2016, le championnat du monde de natation en piscine courte 2018 et les jeux asiatiques 2022 ont eu lieu à hangzhou. Top 100 des villes chinoises en 2020. Hangzhou est la 5ème." } }, { "_index" : "french_index", "_type" : "_doc", "_id" : "1", "_score" : 1.0079018, "_source" : { "text" : "Hangzhou, appelé ?hang?, anciennement connu sous le nom de linan et qiantang, la ville de la province de zhejiang, la capitale de la province, la ville vice-provinciale, la mégapole, la ville du noyau de la zone métropolitaine de hangzhou, le conseil d’état a approuvé et identifié le centre économique, culturel, scientifique et éducatif de la province de zhejiang, l’une des villes du centre du delta du yangtze. à partir de 2019, la ville est administrée par 10 districts municipaux, 2 comtés et une municipalité au niveau de comté. La superficie totale de la ville est de 16850 kilomètres carrés et la zone batie de 648,46 kilomètres carrés. à la fin de 2021, la ville de hangzhou a une population permanente de 12 204 000 personnes. En 2021, la ville a réalisé un produit régional brut de 1810,9 milliards de yuans." } }, { "_index" : "french_index", "_type" : "_doc", "_id" : "2", "_score" : 0.8698735, "_source" : { "text" : "Hangzhou est l’une des premières villes historiques et culturelles nationales, célèbre pour son comté célèbre du sud-est. Les fouilles sur le site du pont trans-lac ont révélé que des humains y ont peuplé il y a plus de 8000 ans. La culture liangzhu, qui date de plus de 5000 ans, est appelée ?l’aube de la civilisation chinoise?. Hangzhou a une histoire de plus de 2200 ans depuis la création du comté sous la dynastie qin. Hangzhou a été la capitale de wuyue et de song du sud." } }, { "_index" : "french_index", "_type" : "_doc", "_id" : "3", "_score" : 0.4163319, "_source" : { "text" : "Hangzhou est située dans la région de l’est de la Chine, en aval de la rivière qiantang, sur la c?te sud-est, au nord du zhejiang et à l’extrémité sud du grand canal de pékin hangzhou. Ses coordonnées géographiques se situent entre 29° 11’ - 30° 34’ de latitude nord et 118° 20’ - 120° 37’ de longitude est. Hangzhou est la ville centrale de la grande baie de la baie de hangzhou et la ville centrale du corridor G60 kochuang. Hangzhou a de nombreux monuments humanistes. Il y a un grand nombre de vestiges de paysages naturels et humains dans et autour de xihu. Les représentants sont la culture xihu, la culture liangzhu, la culture de la soie et la culture du thé." } } ] } }
支持檢索多種語言
Elasticsearch Serverless服務支持漢語、英語、法語、德語、俄語、日語、韓語、越語、緬語等主要語言。
創建
language
對象。POST language/_doc { "中文": "中文", "英語": "english", "法語": "Fran?ais", "德語": "Deutsch", "日語": "にほんご", "俄語": "Русский язык", "韓語": "???", "越南語": "Ti?ng Vi?t", "緬甸語": "??????????" }
搜索
language
對象。GET language/_search
返回結果如下所示,返回結果與插入的數據是一致的。
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "language", "_type" : "_doc", "_id" : "MUVBjYoBaH-7DHixjus2", "_score" : 1.0, "_source" : { "中文" : "中文", "英語" : "english", "法語" : "Fran?ais", "德語" : "Deutsch", "日語" : "にほんご", "俄語" : "Русский язык", "韓語" : "???", "越南語" : "Ti?ng Vi?t", "緬甸語" : "??????????" } } ] } }
地理位置查詢
基于地理位置信息的應用非常多,例如,通過當前位置信息搜索附近的人。下面主要介紹在阿里云Elasticsearch Serverless服務上進行基于位置信息的檢索。
創建索引并寫入數據。
創建索引attractions,指定userid為keyword字段,locationname為text字段,location為geo_point字段,accesstime為date字段。
PUT /attractions { "mappings": { "properties": { "userid": { "type": "keyword" }, "locationname": { "type": "text" }, "location": { "type": "geo_point" }, "accesstime": { "type": "date", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } }
通過bulk方式插入數據。
POST /_bulk { "index": { "_index": "attractions","_id": "1" }} {"userid": "001", "locationname": "頤和園", "location":[116.273987,39.992019], "accesstime": "2021-05-01 10:30:00"} { "index": { "_index": "attractions","_id": "2" }} {"userid": "002", "locationname": "中關村", "location":[116.326933,39.981078], "accesstime": "2021-05-02 14:45:30"} { "index": { "_index": "attractions","_id": "3" }} {"userid": "003", "locationname": "動物園", "location":[116.338239,39.942547], "accesstime": "2021-05-03 09:20:15"} { "index": { "_index": "attractions","_id": "4" }} {"userid": "004", "locationname": "北京大學", "location":[116.319935,39.996275], "accesstime": "2021-05-04 16:55:10"} { "index": { "_index": "attractions","_id": "5" }} {"userid": "005", "locationname": "香山公園", "location":[116.191247,39.997232], "accesstime": "2021-05-05 13:00:45"} { "index": { "_index": "attractions","_id": "6" }} {"userid": "006", "locationname": "圓明園", "location":[116.309868,40.018307], "accesstime": "2021-05-06 19:10:20"}
查詢數據。
通過userid,查詢用戶訪問景點的記錄。
GET /attractions/_search { "query": { "query_string": { "default_field": "userid", "query": "005" } } }
返回結果如下:
{ "took" : 21, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 0.2876821, "hits" : [ { "_index" : "attractions", "_type" : "_doc", "_id" : "5", "_score" : 0.2876821, "_source" : { "userid" : "005", "locationname" : "香山公園", "location" : [ 116.191247, 39.997232 ], "accesstime" : "2021-05-05 13:00:45" } } ] } }
查詢該景點附近10公里范圍內的其他景點以及用戶的訪問時間。
GET /attractions/_search { "query": { "bool": { "filter": { "geo_distance": { "distance": "10km", "location": { "lat": 39.997232, "lon": 116.191247 } } } } } }
返回結果如下:
{ "took" : 46, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.0, "hits" : [ { "_index" : "attractions", "_type" : "_doc", "_id" : "5", "_score" : 0.0, "_source" : { "userid" : "005", "locationname" : "香山公園", "location" : [ 116.191247, 39.997232 ], "accesstime" : "2021-05-05 13:00:45" } }, { "_index" : "attractions", "_type" : "_doc", "_id" : "1", "_score" : 0.0, "_source" : { "userid" : "001", "locationname" : "頤和園", "location" : [ 116.273987, 39.992019 ], "accesstime" : "2021-05-01 10:30:00" } } ] } }