Elasticsearch入门语法


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

  • 在Elasticsearch中存储数据的行为就叫做索引(indexing),不过在索引之前,我们需要明确数据应该存储在哪里。在Elasticsearch中,文档归属于一种类型(type),而这些类型存在于索引(index)中,这样说可能不是很好理解,这里贴出一张es和关系型数据库的对应图,就懂了
     
    Paste_Image.png
  • 第一步:创建员工目录,为每个员工的文档(document)建立索引,每个文档包含了相应员工的所有信息。
    1. 每个文档的类型为employee
    2. employee类型归属于索引megacorp
    3. megacorp索引存储在Elasticsearch集群中。
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" ]
}
  • 搜索,上一步中插入了3个document,我们可以通过http get来获取它们的信息.
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的全部内容

  • 轻量级搜索,查询字符串(query string)
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" ]
            }
         }
      ]
   }
}
  • Elasticsearch提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询。DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。我们可以这样表示之前关于“Smith”的查询:
GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}
  • 更复杂的搜索,使用过滤器(filter)来实现sql中where的效果,比如:你想要搜索一个叫Smith,且年龄大于30的员工,可以这么检索.
GET /megacorp/employee/_search
{
    "query" : {
        "filtered" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            },
            "query" : {
                "match" : {
                    "last_name" : "Smith" 
                }
            }
        }
    }
}
  • 短语搜索(phrases),match_phrase与match的区别在于,前者会把rock climbing(搜索条件)作为一个整体,而后者会命中rock balabala climbing
GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}
  • 聚合(aggregations),它允许你在数据上生成复杂的分析统计,类似于sql中的group by.
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
            }
         ]
      }
   }
}
  • 如果我们想知道所有姓"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" }
                }
            }
        }
    }
}
...
 "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
           }
        }
     ]
  }

posted @ 2018-01-22 11:44  Jenkin.K  阅读(103)  评论(0编辑  收藏  举报