Elasticsearch 搜索简单api请求

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

返回:默认情况下,响应的hits部分包括匹配搜索条件的前10个文档
{
  "took" : 223,              //Elasticsearch运行查询所需的时间(以毫秒为单位)
  "timed_out" : false,        //搜索请求是否超时
  "_shards" : {                //搜索了多少个碎片,以及有多少碎片成功、失败或跳过的细分。
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,            //找到多少个匹配的文档
      "relation" : “eq”//
    },
    "max_score" : null,            //找到的最相关文件的分数
    "hits" : [
      {
        "_index" : "bank",
        "_type" : "_doc",
        "_id" : "0",
        "_score" : null,        //文档的相关性得分(在使用match all时不适用)
        "_source" : {
          "account_number" : 0,
          "balance" : 16623,
          "firstname" : "Bradshaw",
          "lastname" : "Mckenzie",
          "age" : 29,
          "gender" : "F",
          "address" : "244 Columbus Place",
          "employer" : "Euron",
          "email" : "bradshawmckenzie@euron.com",
          "city" : "Hobucken",
          "state" : "CO"
        },
        "sort" : [            //文档的排序位置(不按相关性得分排序时)
          0
        ]
      },


每个搜索请求都是独立的:Elasticsearch不会在请求中维护任何状态信息。 要翻阅搜索结果,请在请求中指定from和size参数。
GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ],
  "from": 10,
  "size": 10
}

要在字段中搜索特定术语,可以使用匹配查询。

GET /bank/_search
{
  "query": { "match": { "address": "mill lane" } }
}

要执行短语搜索而不是匹配单个术语.

GET /bank/_search
{
  "query": { "match_phrase": { "address": "mill lane" } }
}

要构造更复杂的查询,可以使用bool查询来组合多个查询条件。 您可以根据需要(必须匹配),期望(应匹配)或不期望(必须不匹配)指定条件。

例如,以下请求在银行索引中搜索属于40岁客户的帐户,但不包括居住在爱达荷州(ID)的任何人:

GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}

例如,以下请求使用范围filter将结果限制为余额在20,000美元到30,000美元(含)之间的帐户。

GET /bank/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

例如,以下请求使用terms汇总将银行索引中的所有帐户按状态分组,并以降序返回帐户数量最多的十个州:

curl -X GET "10.10.30.115:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,     //这里的size是控制返回值中hits结合中的条数
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      }
    }
  }
}'


返回:
{
  "took" : 46,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1000,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]  //是这个地方的条数
  },
  "aggregations" : {
    "group_by_state" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 743,
      "buckets" : [                -
        {                           | 
          "key" : "TX”,             |
          "doc_count" : 30          |
        },                          |  //这里只显示统计数量
        {                           | 
          "key" : "MD”,             |
          "doc_count" : 28          |  
        },                         -   
        。。。
      ]
    }
  }
}

您可以组合聚合以构建更复杂的数据汇总。 例如,以下请求在前一个group_by_state聚合内嵌套avg聚合,以计算每个州的平均帐户余额。
curl -X GET "10.10.30.115:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword"
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}'

您可以通过在terms聚合中指定顺序来使用嵌套聚合的结果进行排序,而不必按计数对结果进行排序:

curl -X GET "10.10.30.115:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "group_by_state": {
      "terms": {
        "field": "state.keyword",
        "order": {
          "average_balance": "asc"
        }
      },
      "aggs": {
        "average_balance": {
          "avg": {
            "field": "balance"
          }
        }
      }
    }
  }
}'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值