ElasticSearch7.3.2-Rest实战指南-Search APIs

1、Search APIs

Most search APIs are multi-index, with the exception of the Explain API endpoints.

1.1、 Search

Returns search hits that match the query defined in the request.

  • Request
GET /<index>/_search
GET /_search
POST /<index>/_search
POST /_search
  • Example

  • query parameter

curl -XGET 'http://10.143.228.25:9200/student/_search?q=age:20&pretty'

# 查询指定返回字段
curl -XGET 'http://10.143.228.25:9200/student/_search?_source=name,age&pretty'
{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "oFmkQHIBrm6ikcYVWSpb",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "门捷列夫",
          "age" : 20,
          "address" : "俄罗斯",
          "birthday" : "1990-02-03"
        }
      },
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "oVmkQHIBrm6ikcYVXCpE",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "门捷列夫",
          "age" : 20,
          "address" : "俄罗斯",
          "birthday" : "1990-02-03"
        }
      }
    ]
  }
}
  • request body
curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{
    "query" : {
        "term" : { "age" : "20" }
    }
}
'

response示例:

{
  "took" : 10,
  "timed_out" : false,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "oFmkQHIBrm6ikcYVWSpb",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "门捷列夫",
          "age" : 20,
          "address" : "俄罗斯",
          "birthday" : "1990-02-03"
        }
      },
      {
        "_index" : "student",
        "_type" : "_doc",
        "_id" : "oVmkQHIBrm6ikcYVXCpE",
        "_score" : 1.0,
        "_source" : {
          "id" : 1,
          "name" : "门捷列夫",
          "age" : 20,
          "address" : "俄罗斯",
          "birthday" : "1990-02-03"
        }
      }
    ]
  }
}

Doc Value Fields

curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{
    "query" : {
        "term" : { "age" : "20" }
    },
    "_source":["name","age"]
}'

From / Size

curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{
    "from" : 0, "size" : 10,
    "query" : {
        "term" : { "age" : "20" }
    },
    "_source":["name","age"]
}'

Sort

curl -XPOST 'http://10.143.228.25:9200/student/_search?pretty' -H "Content-Type: application/json" -d'
{
    "from" : 0, "size" : 10,
    "query" : {
        "term" : { "age" : "20" }
    },
    "_source":["name","age"],
    "sort":[{"age":"asc"}]
}'

更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-index_.html

1.2、Request Body Search

Specifies search criteria as request body parameters.

  • Request
GET /<index>/_search
{
  "query": {<parameters>}
}
  • Example
curl -XGET 'http://10.143.228.25:9200/student/_doc/1?pretty'

response示例:

{
  "_index" : "student",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "id" : 1,
    "name" : "门捷列夫",
    "age" : 20,
    "address" : "俄罗斯",
    "birthday" : "1990-02-03"
  }
}
  • 返回指定字段
curl -XGET 'http://10.143.228.25:9200/student/_doc/1?pretty&_source=name,age'

response示例:

{
  "_index" : "student",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "门捷列夫",
    "age" : 20
  }
}
  • 使用bool(must、must_not、should)

filter可以实现对结果的过滤

curl -X GET "10.143.228.25:9200/student/_search?pretty" -H 'Content-Type: application/json' -d'
{
   "query": {
     "bool": {
       "must": [
         { "match": { "age": 20 } }
       ],
       "must_not": [
         { "match": { "address": "美国" } }
       ],
       "should": [
         { "match": { "age": 20 } }
       ],
       "filter": {
           "range": {
             "age": {
               "gte": 20,
               "lte": 30
             }
           }
       }
     }
   }
}
'

>更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-request-body.html


## 1.3、Count API

>Gets the number of matches for a search query.
 
- Request

```shell
GET /<index>/_count
  • Example

1、基于URI形式

curl -XGET 'http://10.143.228.25:9200/student/_count?q=age:20&pretty'

response示例:

{
  "count" : 4,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  }
}

2、基于request body形式

curl -XGET 'http://10.143.228.25:9200/student/_count?pretty' -H 'Content-Type: application/json' -d'
{
    "query" : {
        "term" : { "age" : 20 }
    }
}'

response示例:

{
  "count" : 4,
  "_shards" : {
    "total" : 3,
    "successful" : 3,
    "skipped" : 0,
    "failed" : 0
  }
}

更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-count.html

1.4、Search Shards API

Returns the indices and shards that a search request would be executed against.

  • Request
GET /<index>/_search_shards
  • Example
curl -XGET 'http://10.143.228.25:9200/student/_search_shards?pretty'

response示例:

{                                                                                                                                                       
  "nodes" : {                                                                                                                                           
    "fkdv_0G4SCW5YLqxkWL1qg" : {                                                                                                                        
      "name" : "node2",                                                                                                                                 
      "ephemeral_id" : "MBLiX3TKRJihyd7aiGLjnA",                                                                                                        
      "transport_address" : "10.143.228.26:9300",                                                                                                       
      "attributes" : {                                                                                                                                  
        "ml.machine_memory" : "8350584832",                                                                                                             
        "ml.max_open_jobs" : "20",                                                                                                                      
        "xpack.installed" : "true"                                                                                                                      
      }                                                                                                                                                 
    },                                                                                                                                                  
    "E6pmMAUdShurP50uAV3kIA" : {                                                                                                                        
      "name" : "node3",                                                                                                                                 
      "ephemeral_id" : "7eSlOGxnSw65qIEGktBaDQ",                                                                                                        
      "transport_address" : "10.143.228.198:9300",                                                                                                      
      "attributes" : {                                                                                                                                  
        "ml.machine_memory" : "8350584832",                                                                                                             
        "ml.max_open_jobs" : "20",                                                                                                                      
        "xpack.installed" : "true"                                                                                                                      
      }                                                                                                                                                 
    },                                                                                                                                                  
    "XiTKce8fST2vhReZvE-1dA" : {                                                                                                                        
      "name" : "node1",                                                                                                                                 
      "ephemeral_id" : "rQYF0SGZRWmNe23mXyKWEw",                                                                                                        
      "transport_address" : "10.143.228.25:9300",                                                                                                       
      "attributes" : {                                                                                                                                  
        "ml.machine_memory" : "8350576640",                                                                                                             
        "xpack.installed" : "true",                                                                                                                     
        "ml.max_open_jobs" : "20"                                                                                                                       
      }                                                                                                                                                 
    }                                                                                                                                                   
  },                                                                                                                                                    
  "indices" : {                                                                                                                                         
    "student" : { }                                                                                                                                     
  },                                                                                                                                                    
  "shards" : [                                                                                                                                          
    [                                                                                                                                                   
      {                                                                                                                                                 
        "state" : "STARTED",                                                                                                                            
        "primary" : false,                                                                                                                              
        "node" : "E6pmMAUdShurP50uAV3kIA",                                                                                                              
        "relocating_node" : null,                                                                                                                       
        "shard" : 0,                                                                                                                                    
        "index" : "student",                                                                                                                            
        "allocation_id" : {                                                                                                                             
          "id" : "3iTj8tTNSdiDjaUvBgb3gA"                                                                                                               
        }                                                                                                                                               
      },                                                                                                                                                
      {                                                                                                                                                 
        "state" : "STARTED",
        "primary" : false,
        "node" : "XiTKce8fST2vhReZvE-1dA",
        "relocating_node" : null,
        "shard" : 0,
        "index" : "student",
        "allocation_id" : {
          "id" : "rOYuBhe7ToqugfDwBifSEQ"
        }
      },
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "fkdv_0G4SCW5YLqxkWL1qg",
        "relocating_node" : null,
        "shard" : 0,
        "index" : "student",
        "allocation_id" : {
          "id" : "9jSRbREKS9eCFDrEKrOeOQ"
        }
      }
    ],
    [
      {
        "state" : "STARTED",
        "primary" : false,
        "node" : "fkdv_0G4SCW5YLqxkWL1qg",
        "relocating_node" : null,
        "shard" : 1,
        "index" : "student",
        "allocation_id" : {
          "id" : "erg_YsbmQPWQFrXAm_icpQ"
        }
      },
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "E6pmMAUdShurP50uAV3kIA",
        "relocating_node" : null,
        "shard" : 1,
        "index" : "student",
        "allocation_id" : {
          "id" : "Hvd9J2IcTmOPRESGtruAAQ"
        }
      },
      {
        "state" : "STARTED",
        "primary" : false,
        "node" : "XiTKce8fST2vhReZvE-1dA",
        "relocating_node" : null,
        "shard" : 1,
        "index" : "student",
        "allocation_id" : {
          "id" : "Wgl0mc6zQmG4qIwvLJMS8Q"
        }
      }
    ],
    [
      {
        "state" : "STARTED",
        "primary" : false,
        "node" : "fkdv_0G4SCW5YLqxkWL1qg",
        "relocating_node" : null,
        "shard" : 2,
        "index" : "student",
        "allocation_id" : {
          "id" : "T9dZf0nATfCKlN0EbDca6w"
        }
      },
      {
        "state" : "STARTED",
        "primary" : false,
        "node" : "E6pmMAUdShurP50uAV3kIA",
        "relocating_node" : null,
        "shard" : 2,
        "index" : "student",
        "allocation_id" : {
          "id" : "CRmJU1_2RBegt8mPZXQt-A"
        }
      },
      {
        "state" : "STARTED",
        "primary" : true,
        "node" : "XiTKce8fST2vhReZvE-1dA",
        "relocating_node" : null,
        "shard" : 2,
        "index" : "student",
        "allocation_id" : {
          "id" : "mTGFJhjmS0yIbsioZRxnsg"
        }
      }
    ]
  ]
}

更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-shards.html

3、More

https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search.html

4、elasticsearch-header插件

elasticsearch-header插件就像mysql之navicat或者sqlYog,一个客户端插件,如上的一些curl命令可以通过界面操作,提高易用性。

下载地址:https://gitee.com/suze/elasticsearch-head

安装步骤详见源码的readme即可。

部分截图:
数据浏览
基本查询
概览

下面的是我的公众号二维码图片,欢迎关注。
秋夜无霜

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值