【ElasticSearch】使用Docker安装ElasticSearch、基本增删改查使用
一、Elasticsearch
Elasticsearch 是一個分布式、可擴展、實時的搜索與數據分析引擎。
官方中文文檔:Elasticsearch 權威指南
二、安裝
1、拉取鏡像
docker pull elasticsearch
2、新建容器并運行
如果配置較低,可以設置初始、最大堆內存空間256m(默認是2G)
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 elasticsearch
3、檢測安裝成功
瀏覽器訪問 ip地址:9200,返回結果如下,安裝成功
附:常用命令
進入容器并使用命令行交互
docker exec -it d345daee829c /bin/bash
啟動一個已經停止的容器
docker run d345daee829c
三、基本使用(Restful API)
使用 postman 進行測試
1、增加
使用 PUT 請求 進行增加操作,將以下 JSON 數據放入請求體 Body 中:
{"name":"John","age":25,"about":"I love coding","interests":["coding","running","sleeping","eating"] }可以看到響應的 JSON 數據:
2、刪除
使用 DELETE請求 進行刪除操作:
3、修改
仍然使用 PUT 請求 進行修改操作,只不過返回的 JSON 中的 result 為:"result": "updated"
4、查詢
(1)使用 HEAD 請求 判斷是否存在
(2)使用 GET 請求 獲取整個 JSON
(3)查詢所有:使用 _search 查詢所有內容
(4)條件查詢
條件查詢1:直接使用 url 查詢 about 屬性包含 love 的內容
在返回結果中,可以看到搜索結果以及相關性得分:"_score": 0.25316024
條件查詢2:使用查詢表達式
(實際使用時,為了解決GET沒有請求體的問題,可以使用POST請求)
(5)全文搜索:將 “I love” 分詞之后檢索
(6)短語搜索:將 “I love” 作為一個整體檢索
{"query":{"match_phrase":{"about":"I love"}} }(7)高亮搜索:將查詢結果中的指定內容高亮出來
{"query":{"match":{"about":"I am"}},"highlight":{"fields":{"about":{}}} }高亮搜索返回結果:
{"took": 11,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": 4,"max_score": 0.67779577,"hits": [{"_index": "megacorp","_type": "employee","_id": "3","_score": 0.67779577,"_source": {"name": "Tim","age": 20,"about": "I am a programmer, I hate cats, here add something.","interests": ["programming","programming","programming"]},"highlight": {"about": ["<em>I</em> <em>am</em> a programmer, <em>I</em> hate cats, here add something."]}},{"_index": "megacorp","_type": "employee","_id": "7","_score": 0.25316024,"_source": {"name": "John","age": 25,"about": "I love coding","interests": ["coding","running","sleeping","eating"]},"highlight": {"about": ["<em>I</em> love coding"]}},{"_index": "megacorp","_type": "employee","_id": "2","_score": 0.16044298,"_source": {"name": "Bob","age": 27,"about": "I love cats","interests": ["swimming","reading","thinking","washing"]},"highlight": {"about": ["<em>I</em> love cats"]}},{"_index": "megacorp","_type": "employee","_id": "6","_score": 0.16044298,"_source": {"name": "John","age": 25,"about": "I love coding","interests": ["coding","running","sleeping","eating"]},"highlight": {"about": ["<em>I</em> love coding"]}}]} }(8)多條件查詢
查詢 about 屬性中包含 “I love” ,并且為 23 歲以上的雇員:
總結
以上是生活随笔為你收集整理的【ElasticSearch】使用Docker安装ElasticSearch、基本增删改查使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 网络IO发展历程:BIO、NIO、多路复
- 下一篇: 【jQuery】使用id选择器,找出外层