Elasticsearch2.4使用入门

本文介绍如何使用Elasticsearch进行数据的存储、检索及高级查询,包括创建索引、文档操作、复杂查询、聚合分析等,并提供了丰富的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 es和关系型数据库的关系:

 

       在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)中,我们可以画一些简单的对比图来类比传统关系型数据库:2

 

       RelationalDB -> Databases -> Tables -> Rows -> Columns

       Elasticsearch-> Indices   -> Types  -> Documents -> Fields

 

2创建

创建customer的index:curl -XPUT 'cdh1:9200/customer?pretty'

      

查询所有的索引:curl 'cdh1:9200/_cat/indices?v'

 

 

put数据到customer:type为external,document为1

curl -XPUT'cdh1:9200/customer/external/1?pretty' -d '

{

       "first_name":"John",

       "last_name": "Smith",

       "age": 25,

       "about": "I love to go rock climbing",

       "interests":[ "sports", "music" ]

}

 

curl -XPUT'cdh1:9200/customer/external/2?pretty' -d '

{

   "first_name" : "Jane",

   "last_name" :  "Smith",

   "age" :         32,

   "about" :       "Ilike to collect rock albums",

   "interests":  ["music" ]

}

 

curl -XPUT'cdh1:9200/customer/external/3?pretty' -d '

{

   "first_name" : "Douglas",

   "last_name" :  "Fir",

   "age" :         35,

   "about":        "Ilike to build cabinets",

   "interests":  ["forestry" ]

}

 

3删除

       删除:curl -XDELETE 'cdh1:9200/customer/external/1'

 

4更新

       更新:

       curl-XPOST 'cdh1:9200/customer/external/1/_update' -d '

       {

         "first_name": "John"

         "last_name" :   "Smith",

         "age" :         25,

         "about" :       "I love to go rock climbing",

         "interests":  [ "sports", "music" ]

       }

 

 

5查询搜索

查询:curl -XGET'cdh1:9200/customer/external/3?pretty'

 

简单搜索(返回前十条记录):curl -XGET'cdh1:9200/customer/external/_search'

查询字符串(query string)搜索:curl -XGET 'cdh1:9200/customer/external/_search?q=last_name:Smith'

 

es提供 DSL 查询(Query DSL):

curl -XPOST'cdh1:9200/customer/external/_search?pretty' -d '

{  

       "query": {"match" : {"last_name" : "Smith"}}

}

过滤器:

curl -XPOST'cdh1:9200/customer/external/_search?pretty' -d '

{  

       "query": {"filtered" : {"filter" : {"range" :{"age" : {"gt" : 30}}},"query" :{"match" : {"last_name" : "Smith"}}}}

}’

 

全文搜索:

curl -XPOST'cdh1:9200/customer/external/_search?pretty' -d '

{

   "query" : {

       "match" : {

           "about" : "rock climbing"

       }

    }

}’

短语搜索(精确搜索):

curl -XPOST'cdh1:9200/customer/external/_search?pretty' -d '

{

   "query" : {

       "match_phrase" : {

           "about" : "rock albums"

       }

    }

}’

高亮搜索:

curl -XPOST 'cdh1:9200/customer/external/_search?pretty'-d '

{

   "query" : {

       "match_phrase" : {

           "about" : "rock albums"

       }

   },

       "highlight":{

       "fields" : {

           "about" : {}

       }

    }

}’

 

6聚合(aggregations)

它允许你在数据上生成复杂的分析统计。它很像SQL中的GROUP BY但是功能更强大。

6.1 Terms聚合

找到所有customer中最大的共同点(兴趣爱好)是什么:

curl -XPOST'cdh1:9200/customer/external/_search?pretty' -d '

{

 "aggs": {

   "all_interests": {

     "terms": { "field": "interests" }

    }

  }

}’

 

如果我们想知道所有姓"Smith"的人最大的共同点(兴趣爱好):

curl -XPOST'cdh1:9200/customer/external/_search?pretty' -d '

{

 "query": {

   "match": {

     "last_name": "smith"

    }

  },

 "aggs": {

   "all_interests": {

     "terms": {

       "field": "interests"

     }

    }

  }

}’

 

聚合也允许分级汇总。例如,让我们统计每种兴趣下职员的平均年龄:

curl -XPOST'cdh1:9200/customer/external/_search?pretty' -d '

{

   "aggs" : {

       "all_interests" : {

           "terms" : { "field" : "interests" },

           "aggs" : {

                "avg_age" : {

                    "avg" : { "field" :"age" }

                }

           }

       }

    }

}’

 

数据的不确定性

使用terms聚合,结果可能带有一定的偏差与错误性。

比如:

我们想要获取name字段中出现频率最高的前5个。

此时,客户端向ES发送聚合请求,主节点接收到请求后,会向每个独立的分片发送该请求。
分片独立的计算自己分片上的前5name,然后返回。当所有的分片结果都返回后,在主节点进行结果的合并,再求出频率最高的前5个,返回给客户端。

这样就会造成一定的误差,比如最后返回的前5个中,有一个叫A的,有50个文档;B49但是由于每个分片独立的保存信息,信息的分布也是不确定的。有可能第一个分片中B的信息有2个,但是没有排到前5,所以没有在最后合并的结果中出现。这就导致B的总数少计算了2,本来可能排到第一位,却排到了A的后面。

size与shard_size

为了改善上面的问题,就可以使用sizeshard_size参数。

  • size参数规定了最后返回的term个数(默认是10个)
  • shard_size参数规定了每个分片上返回的个数
  • 如果shard_size小于size,那么分片也会按照size指定的个数计算

通过这两个参数,如果我们想要返回前5个,size=5;shard_size可以设置大于5,这样每个分片返回的词条信息就会增多,相应的误差几率也会减小。

 

更多请看:http://www.cnblogs.com/xing901022/p/4947436.html

 

6.2 Metrics 聚合

Max、取最大值

min、取最小值

sum、取和

avg、取平均值

stats、同时求出count、min、max、sum、avg

top、

cardinality、

percentile

求最大年龄

{

       "aggs": {

              "max_age": {

                     "field": "age"

              }

       }

}

求一共有多少种last_name

{

       "aggs": {

              "name_count": {

                     "cardinality" : {

                "field" :"last_name"

           }

              }

       }

}

6.3 Pipline聚合

案例:每天12个时间点,每个时间点多个值,求每天的每个时间点最大值之和

案例数据:


{

       "aggs": {

              "sum_time_max": {

                     "terms": {

                            "field": "time"

                     },

                     "aggs":{

                            "the_max":{

                                   "max":{"field" : "val" }

                            }

                     }

              },

              "sum_day_max": {

                     "sum_bucket":{

                            "buckets_path": "sum_time_max>the_max"

                     }

              }

       }

}

 

6.4 Children 聚合

案例:试题库,由问题和答案构成

1、创建索引库

curl http://cdh1:9200/stack -d '

{

 "mappings": {

   "qustions": {},

   "answer": {

     "_parent": {

       "type": "qustions"

     }

    }

  }

}'

2、添加问题文档

curl http://cdh1:9200/stack/qustions/1 -d '

{

   "body": "<p>I have Windows 2003 server and i boughta new Windows 2008 server...",

   "title": "Whats the best way to file transfer my sitefrom server to a newer one?",

   "tags": [

       "windows-server-2003",

       "windows-server-2008",

       "file-transfer"

    ]

}'

3、添加答案文档

curl http://cdh1:9200/stack/answer/1 -d '

{

   "owner": {

       "location": "Norfolk, United Kingdom",

       "display_name": "Sam",

       "id": 48

   },

    "body":"<p>Unfortunately your pretty much limited to FTP...",

   "creation_date": "2009-05-04T13:45:37.030"

}'

 

4、查询

{

 "aggs": {

   "top-tags": {

     "terms": {

       "field": "tags",

       "size": 10

     },

     "aggs": {

       "to-answers": {

         "children": {

           "type": "answer"

         },

         "aggs": {

           "top-names": {

              "terms": {

                "field":"owner.display_name",

                "size": 10

              }

            }

         }

       }

     }

    }

  }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值