ElasticSearch記錄(2)curl操作
新建和刪除 Index
新建 Index,可以直接向 Elastic 服務器發出 PUT 請求。下面的例子是新建一個名叫weather
的 Index。
curl -XPUT http://192.168.239.101:9200/weather/
服務器返回一個 JSON 對象,裏面的acknowledged
字段表示操作成功。
{ "acknowledged":true, "shards_acknowledged":true }
然後,我們發出 DELETE 請求,刪除這個 Index。
$ curl -X DELETE http://192.168.239.101:9200/weather
數據操作
5.1 新增記錄
向指定的 /Index/Type 發送 PUT 請求,就可以在 Index 裏面新增一條記錄。比如,向/accounts/person
發送請求,就可以新增一條人員記錄。
curl -X PUT http://192.168.239.101:9200/accounts/person -d ‘ { "user": "張三", "title": "工程師", "desc": "數據庫管理" }‘
服務器返回的 JSON 對象,會給出 Index、Type、Id、Version 等信息。
{ "_index":"accounts", "_type":"person", "_id":"1", "_version":1, "result":"created", "_shards":{"total":2,"successful":1,"failed":0}, "created":true }
如果你仔細看,會發現請求路徑是/accounts/person/1
,最後的1
是該條記錄的 Id。它不一定是數字,任意字符串(比如abc
)都可以。
新增記錄的時候,也可以不指定 Id,這時要改成 POST 請求。
curl -X POST http://192.168.239.101:9200/accounts/person -d ‘{ "user": "李四", "title": "工程師", "desc": "系統管理" }‘
上面代碼中,向/accounts/person
發出一個 POST 請求,添加一個記錄。這時,服務器返回的 JSON 對象裏面,_id
字段就是一個隨機字符串。
{ "_index":"accounts", "_type":"person", "_id":"AV3qGfrC6jMbsbXb6k1p", "_version":1, "result":"created", "_shards":{"total":2,"successful":1,"failed":0}, "created":true }
註意,如果沒有先創建 Index(這個例子是accounts
),直接執行上面的命令,Elastic 也不會報錯,而是直接生成指定的 Index。所以,打字的時候要小心,不要寫錯 Index 的名稱。
5.2 查看記錄
向/Index/Type/Id
發出 GET 請求,就可以查看這條記錄。
curl -XGET http://192.168.239.101:9200/accounts/person/1?pretty
上面代碼請求查看/accounts/person/1
這條記錄,URL 的參數pretty=true
表示以易讀的格式返回。
返回的數據中,found
字段表示查詢成功,_source
字段返回原始記錄。
{ "_index" : "accounts", "_type" : "person", "_id" : "1", "_version" : 1, "found" : true, "_source" : { "user" : "張三", "title" : "工程師", "desc" : "數據庫管理" } }
如果 Id 不正確,就查不到數據,found
字段就是false
。
curl -XGET http://192.168.239.101:9200/accounts/person/1?pretty { "_index" : "accounts", "_type" : "person", "_id" : "1", "found" : false }
5.3 刪除記錄
刪除記錄就是發出 DELETE 請求。
curl -X DELETE ‘http://192.168.239.101:9200/accounts/person/AWjP8cuP2r1J3ImKYMiA‘
5.4 更新記錄
更新記錄就是使用 PUT 請求,重新發送一次數據。
curl -X PUT http://192.168.239.101:9200/accounts/person/1 -d ‘ { "user": "李四", "title": "工程師", "desc": "數據庫管理" }‘
curl -X PUT http://192.168.239.101:9200/accounts/person/1 -d ‘ > { > "user": "李四", > "title": "工程師", > "desc": "數據庫管理" > }‘ {"_index":"accounts","_type":"person","_id":"1","_version":2,"_shards":{"total":2,"successful":2,"failed":0},"created":false}
上面代碼中,我們將原始數據從"數據庫管理"改成"數據庫管理,軟件開發"。 返回結果裏面,有幾個字段發生了變化。
"_version" : 2, "result" : "updated", "created" : false
可以看到,記錄的 Id 沒變,但是版本(version)從1
變成2
,操作類型(result)從created
變成updated
,created
字段變成false
,因為這次不是新建記錄。
數據查詢
返回所有記錄
使用 GET 方法,直接請求/Index/Type/_search
,就會返回所有記錄。
curl http://192.168.239.101:9200/accounts/person/_search?pretty { "took" : 5, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.0, "hits" : [ { "_index" : "accounts", "_type" : "person", "_id" : "2", "_score" : 1.0, "_source" : { "user" : "十大的", "title" : "工程師", "desc" : "數據庫管理" } }, { "_index" : "accounts", "_type" : "person", "_id" : "1", "_score" : 1.0, "_source" : { "user" : "李四", "title" : "工程師", "desc" : "數據庫管理" } } ] } }
上面代碼中,返回結果的 took
字段表示該操作的耗時(單位為毫秒),timed_out
字段表示是否超時,hits
字段表示命中的記錄,裏面子字段的含義如下。
total
:返回記錄數,本例是2條。max_score
:最高的匹配程度,本例是1.0
。hits
:返回的記錄組成的數組。
返回的記錄中,每條記錄都有一個_score
字段,表示匹配的程序,默認是按照這個字段降序排列。
全文搜索
Elastic 的查詢非常特別,使用自己的查詢語法,要求 GET 請求帶有數據體。
curl ‘http://192.168.239.101:9200/accounts/person/_search‘ -d ‘ { "query" : { "match" : { "desc" : "系統" }} }‘
上面代碼使用 Match 查詢,指定的匹配條件是desc
字段裏面包含"軟件"這個詞。返回結果如下。
{"took":11,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.2169777,"hits":[{"_index":"accounts","_type":"person","_id":"2","_score":0.2169777,"_source": { "user": "十大的", "title": "工程師", "desc": "系統管理" }}]}}
Elastic 默認一次返回10條結果,可以通過size
字段改變這個設置。
curl ‘http://192.168.239.101:9200/accounts/person/_search‘ -d ‘ { "query" : { "match" : { "desc" : "系統" }}, "size": 1 }‘
上面代碼指定,每次只返回一條結果。
還可以通過from
字段,指定位移。
curl ‘http://192.168.239.101:9200/accounts/person/_search‘ -d ‘ { "query" : { "match" : { "desc" : "系統" }}, "from": 1, "size": 1 }‘
上面代碼指定,從位置1開始(默認是從位置0開始),只返回一條結果。
邏輯運算
如果有多個搜索關鍵字, Elastic 認為它們是or
關系。
curl ‘http://192.168.239.101:9200/accounts/person/_search?pretty‘ -d ‘ > { > "query" : { "match" : { "desc" : "數據 系統" }} > }‘ { "took" : 9, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 0.03182549, "hits" : [ { "_index" : "accounts", "_type" : "person", "_id" : "2", "_score" : 0.03182549, "_source" : { "user" : "十大的", "title" : "工程師", "desc" : "系統管理" } }, { "_index" : "accounts", "_type" : "person", "_id" : "1", "_score" : 0.027847305, "_source" : { "user" : "李四", "title" : "工程師", "desc" : "數據庫管理" } } ] } }
上面代碼搜索的是軟件 or 系統
。
對多個field發起查詢:multi_match
curl -XGET http://192.168.239.101:9200/bjsxt/employee/_search?pretty -d ‘ { "query": {"multi_match": { "query":"bin", "fields":["last_name","first_name"], "operator":"and" } } }‘
如果要執行多個關鍵詞的and
搜索,必須使用布爾查詢。
curl ‘http://192.168.239.101:9200/accounts/person/_search?pretty‘ -d ‘ { "query": { "bool": { "must": [ { "match": { "desc": "軟件" } }, { "match": { "desc": "系統" } } ] } } }‘
ElasticSearch記錄(2)curl操作