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
{
"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
GET /megacorp/employee/_search?q=last_name:Smith
GET /megacorp/employee/_search
{
"query" : {
"match" : {
"last_name" : "Smith"
}
}
}
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
}
}
]
}