美文网首页
elasticsearch之索引文档和取回文档

elasticsearch之索引文档和取回文档

作者: 煮茶boy | 来源:发表于2018-10-22 00:55 被阅读0次

    文档: 多数实体或对象可以被序列化为包含键值对的 JSON 对象,文档是指最顶层或者根对象, 这个根对象被序列化成 JSON 并存储到 Elasticsearch 中,指定了唯一 ID。

    文档元数据: 一个文档不仅仅包含它的数据 ,也包含元数据 —— 有关*文档的信息。

    • _index: 文档在哪存放
    • _type: 文档表示的对象类别
    • _id: 文档唯一标识

    索引文档

    • 使用自定义的ID
    PUT /index/type/id
    {
      "field": "value",
      ...
    }
    
    • elasticsearch自动为我们生成ID(使用POST方法
    POST /website/blog/
    {
      "title": "My second blog entry",
      "text":  "Still trying this out...",
      "date":  "2014/01/01"
    }
    

    取回一个文档

    GET /index/type/id?pretty    --- pretty格式化输出,是输出结果更加美观
    
    GET /website/blog/123?pretty
    
    {
      "_index" :   "website",
      "_type" :    "blog",
      "_id" :      "123",
      "_version" : 1,
      "found" :    true,
      "_source" :  {      ---- '_source'字段里面就是我们需要的JSON数据
          "title": "My first blog entry",
          "text":  "Just trying this out...",
          "date":  "2014/01/01"
      }
    }
    
    • 返回JSON数据中的一部分字段(需要在url上给_source参数赋值)
    GET /website/blog/123?_source=title,text
    
    {
      "_index" :   "website",
      "_type" :    "blog",
      "_id" :      "123",
      "_version" : 1,
      "found" :   true,
      "_source" : {
          "title": "My first blog entry" ,
          "text":  "Just trying this out..."
      }
    }
    
    • 只返回_source里面的字段(需要使用_source 端点)
    GET /website/blog/123/_source
    
    {
       "title": "My first blog entry",
       "text":  "Just trying this out...",
       "date":  "2014/01/01"
    }
    

    相关文章

      网友评论

          本文标题:elasticsearch之索引文档和取回文档

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