报表首页投稿(暂停使用,暂停投稿)

Elasticsearch入门语法

2016-05-23  本文已影响23959人  MacSam

接着上一篇的安装后,笔者来介绍下es的简单用法

首先是一些术语和基本的概念,这里的数据借鉴了es的中文文档
让我们建立一个员工目录
假设我们刚好在Megacorp工作,这时人力资源部门出于某种目的需要让我们创建一个员工目录,这个目录用于促进人文关怀和用于实时协同工作,所以它有以下不同的需求:
数据能够包含多个值的标签、数字和纯文本。
检索任何员工的所有信息。
支持结构化搜索,例如查找30岁以上的员工。
支持简单的全文搜索和更复杂的短语(phrase)搜索
高亮搜索结果中的关键字
能够利用图表管理分析这些数据

PUT /megacorp/employee/1
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}

上图代表的意思是在一个叫megacorp的索引(index)中建立了一个叫
employee的类型(type),它的ID是1,整个json部分代表的是这个id为1的文档(document).
同样的,我们再次插入两个document.

PUT /megacorp/employee/2
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}
PUT /megacorp/employee/3
{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}
GET /megacorp/employee/1
{
  "_index" :   "megacorp",
  "_type" :    "employee",
  "_id" :      "1",
  "_version" : 1,
  "found" :    true,
  "_source" :  {
      "first_name" :  "John",
      "last_name" :   "Smith",
      "age" :         25,
      "about" :       "I love to go rock climbing",
      "interests":  [ "sports", "music" ]
    }
}

_index--索引名称
_type--类型名称
_id--id(这个id可以自己指定也可以自动生成)
_version--版本号,每次改动会+1
found--true表示在document存在
_source--document的全部内容

GET /megacorp/employee/_search?q=last_name:Smith
{
   "took":      6,                                    //花费的时间(毫秒)
   "timed_out": false,                                //是否超时
    "_shards": { ... },                               //分片相关,后续介绍
   "hits": {                                          //存放所有命中
      "total":      2,                                //命中document的个数
      "max_score":  0.30685282,                       //最高命中分
      "hits": [
         {
            ...
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            }
         },
         {
            ...
            "_source": {
               "first_name":  "Jane",
               "last_name":   "Smith",
               "age":         32,
               "about":       "I like to collect rock albums",
               "interests": [ "music" ]
            }
         }
      ]
   }
}
GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}
GET /megacorp/employee/_search
{
    "query" : {
        "filtered" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            },
            "query" : {
                "match" : {
                    "last_name" : "Smith" 
                }
            }
        }
    }
}
GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}
GET /megacorp/employee/_search
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}
{
   ...
   "hits": { ... },
   "aggregations": {
      "all_interests": {
         "buckets": [
            {
               "key":       "music",
               "doc_count": 2
            },
            {
               "key":       "forestry",
               "doc_count": 1
            },
            {
               "key":       "sports",
               "doc_count": 1
            }
         ]
      }
   }
}
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" }
                }
            }
        }
    }
}
...
 "all_interests": {
     "buckets": [
        {
           "key": "music",
           "doc_count": 2,
           "avg_age": {
              "value": 28.5
           }
        },
        {
           "key": "forestry",
           "doc_count": 1,
           "avg_age": {
              "value": 35
           }
        },
        {
           "key": "sports",
           "doc_count": 1,
           "avg_age": {
              "value": 25
           }
        }
     ]
  }

当然这只是一些皮毛,为了保持简短,还有很多的特性未提及——像推荐、定位、渗透、模糊以及部分匹配等,这里是简单的介绍了如何创建及简单的查询
下篇预告 : ES的数据CURD

上一篇下一篇

猜你喜欢

热点阅读