ES语法&官方文档链接

ES语法

查询

  • 请求
GET _search 
{
    "size":1,
    "query": {
        "bool": {
            "filter":{
              "term":{
                "translateId":45
              }
            }
        }
    }
}
  • 返回
{
  "took": 100,
  "timed_out": false,
  "_shards": {
    "total": 3,
    "successful": 3,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 6,
    "max_score": 0,
    "hits": [
      {
        "_index": "osd_translate_result",
        "_type": "osd_translate_result",
        "_id": "yJ2RvXMBvHD4Onc7CfQJ",
        "_score": 0,
        "_source": {
          "wordCount": 7,
          "isActive": true,
          "datachangeCreateuserid": "S75011",
          "datachangeLastuserid": "S71567",
          "datachangeCreatetime": "2018-10-24T16:51:57",
          "datachangeLasttime": "2018-10-24T17:04:48",
          "batchNo": null,
          "remark": null,
          "translateId": 45,
          "languageCode": "KR",
          "translateResult": "주행거리 무제한",
          "md5": "c518e645deefc592ea30538a29f18152"
        }
      }
    ]
  }
}
  • 返回结果分析
    • took:本次操作花费的时间,单位为毫秒
    • time_out:请求是否超时
    • _shards:说明本次操作共搜索了哪些分片
    • hits:搜索命中的记录
    • hits.total:符合条件的文档总数hits.hits:匹配度较高的前N个文档
    • hits.max_score:文档匹配得分,这里为最高分
    • _score:每个文档都有一个匹配得分,按照降序排列
    • _source:显示了文档的原始内容

DSL语法

集群中文档的数量

curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}

term

  • term主要用于精确匹配哪些值,比如数字,日期,布尔值或not_analyzed的字符值。
{ 
  "query": { 
    "term": { 
      "title": "内蒙古" 
    } 
 }

terms过滤

  • terms跟term有点类似,但terms允许指定多个匹配条件。如果某个字段指定了多个值,那么document需要一起去做匹配。
{
  "query": {
    "terms": {
      "title": [
        "内蒙古",
        "黑龙江"
      ]
    }
  }
}

range

  • range过滤允许我们按照指定范围查找一批数据
{
"query":{
  "range": {
    "pubTime": {
      "gt": "2017-06-25",
      "lt": "2017-07-01"
    	}
  	}
	}
}
  • gt 大于;gte 大于等于;lt 小于;lte 小于等于

exists和missing

  • exists和missing过滤可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的IS_NULL条件。
  • 这两个过滤只是针对已经查出一批数据来,但是想区分出某个字段是否存在的时候使用。
{
    "exists":{
        "field":"title"
    }
}

bool过滤

  • bool过滤可以用来合并多个过滤条件查询结果的布尔逻辑,它包含以下操作符
    • must,多个查询条件的完全匹配,相等于and
    • must_not,多个查询条件的相反匹配,相当于not
    • should,至少有一个查询条件匹配,相当于or
{
    "bool":{
        "must":{
            "term":{
                "folder":"inbox"
            }
        },
        "must_not":{
            "term":{
                "tag":"spam"
            }
        },
        "should":[
            {
                "term":{
                    "starred":true
                }
            },
            {
                "term":{
                    "unread":true
                }
            }
        ]
    }
}

PUT操作

curl -X PUT "localhost:9200/megacorp/employee/1?pretty" -H 'Content-Type: application/json' -d'
{
    "first_name" : "John",
    "last_name" :  "Smith",
    "age" :        25,
    "about" :      "I love to go rock climbing",
    "interests": [ "sports", "music" ]
}
  • megacorp-索引名称
  • employee-类型名称
  • 1-特定雇员的id

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" ]
  }
}
  • 获取所有数据
GET /megacorp/employee/_search
  • 获取姓氏为Smith的雇员
GET /megacorp/employee/_search?q=last_name:Smith
  • 使用DSL构建一个JSON请求
GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}
  • 搜索姓氏为Smith的员工且年龄大于30
GET /megacorp/employee/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}

全文搜索

  • 搜索所有喜欢攀岩的员工
GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}

短语搜索

  • 找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者短短语。
  • 比如,我们想执行这样一个查询,仅匹配同时包含rock和climbing,并且二者以短语"rock climbing"的形式紧挨着的雇员记录
GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    }
}

高亮搜索

  • 许多应用都倾向于在每个搜索结果中高亮部分文本片段,以便让用户知道为何该文档符合查询条件。
  • 增加一个highlight参数
GET /megacorp/employee/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "rock climbing"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}
  • 结果
{
   ...
   "hits": {
      "total":      1,
      "max_score":  0.23013961,
      "hits": [
         {
            ...
            "_score":         0.23013961,
            "_source": {
               "first_name":  "John",
               "last_name":   "Smith",
               "age":         25,
               "about":       "I love to go rock climbing",
               "interests": [ "sports", "music" ]
            },
            "highlight": {
               "about": [
                  "I love to go <em>rock</em> <em>climbing</em>" 
               ]
            }
         }
      ]
   }
}

分析

  • Elasticsearch有一个功能叫聚合,允许我们基于数据生成一些精细的分析结果。聚合与SQL中的GROUP BY类似但更强大。
  • 挖掘出员工中最受欢迎的兴趣爱好
GET /megacorp/employee/_search
{
  "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
           }
        }
     ]
  }
  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值