Elasticsearch 简述
Elasticsearch(构建于 Lucene 之上)在一个容易管理的包中提供了高性能的全文搜索功能,支持开箱即用地集群化扩展。您可以通过标准的 REST API 或从特定于编程语言的客户端库与 Elasticsearch 进行交互。
个人使用下来明显感觉好用,封装的简单,开发者在尽量降低使用门槛。
Elasticsearch 命令行例子
- 启动
/elastic-search-dir/bin/elasticsearch
- 添加数据
curl -XPUT "http://localhost:9200/music/songs/1" -d '{ "name": "Deck the Halls", "year": 1885, "lyrics": "Fa la la la la" }'
- 更新数据
curl -XPUT "http://localhost:9200/music/songs/1" -d '{ "name": "Deck the Halls", "year": 1886, "lyrics": "Fa la la la la" }'
- 通过json添加数据
curl -XPUT "http://localhost:9200/music/songs/2" -d @caseyjones.json
curl -XPUT "http://localhost:9200/music/songs/3" -d @walking.json
caseyjones.json文件:
{ "artist": "Wallace Saunders", "year": 1909, "styles": [ "traditional" ], "album": "Unknown", "name": "Ballad of Casey Jones", "lyrics": "Come all you rounders if you want to hear The story of a brave engineer Casey Jones was the rounder's name.... Come all you rounders if you want to hear The story of a brave engineer Casey Jones was the rounder's name On the six-eight wheeler, boys, he won his fame The caller called Casey at half past four He kissed his wife at the station door He mounted to the cabin with the orders in his hand And he took his farewell trip to that promis'd land Chorus: Casey Jones--mounted to his cabin Casey Jones--with his orders in his hand Casey Jones--mounted to his cabin And he took his... land" }
walking.json文件:
{ "artist": "Clarence Ashley", "year": 1920, "name": "Walking Boss", "styles": [ "folk", "protest" ], "album": "Traditional", "lyrics": "Walkin' boss Walkin' boss Walkin' boss I don't belong to you I belong I belong I belong To that steel driving crew Well you work one day Work one day Work one day Then go lay around the shanty two" }
- 查询字段
curl -XGET "http://localhost:9200/music/songs/_search?q=lyrics:'you'"
- 查询范围
curl -XGET "http://localhost:9200/music/songs/_search?q=year:<1900"
- 查询范围+挑选结果字段
curl -XGET "http://localhost:9200/music/songs/_search?q=year:>1900&_source=year"
- 查询也可以用json
curl -XGET "http://localhost:9200/music/songs/_search" -d @query.json
query.json文件:
{ "query" : { "match" : { "album" : "Traditional" } } }
后续1:java maven依赖包
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.1.1</version> </dependency>
后续2:实时日志框架
ELK 堆栈,即 Elasticsearch、Logstash(用于日志管理)和 Kibana(用于报告/可视化).
最后写点感想吧,好用的东西,大数据搜索非常快,有效补充数据库DB的缺点。