ElasticSearch - 检索

官方文档

一、SearchAPI

ES支持两种基本方式检索 :

  • 一个是通过使用 REST request URI 发送搜索参数(uri+检索参数)
  • 另一个是通过使用 REST request body 来发送它们(uri+请求体)

1)、检索信息

一切检索从_search开始

GET bank/_search #检索bank下所有信息,包括type和docs
GET bank/_search?q=*&sort=account_number:asc 请求参数方式检索

响应结果解释:

took:Elasticsearch 执行搜索的时间(毫秒)
time_out:告诉我们搜索是否超时
_shards:告诉我们多少个分片被搜索了,以及统计了成功/失败的搜索分片
hits:搜索结果
hits.total:搜索结果
hits.hits:实际的搜索结果数组(默认为前 10 的文档)
sort:结果的排序 key(键)(没有则按 score 排序)
score 和 max_score:相关性得分和最高得分(全文检索用)

uri+请求体进行检索

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "account_number": {
        "order": "desc"
      }
    }
  ]
}

HTTP客户端工具(POSTMAN),get请求不能携带请求体,我们变为post也是一样的
我们 POST 一个 JSON 风格的查询请求体到 _search API。
需要了解,一旦搜索的结果被返回,ElasticSearch 就完成了这次请求,并且不会维护任何服务端的资源或者结果的 cursor(游标)

二、Query DSL

1)、基本语法格式

ElasticSearch 提供了一个可以执行查询的 Json 风格的 DSL(domain-specific language 领域特定语言)。这个被称为 Query DSL。该查询语言非常全面,并且刚开始的时候感觉有点复杂,真正学好它的方法是从一些基础的示例开始的。

  • 一个查询语句 的典型结构
{
    QUERY_NAME: {
        ARGUMENT: VALUE,
        ARGUMENT: VALUE,...
    }
}
  • 如果是针对某个字段,那么它的结构如下:
{
    QUERY_NAME: {
        FIELD_NAME: {
            ARGUMENT: VALUE,
            ARGUMENT: VALUE,...
        }
    }
}

示例:

GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 5,
  "sort": [
    {
      "account_number": {
        "order": "desc"
      }
    }
  ]
}
  • query 定义如何查询,
  • match_all 查询类型【代表查询所有的所有】,es中可以在query中组合非常多的查询类型完成复杂查询
  • 除了 query 参数之外,我们也可以传递其它的参数以改变查询结果。如sort,size
  • from+size限定,完成分页功能
  • sort排序,多字段排序,会在前序字段相等时后续字段内部排序,否则以前序为准

2)、_source【返回部分字段】

# 返回age、balance字段,其余不返回
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 5,
  "_source": ["age","balance"]
}

# 返回name、address字段,age、birthday字段不返回,一般都includes与excludes二选一使用,不会一起使用,一起使用没有意义。
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 5,
  "_source": {
    "includes": ["name" ,"address"],
    "excludes": ["age" , "birthday"]
  }
}

# 只返回_source下的数据,_index、_type等不返回
GET bank/_search
{
  "query": {
    "match_all": {}
  },
  "from": 0,
  "size": 5,
  "_source"
}

3)、match【匹配查询】

  • 基本类型(非字符串),精确匹配,
#返回account_number=20的所有记录
GET bank/_search
{
  "query": {
    "match": {
      "account_number": "20"
    }
  }
}
  • 字符串,全文检索
#返回address中包含mill单词的所有记录,并且每条记录有相关性得分
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  }
}
  • 字符串,多个单词(分词+全文检索)
#返回address中包含mill或者road或者mill road的所有记录,并给出相关性得分
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill road"
    }
  }
}

4)、match_phrase【短语匹配】

将需要匹配的值当成一个整体单词(不分词)进行检索

#返回address中包含mill road的所有记录,并给出相关性得分
GET bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill road"
    }
  }
}

5)、multi_match【多字段匹配】

#返回state字段或者address字段包含mill的所有记录
GET bank/_search
{
  "query": {
    "multi_match": {
      "query": "mill",
      "fields": ["state","address"]
    }
  }
}

6)、bool【复合查询】

复合语句可以合并 任何 其它查询语句,包括复合语句,了解这一点是很重要的。这就意味着,复合语句之间可以互相嵌套,可以表达非常复杂的逻辑。

  • must:必须达到must列举的所有条件
GET bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "address": "mill" } },
        { "match": { "gender": "M" } }
      ]
    }
  }
}
  • should:应该达到should列举的条件,如果达到会增加相关文档的评分,并不会改变查询的结果。如果query中只有should且只有一种匹配规则,那么should的条件就会被作为默认匹配条件而去改变查询结果
GET bank/_search
{
  "query": {
    "bool": {
      "must": [ 
		{ "match": { "address": "mill" } },
        { "match": { "gender": "M" } }
      ],
      "should": [
        {"match": { "address": "lane" }}
      ]
    }
  }
}
  • must_not必须不是指定的情况
GET bank/_search
{
  "query": {
    "bool": {
      "must": [ 
        { "match": { "address": "mill" } },
        { "match_phrase": { "gender": "M" } }
      ],
      "must_not": [
        {"match": { "state": "IL" }}
      ],
      "should": [
        { "range": {
            "age": {
              "gte": 20,
              "lte": 30
            }
	      }
        }
      ]
    }
  }
}

address包含mill,并且gender是M,如果address里面有lane最好不过,但是email必须不包含baluba.com

7)、filter【结果过滤】

并不是所有的查询都需要产生分数,特别是那些仅用于 “filtering”(过滤)的文档。为了不计算分数 Elasticsearch 会自动检查场景并且优化查询的执行。

GET bank/_search
{
  "query": {
     "bool": {
       "must": [
         {"match": { "address": "mill"}}
       ],
       "filter": {
         "range": {
           "balance": {
             "gte": 10000,
             "lte": 20000
           }
         }
       }
     }
  }
}

8)、aggregations【执行聚合】

聚合提供了从数据中分组和提取数据的能力。最简单的聚合方法大致等于 SQL GROUP BY 和 SQL 聚合函数。在 Elasticsearch 中,您有执行搜索返回 hits(命中结果),并且同时返回聚合结果,把一个响应中的所有 hits(命中结果)分隔开的能力。这是非常强大且有效的,您可以执行查询和多个聚合,并且在一次使用中得到各自的(任何一个的)返回结果,使用一次简洁和简化的 API 来避免网络往返。

  • 搜索address中包含mill的所有人的年龄分布以及平均年龄,但不显示这些人的详情。
GET bank/_search
{
  "query": {
    "match": {
      "address": "mill"
    }
  },
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "age"
      }
    },
    "avg_age": {
      "avg": {
        "field": "age"
      }
    }
  },
  "size": 0
}

说明:

size:0    不显示搜索数据
aggs:   执行聚合。聚合语法如下
"aggs": {
    "aggs_name": {  这次聚合的名字,方便展示在结果集中
      "AGG_TYPE": {  聚合的类型(avg,term,terms)
      
      }  
    }
  },
  • 按照年龄聚合,并且请求这些年龄段的这些人的平均薪资
GET bank/account/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "age_avg": {
      "terms": {
        "field": "age",
        "size": 1000
      },
      "aggs": {
        "banlances_avg": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  },
  "size": 1000
}
  • 查出所有年龄分布,并且这些年龄段中M的平均薪资和F的平均薪资以及这个年龄段的总体平均薪资
GET bank/account/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "age_agg": {
      "terms": {
        "field": "age",
        "size": 100
      },
      "aggs": {
        "gender_agg": {
          "terms": {
            "field": "gender.keyword",
            "size": 100
          },
          "aggs": {
            "balance_avg": {
              "avg": {
                "field": "balance"
              }
            }
          }
        },
        "balance_avg":{
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  },
  "size": 1000
}
  • 查出bank中每个售价区间(0-19999,20000-39999,40000-59999,600…)内汽车的销量。和每个售价区间内汽车所带来的收入。
GET /cars/transactions/_search
{
   "size" : 0,
   "aggs":{
      "price":{
         "histogram":{ 
            "field": "price",
            "interval": 20000
         },
         "aggs":{
            "revenue": {
               "sum": { 
                 "field" : "price"
               }
             }
         }
      }
   }
}
  • 查出bank中最受欢迎 10 种汽车以及它们的平均售价、标准差等这些信息。
#extended_stats函数,可以返回常见的count、sum、avg、min、max等信息
GET /cars/transactions/_search
{
  "size" : 0,
  "aggs": {
    "makes": {
      "terms": {
        "field": "make",
        "size": 10
      },
      "aggs": {
        "stats": {
          "extended_stats": {
            "field": "price"
          }
        }
      }
    }
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值