elasticsearch是一个基于Lucene构建的开源,分布式,Restful搜索引擎,支持Http使用JSON进行数据索引,下面简单试用。
首先下载(我选择的版本是0.19.9),解压后运行(执行bin/elasticsearch)
一,插入索引
curl -XPUT 'http://localhost:9200/addressbook/friends/1' -d '{
> "name":"stone",
> "tel":"1234567"
> }'
{"ok":true,"_index":"addressbook","_type":"friends","_id":"1","_version":1}
或者通过制定operation type的方式插入索引(uri的风格还有其他方式,可以参照官方介绍)
curl -XPUT 'http://localhost:9200/addressbook/friends/2?op_type=create'
> "name":"penjin",
> "tel":"2345678"
> }'
{"ok":true,"_index":"addressbook","_type":"friends","_id":"2","_version":1}
二,查找索引
curl -XGET 'http://localhost:9200/addressbook/friends/1'
{"_index":"addressbook","_type":"friends","_id":"1","_version":1,"exists":true, "_source" : {
"name":"stone",
"tel":"1234567"
}}
查找索引时指定返回的域
curl -XGET 'http://localhost:9200/addressbook/friends/2?fields=tel'
{"_index":"addressbook","_type":"friends","_id":"2","_version":1,"exists":true,"fields":{"tel":"2345678"}}
查找不存在的索引返回错误
curl -XGET 'http://localhost:9200/addressbook/friends/3'
{"_index":"addressbook","_type":"friends","_id":"3","exists":false}
一次查询返回多个结果(具体还可以设置返回指定域)
curl 'localhost:9200/addressbook/friends/_mget' -d '{
> "ids":["1","2"]
> }'
{"docs":[{"_index":"addressbook","_type":"friends","_id":"1","_version":1,"exists":true, "_source" : {
"name":"stone",
"tel":"1234567"
}},{"_index":"addressbook","_type":"friends","_id":"2","_version":1,"exists":true, "_source" : {
"name":"penjin",
"tel":"2345678"
}}]}
三,搜索(可指定搜索条件,搜索范围,结果排序等,示例如下)
curl -XGET 'http://localhost:9200/addressbook/friends/_search' -d '{
> "query":{
> "term" : {"name":"penjin"}
> }
> }'
{"took":262,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":1,"max_score":0.30685282,"hits":[{"_index":"addressbook","_type":"friends","_id":"2","_score":0.30685282, "_source" : {
"name":"penjin",
"tel":"2345678"
}}]}}
四,删除索引(还可指定删除条件等)
curl -XDELETE 'http://localhost:9200/addressbook/friends/1'
{"ok":true,"found":true,"_index":"addressbook","_type":"friends","_id":"1","_version":2}
elasticsearch是一个分布式的搜索引擎,具体使用上如何分片,集群等,以后有项目做时再深入了解,感觉用来做点日志分析挺不错的。