zoukankan      html  css  js  c++  java
  • es的相关知识二(检索文档)

    一、es的使用

    1、检索文档:

    想要从Elasticsearch中获取文档,我们使用同样的 _index  、 _type  、 _id  ,但是HTTP方法改为 GET  :

    GET /{index}/{type}/{id}?pretty
    

      


    这里pretty的作用:在任意的查询字符串中增加 pretty  参数,类似于上面的例子。会让Elasticsearch美化输出(pretty-print)JSON响应,以便更加容易阅读。 _source  字段不会被美化,它的样子与我们输入的一致。

    {
    "_index" : "...",
    "_type" : "...",
    "_id" : "...",
    "_version" : 1,
    "found" : true,
    "_source" : {
    "title": "...",
    "text": "...",
    "date": "..."
    }
    }


    GET请求返回的响应内容包括 {"found": true}  。这意味着文档已经找到。如果我们请求一个不存在的文档,依旧会得到一个
    JSON,不过 found  值变成了 false  。

    此外,HTTP响应状态码也会变成 '404 Not Found'  代替 '200 OK'  。我们可以在 curl  后加 -i  参数得到响应头:

    curl -i -XGET http://localhost:9200/{index}/{type}/{id}?pretty
    

      


    响应:

    HTTP/1.1 404 Not Found
    Content-Type: application/json; charset=UTF-8
    Content-Length: 83

    {
    "_index" : "...",
    "_type" : "...",
    "_id" : "...",
    "found" : false
    }
    

      


    2、检索文档的一部分

    通常, GET  请求将返回文档的全部,存储在 _source  参数中。但是可能你感兴趣的字段只是 title  。请求个别字段可以使
    用 _source  参数。多个字段可以使用逗号分隔:

    GET /{index}/{type}/{id}?_source=title,text
    

      


    _source  字段现在只包含我们请求的字段,而且过滤了 date  字段:
    响应:
     

    {
    "_index" : "。。。",
    "_type" : "。。。",
    "_id" : "。。。",
    "_version" : 1,
    "exists" : true,
    "_source" : {
    "title": "My first blog entry" ,
    "text": "Just trying this out..."
    }
    }
    

      


    或者你只想得到 _source  字段而不要其他的元数据,你可以这样请求:

    GET /{index}/{type}/{id}/_source
    

      


    响应内容:

    {
    "title": "My first blog entry",
    "text": "Just trying this out...",
    "date": "2014/01/01"
    }
     
    ————————————————
    版权声明:本文为CSDN博主「大数据小蜗牛」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/qq_40651753/article/details/86009138

  • 相关阅读:
    LeetCode 1275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
    LeetCode 307. 区域和检索
    LeetCode 1271 十六进制魔术数字 Hexspeak
    秋实大哥与花 线段树模板
    AcWing 835. Trie字符串统计
    Leetcode 216. 组合总和 III
    Mybatis 示例之 复杂(complex)属性(property)
    Mybatis 示例之 复杂(complex)属性(property)
    Mybatis 高级结果映射 ResultMap Association Collection
    Mybatis 高级结果映射 ResultMap Association Collection
  • 原文地址:https://www.cnblogs.com/wang102030/p/11950634.html
Copyright © 2011-2022 走看看