elasticsearch的基础语句介绍 聚合功能很强大 可以分析数据

elasticsearch聚合功能很强大 可以分析数据,比MySQL更加强大。可以根据爬山归类到运动。。。
下面是 安装以及一些基础命令
安装
curl -L -O http://download.elasticsearch.org/PATH/TO/VERSION.zip <1>
unzip elasticsearch- VERSION.zipcdelasticsearch VERSION

插件
./bin/plugin -i elasticsearch/marvel/latest

禁用监控,关闭Marvel:
echo ‘marvel.agent.enabled: false’ >> ./config/elasticsearch.yml

运行
cd /usr/share/elasticsearch
./bin/elasticsearch

打开另一个终端运行测试:
curl ‘http://localhost:9200/?pretty
你能看到以下信息:{
“status”: 200,
“name”: “Shrunken Bones”,
“version”: {
“number”: “1.4.0”,
“lucene_version”: “4.10”
},
“tagline”: “You Know, for Search”
}
这说明你的ELasticsearch集群已经启动并运行

数据库对比
关系数据库(Relational DB) -> 库(Databases) -> 表(Tables) -> 行(Rows) -> 列(Columns)
Elasticsearch -> 索引(Indices) -> 类型(Types) -> 文档(Documents) -> 字段(Fields)

创建数据
PUT /megacorp/employee/1
{
“first_name” : “John”,
“last_name” : “Smith”,
“age” : 25,
“about” : “I love to go rock climbing”,
“interests”: [ “sports”, “music” ]
}

PUT /megacorp/employee/3
{
“first_name”: “Douglas”,
“last_name”:”Fir”,
“age”:35,
“about”:”I like to build cabinets”,
“interests”:[“forestry”]
}
注意到路径 /megacorp/employee/1 包含三部分信息:
名字
说明
megacorp 索引名
employee 类型名
1 这个职员的ID

搜索全部员工的请求:
GET /megacorp/employee/_search
查询一条
GET /megacorp/employee/_search?q=_id:AU3leyatuy05ZviT4RuM

DSL语句查询 json格式
GET /megacorp/employee/_search
{
“query” : {
“match” : {
“last_name” : “Smith”
}
}
}
全文模糊 全文搜索
GET /megacorp/employee/_search
{
“query” : {
“match” : {
“about” : “rock climbing”
}
}
}
查询 about 包含完整短语“rock climbing”的员工。
GET /megacorp/employee/_search
{
“query” : {
“match_phrase” : {
“about” : “rock climbing”
}
}
}

高亮查询结果
GET /megacorp/employee/_search
{
“query”:{
“match_phrase”:{
“about”:”rock climbing”
}
},
“highlight”:{
“fields”:{
“about” : {}
}
}
}

牛逼的聚合功能
GET /megacorp/employee/_search
{
“aggs”: {
“all_interests”: {
“terms”: { “field”: “interests” }
}
}
}

结果
“aggregations”: {
“all_interests”: {
“doc_count_error_upper_bound”: 0,
“sum_other_doc_count”: 0,
“buckets”: [
{
“key”: “music”,
“doc_count”: 4
},
{
“key”: “sports”,
“doc_count”: 4
},
{
“key”: “forestry”,
“doc_count”: 3
}
]
}
}

自动把员工的爱好归类 比如 爬山 归类到运动!!聚合功能就是在分析数据

姓”Smith”的兴趣爱好最多的是
GET /megacorp/employee/_search
{
“query”: {
“match”: {
“last_name”: “smith”
}
},
“aggs”: {
“all_interests”: {
“terms”: {
“field”: “interests”
}
}
}
}

统计每种兴趣下职员的平均年龄
GET /megacorp/employee/_search
{
“aggs” : {
“all_interests” : {
“terms” : { “field” : “interests” },
“aggs” : {
“avg_age” : {
“avg” : { “field” : “age” }
}
}
}
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

merlin.feng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值