Elasticsearch 基本命令
启动Elasticsearch
./bin/elasticsearch
以守护进程方式在后台运行
./bin/elasticsearch -d
检查Elasticsearch是否启动成功
curl -XGET 'http://localhost:9200/' -H 'Content-Type: application/json'
创建集群(多节点)
./bin/elasticsearch -d ./elasticsearch -Epath.data=data2 -Epath.logs=log2 -d ./elasticsearch -Epath.data=data3 -Epath.logs=log3 -d
启动Kibana
./bin/kibana //压缩包安装 brew service start Kibaba //mac brew 安装
使用 -i 显示HTTP头信息
curl -i -XGET 'http://localhost:9200/' -H 'Content-Type: application/json'
创建索引
curl -X PUT "localhost:9200/megacorp/employee/1?pretty" -H 'Content-Type: application/json' -d' { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } //解释: curl -X PUT "localhost:9200/索引名称/类型名称/特定雇员的ID?pretty" -H 'Content-Type: application/json' -d'
检索文档
curl -X GET "localhost:9200/megacorp/employee/1?pretty"
删除文档
curl -XDELETE "http://localhost:9200/megacorp/employee/1?pretty"
判断文档是否存在
curl -XHEAD "http://localhost:9200/megacorp/employee/1?pretty" //成功返回200,失败返回404
使用_search 搜索所有文档
curl -XGET "http://localhost:9200/megacorp/employee/_search" //结果放在数组hit之中
条件查询,如查询last_name=Smith的
curl -XGET "http://localhost:9200/megacorp/employee/_search?q=last_name:Smith&pretty"
使用DSL(领域特定语言)进行查询(构造成JSON)
//放在query请求体中 curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match": { "last_name": "Smith" } } }'
使用过滤器
curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": { "match": { "last_name": "smith" } }, "filter": { "range": { "age": { "gt": 30 } } } } } }'
全文搜索
GET /megacorp/employee/_search?pretty { "query": { "match": { "about": "rock climbing" } } } //返回结果中_score是相关性评分
短语搜索(精确匹配词语)
curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_phrase": { "about": "rock climbing" } } }'
高亮搜索
//当执行该查询时,返回结果与之前一样,与此同时结果中还多了一个叫做 highlight 的部分。这个部分包含了 about 属性匹配的文本片段,并以 HTML 标签 <em></em> 封装 curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_phrase": { "about": "rock climbing" } }, "highlight": { "fields": { "about": {} } } }'
聚合(分析,类似于group by)
curl -X GET "localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "aggs": { "all_interests": {//聚合字段 "terms": { "field": "interests" } } } } //该操作会报错,不允许聚合,是因为该字段没有进行优化(类似加索引),没有优化的字段ES是禁止聚合/排序操作的,所以需要将要聚合的子弹添加优化
添加优化
curl -X PUT "localhost:9200/megacorp/_mapping?pretty" -H 'Content-Type: application/json' -d' { "properties": { "interests": { "type": "text", "fielddata": true } } }
只对查询到的数据聚合
curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match": { "last_name": "smith" } }, "aggs": { "all_interests": { "terms": { "field": "interests" } } } }'
聚合求平均值
curl -XGET "http://localhost:9200/megacorp/employee/_search?pretty" -H 'Content-Type: application/json' -d' { "aggs": { "all_interests": { "terms": { "field": "interests" }, "aggs": { "avg_age": { "avg": { "field": "age" } } } } } }'
获取集群健康状态
curl -XGET "http://localhost:9200/_cluster/health?pretty"
创建索引时设置主分片数量和副本数量
curl -XPUT "http://localhost:9200/blogs?pretty" -H 'Content-Type: application/json' -d' { "settings": { "number_of_shards": 3, "number_of_replicas": 1 } }'
修改副本分片数量
PUT /blogs/_settings?pretty { "number_of_replicas" : 2 }
创建索引(使用POST 自动生成ID)
curl -XPOST "http://localhost:9200/website/blog/?pretty" -H 'Content-Type: application/json' -d' { "title": "My second blog entry", "text": "Still trying this out...", "date": "2014/01/01" }'
返回文档的一部分
curl -XGET "http://localhost:9200/website/blog/123?_source=title,text&pretty"
只想要_source字段,不需要任何元数据
curl -XGET "http://localhost:9200/website/blog/123/_source?pretty"
索引唯一文档
//方法一:POST //方法二:PUT,但是判断没有相同索引存在的时候才创建索引 //2.1 使用?op_type=create参数 curl -XPUT "http://localhost:9200/website/blog/123?op_type=create" -H 'Content-Type: application/json' -d' { "title": "My second blog entry", "text": "Still trying this out...", "date": "2014/01/01" }' //2.2 使用_create参数 PUT /website/blog/123/_create { "title": "My second blog entry", "text": "Still trying this out...", "date": "2014/01/01" } //失败返回409,成功返回201
删除文档
curl -XDELETE "http://localhost:9200/website/blog/123?pretty"
文档部分更新
curl -XPOST "http://localhost:9200/website/blog/1/_update?pretty" -H 'Content-Type: application/json' -d' { "doc": { "tags": ["testing"], "views": 0 } }'
使用脚本更新
curl -X POST "localhost:9200/website/blog/1/_update?pretty" -H 'Content-Type: application/json' -d' { "script" : "ctx._source.views+=1" }
批量取回数据
curl -XGET "http://localhost:9200/_mget?pretty" -H 'Content-Type: application/json' -d' { "docs" : [ { "_index" : "website", "_type" : "blog", "_id" : 2 }, { "_index" : "website", "_type" : "pageviews", "_id" : 1, "_source": "views" } ] }'
空搜索
curl -XGET "http://localhost:9200/_search?pretty"
设置超时时间
curl -XGET "http://localhost:9200/_search??timeout=10ms"
多索引多类型
/_search
在所有的索引中搜索所有的类型
/gb/_search
在 gb 索引中搜索所有的类型
/gb,us/_search
在 gb 和 us 索引中搜索所有的文档
/g,u/_search
在任何以 g 或者 u 开头的索引中搜索所有的类型
/gb/user/_search
在 gb 索引中搜索 user 类型
/gb,us/user,tweet/_search
在 gb 和 us 索引中搜索 user 和 tweet 类型
/_all/user,tweet/_search
在所有的索引中搜索 user 和 tweet 类型分页
curl -XGET "http://localhost:9200/_search?size=5&from=10&pretty"
分析器(分词器)
curl -XGET "http://localhost:9200/_analyze?pretty" -H 'Content-Type: application/json' -d' { "analyzer": "standard", "text": "Text to analyze" }'
查看映射
curl -XGET "http://localhost:9200/website/_mapping/"
空查询
curl -XGET "http://localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' {}' //等价于 curl -XGET "http://localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } }'
查询结构
{ QUERY_NAME: { FIELD_NAME: { ARGUMENT: VALUE, ARGUMENT: VALUE, ... } } } { "match": { "tweet": "elasticsearch" } }
验证查询
curl -XGET "http://localhost:9200/gb/tweet/_validate/query?pretty" -H 'Content-Type: application/json' -d' { "query": { "tweet": { "match": "really powerful" } } }'
寻找错误信息
curl -X GET "localhost:9200/gb/tweet/_validate/query?explain&pretty" -H 'Content-Type: application/json' -d' { "query": { "tweet": { "match": "really powerful" } } }
排序
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "filter": { "term": { "user_id": 1 } } } }, "sort": { "date": { "order": "desc" } } }
多级排序
curl -X GET "localhost:9200/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": { "match": { "tweet": "manage text search" } }, "filter": { "term": { "user_id": 2 } } } }, "sort": [{ "date": { "order": "desc" } }, { "_score": { "order": "desc" } }] }'
本作品采用《CC 协议》,转载必须注明作者和本文链接