GET/MGET

作者: 潘大的笔记 | 来源:发表于2019-03-01 18:35 被阅读0次

    GET

    返回部分字段。参数_source带上对应字段

    GET /website/blog/123?_source=title,text
    

    只需要_source字段,不需要任何元数据

    GET /website/blog/123/_source
    

    MGET

    mget要求有一个docs数组作为参数,每个元素包含需要检索文档的元数据,如果想检索特定字段,可以通过_source参数来指定这些字段的名字

    GET /_mget
    {
       "docs" : [
          {
             "_index" : "website",
             "_type" :  "blog",
             "_id" :    2
          },
          {
             "_index" : "website",
             "_type" :  "pageviews",
             "_id" :    1,
             "_source": "views"
          }
       ]
    }
    

    返回仍然是docs数组

    {
       "docs" : [
          {
             "_index" :   "website",
             "_id" :      "2",
             "_type" :    "blog",
             "found" :    true,
             "_source" : {
                "text" :  "This is a piece of cake...",
                "title" : "My first external blog entry"
             },
             "_version" : 10
          },
          {
             "_index" :   "website",
             "_id" :      "1",
             "_type" :    "pageviews",
             "found" :    true,
             "_version" : 2,
             "_source" : {
                "views" : 2
             }
          }
       ]
    }
    

    如果想检索的数据都在相同的_index中,甚至_type中,可以在URL中默认指定。也可以在通过单独请求覆盖

    GET /website/blog/_mget
    {
       "docs" : [
          { "_id" : 2 },
          { "_type" : "pageviews", "_id" :   1 }
       ]
    }
    

    也可以只传一个ids数组,而不是整个docs

    GET /website/blog/_mget
    {
       "ids" : [ "2", "1" ]
    }
    

    文档没有找不到HTTP状态码仍然是200mget请求本身成功执行就是200
    确定某个文档查找是成功或者失败,需要坚持found标记

    相关文章

      网友评论

          本文标题:GET/MGET

          本文链接:https://www.haomeiwen.com/subject/zznduqtx.html