Elasticsearch 5 入门

好风 发表于 2016-12-05T03:27:15.125130Z
引用地址:https://plus.ooclab.com/note/article/1312

基本操作

版本

GET /

Request Body:

{
  "name": "AVFDllT",
  "cluster_name": "elasticsearch",
  "cluster_uuid": "dR_vZK6fQtOsJDomuCjGyg",
  "version": {
    "number": "5.0.2",
    "build_hash": "f6b4951",
    "build_date": "2016-11-24T10:07:18.101Z",
    "build_snapshot": false,
    "lucene_version": "6.2.1"
  },
  "tagline": "You Know, for Search"
}

创建 Index, 插入 document

PUT /megacorp/employee/1

Request Body:

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

PUT /megacorp/employee/2

Request Body:

{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}

PUT /megacorp/employee/3

Request Body:

{
    "first_name" :  "Douglas",
    "last_name" :   "Fir",
    "age" :         35,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}

查询 & 搜索

GET /megacorp/employee/1

Response Body:

{
  "_index": "megacorp",
  "_type": "employee",
  "_id": "1",
  "_version": 4,
  "found": true,
  "_source": {
    "first_name": "John",
    "last_name": "Smith",
    "age": 25,
    "about": "I love to go rock climbing",
    "interests": [
      "sports",
      "music"
    ]
  }
}

简单搜索

GET /megacorp/employee/_search
GET /megacorp/employee/_search?q=last_name:Smith

Query DSL 搜索

POST /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}

条件查询

POST /megacorp/employee/_search
{
    "query" : {
        "bool" : {
            "filter" : {
                "range" : {
                    "age" : { "gt" : 30 }
                }
            },
            "must" : {
                "match" : {
                    "last_name" : "Smith"
                }
            }
        }
    }
}

全文搜索

简单相关性搜索:

POST /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

短语匹配:

POST /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}

高亮搜索结果:

POST /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}

汇总 (aggregations)

这个功能有些类似于 SQL 中的 GROUP BY,但是要比它更加强大。

POST /megacorp/employee/_search
{
  "aggs": {
    "all_interests": {
      "terms": { "field": "interests" }
    }
  }
}

btw, 如果上面查询出现类似错误 "Fielddata is disabled on text fields by default. Set fielddata=true on [interests] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory." 请通过下面方法,设置 interests 字段属性 fielddata 为 true 。

PUT /megacorp/_mapping/employee
{
  "properties": {
    "interests": { 
      "type":     "text",
      "fielddata": true
    }
  }
}