Elasticsearch入門級用法
首先, Elasticsearch是一個實時分散式搜尋和分析引擎。它使用Java開發並使用Lucene作為其核心來實現所有索引和搜尋的功能,但是它的目的是通過簡單的 RESTfulAPI 來隱藏Lucene的複雜性,從而讓全文搜尋變得簡單。不過,Elasticsearch不僅僅是Lucene和全文搜尋,我們還能這樣去描述它:
1.分散式的實時檔案儲存,每個欄位都被索引並可被搜尋
2.分散式的實時分析搜尋引擎
3.可以擴充套件到上百臺伺服器,處理PB級結構化或非結構化資料
而且,所有的這些功能被整合到一個服務裡面,你的應用可以通過簡單的 RESTful API 、各種語言的客戶端甚至命令列與之互動。so 上手還是比較容易的。
官網:https://www.elastic.co/guide/en/elasticsearch/reference/index.html
下面是一些入門級的curl命令
索引對映的定義
1.curl -XPUT localhost:9200/_template/template001?pretty --data-binary @mapping.json
2."curl -XPUT localhost:9200/_template/template001 -d '
{
"template": "te*",
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "0"
}
},
"mappings": {
"type001": {
"properties": {
"id": { "type": "keyword" },
"name": { "type": "text" },
"num":{"type":"double" },
"time":{"type" : "date"}
}
}
}
}'
put 資料
1.curl -XPOST localhost:9200/_bulk?pretty --data-binary @data.json
data.json格式
{ "index" : { "_index" : "test", "_type" : "type001", "_id" : "1" } }
{"id":"001","name":"jam","num":[ "001","002"],"date": "2016-01-02"}
{ "index" : { "_index" : "test", "_type" : "type001", "_id" : "2" } }
{"id":"001","name":"jam","num":[ "001","002"],"date": "2016-01-02"}
注意:第二行資料即使超長也一定不要下換一行。保持一行index一行資料是沒錯的,本人親驗過的。
2.curl -XPOST localhost:9200/temp001/type001/001?pretty -d '
{
"id": "id001",
"name": "text123456",
"num": 123.00,
"time": "2017-01-01"
}'
get資料
curl -XGET localhost:9200/_template?pretty(檢索所有的template)
curl -XGET localhost:9200/_template/temp*?pretty
curl 'http://localhost:9200/_search?pretty'(檢索所有的index)
curl -XGET localhost:9200/_template/temp001?pretty
curl -XGET localhost:9200/temp001/_mapping?pretty
curl -XGET localhost:9200/temp001/type001/_search?q=num:123.00 (帶參search)
delete資料
curl -XDELETE localhost:9200/_template/temp001
建立search template
curl -XPUT localhost:9200/_search/template/tempsearch -d '
{
"template": {
"query": {
"more_like_this" : {
"like" : {
"_index" : "{{index}}",
"_type" : "{{type}}",
"_id" : "{{id}}"
},
}
}
}
}'
curl -XGET localhost:9200/_search/template?pretty -d '
{
"id": "tempsearch",
"params": {
"index" : "test001",
"type" : "test01",
"id" : "1"
}
}'
分頁
curl localhost:9200/_search?pretty -d'
{
"from" :10 , "size":10
}'
以上只是一些基本的用法,具體可見官網document。個人感覺ES蠻強大的,使用DSL語句查詢:dsl指定json作為請求體。這也是我們使用它的原因之一。容錯能力比較強:主片掛了,副片頂上 ,容易擴充套件。但是,瓶頸也不少,讀寫有一定的延時,我在一次性PUT大量資料之後明顯感覺等待的時間較長。在記憶體方面,ES在聚合時超佔記憶體。需要優秀的硬體資源做支撐。個人覺得最好將ES建立在其他獨立的資料庫上使用。我們就是這麼做的。