1、es是基於lucene開發的一個分布式全文索引框架。往ES中存儲和從ES中查詢,格式為Json
索引:Index 相當於數據庫中的database
類型:type 相當於數據庫中的table
主鍵:id 相當於數據庫中的主鍵
往ES中存儲數據,其實就是往ES中的index下的type存儲Json數據
RESTFul風格API
通過http的形式,發送請求,對ES進行操作
查詢: GET
刪除: DELETE
添加: PUT/POST
修改: PUT/POST
1、PUT操作,store為database;books為table;2為主鍵
curl -XPUT 'http://hadoop100:9200/store/books/2' -d '{ "title": "Elasticsearch Blueprints", "name" : { "first" : "Vineeth", "last" : "Mohan" }, "publish_date":"2015-06-06", "price":"35.99" }'
2、GET操作
a)根據id查詢
curl -XGET 'http://hadoop100:9200/store/books/1'
b)通過_source獲取指定的字段
curl -XGET 'http://hadoop100:9200/store/books/1?_source=title' curl -XGET 'http://hadoop100:9200/store/books/1?_source=title,price' curl -XGET 'http://hadoop100:9200/store/books/1?_source'
c)最簡單_search 結合filter查詢
curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{ "query": { "bool": { "must": { "match_all": {} }, "filter": { "term": { "price": 35.99 } } } } }'
#指定多個值 curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{ "query" : { "bool" : { "filter" : { "terms" : { "price" : [35.99, 99.99] } } } } }'
curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{ "query" : { "bool" : { "filter" : { "term" : { "publish_date" : "2015-02-06" } } } } }'
d)bool過濾查詢,可以做組合過濾查詢
# SELECT * FROM books WHERE (price = 35.99 OR price = 99.99) AND publish_date != "2016-02-06" # 類似的,Elasticsearch也有 and, or, not這樣的組合條件的查詢方式 # 格式如下: # { # "bool" : { # "must" : [], # "should" : [], # "must_not" : [], # } # } # # must: 條件必須滿足,相當於 and # should: 條件可以滿足也可以不滿足,相當於 or # must_not: 條件不需要滿足,相當於 not curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{ "query" : { "bool" : { "should" : [ { "term" : {"price" : 35.99}}, { "term" : {"price" : 99.99}} ], "must_not" : { "term" : {"publish_date" : "2016-02-06"} } } } }'
e)嵌套查詢
# SELECT * FROM books WHERE price = 35.99 OR ( publish_date = "2016-02-06" AND price = 99.99 ) curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{ "query": { "bool": { "should": [ { "term": { "price": 35.99 } }, { "bool": { "must": [ { "term": { "publish_date": "2016-02-06" } }, { "term": { "price": 99.99 } } ] } } ] } } }'
f)range范圍過濾
# SELECT * FROM books WHERE price >= 10 AND price < 99 # gt : > 大於 # lt : < 小於 # gte : >= 大於等於 # lte : <= 小於等於 curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{ "query": { "range" : { "price" : { "gte" : 10, "lt" : 99 } } } }
#name和author都必須包含Guide,並且價錢等於33.99或者188.99 curl -XGET 'http://hadoop100:9200/store/books/_search' -d '{ "query": { "bool": { "must": { "multi_match": { "operator": "and", "fields": [ "name", "author" ], "query": "Guide" } }, "filter": { "terms": { "price": [ 35.99, 188.99 ] } } } } }'
3、UPDATE操作
a)以通過覆蓋的方式更新
curl -XPUT 'http://192.168.10.16:9200/store/books/1' -d '{ "title": "Elasticsearch: The Definitive Guide", "name" : { "first" : "Zachary", "last" : "Tong" }, "publish_date":"2016-02-06", "price":"99.99" }'
b)通過 _update API的方式單獨更新你想要更新的
curl -XPOST 'http://192.168.10.16:9200/store/books/1/_update' -d '{ "doc": { "price" : 88.88 } }'
4、DELETE操作
curl -XDELETE 'http://192.168.10.16:9200/store/books/1'
5、查詢所有(_search)
http://hadoop100:9200/store/books/_search