首先, 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建立在其他独立的数据库上使用。我们就是这么做的。