美文网首页
Elasticsearch 存储json 格式数据

Elasticsearch 存储json 格式数据

作者: 觉释 | 来源:发表于2020-09-15 14:12 被阅读0次

    首先我们有这样格式的json 串

     
    {
        "username": "xiaosi",
        "other": [{
            "age": 25,
            "last": "yes",
            "datetime": "2020-01-01",
            "weight": 48.0,
            "addone": "100"
    
        }]
    }
     
    

    建立索引,索引如下面所示

    PUT my_index_json/
    {
      "mappings": {
       
          "properties": {
            "username": {
              "type": "text"},
            "other": {
              "properties": {
                "age": { "type": "long" },
                "last":  { "type": "text" },
                "datetime" : {"type" : "date"},
                "weight" : {"type" : "long"}
              }
            } 
          }
     
      }
    }
    

    查看索引mapping

    GET my_index_json/_mapping
    

    插入索引数据

    PUT my_index_json/_doc/1
    {
      "username" : "xiaoli",
      "other" : [ 
        {
          "age" : 30,
          "last" :  "Smith",
          "datetime":"2020-01-01",
          "weight":50.0
          
        }
      ]
    }
    
    PUT my_index_json/_doc/2
    {
      "username" : "xiaoli",
      "other" : [ 
         {
           "age" : 28,
          "last" :  "Smith",
          "datetime":"2020-01-03",
          "weight":40.0
        }
      ]
    }
    
    #插入一条(addone 没有在索引mapping  中体现)
    PUT my_index_json/_doc/3
    {
      "username" : "xiaosi",
      "other" : [ 
        {
          "age" : 25,
          "last" :  "yes",
          "datetime":"2020-01-01",
          "weight":48.0,
          "addone":"100"
          
        } 
      ]
    }
    
    
    或者是批量插入,自己写
    

    检索索引

    GET my_index_json/_search
    

    根据条件检索

    GET my_index_json/_search
    {
      "query": {
        "range": {
          "other.age": {
              "gte": 10,
              "lte": 29
          }
        }
      },
      "size": 1000
    }
    返回数据
    
    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "my_index_json",
            "_type" : "_doc",
            "_id" : "2",
            "_score" : 1.0,
            "_source" : {
              "username" : "xiaozhang",
              "other" : [
                {
                  "age" : 28,
                  "last" : "Smith",
                  "datetime" : "2020-01-01",
                  "weight" : 50.0
                }
              ]
            }
          }
        ]
      }
    }
    
    

    根据条件检索2

    GET /my_index_json/_search
    {
      "size" : 1, 
      "query": { 
        "bool": {
          "must": [
            { "match": { "other.addone":     "100" }} 
          ]
        }
      }
    }
    
    返回数据
    {
      "took" : 55,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 0.2876821,
        "hits" : [
          {
            "_index" : "my_index_json",
            "_type" : "_doc",
            "_id" : "3",
            "_score" : 0.2876821,
            "_source" : {
              "username" : "xiaosi",
              "other" : [
                {
                  "age" : 25,
                  "last" : "yes",
                  "datetime" : "2020-01-01",
                  "weight" : 48.0,
                  "addone" : "100"
                }
              ]
            }
          }
        ]
      }
    }
    
    

    相关文章

      网友评论

          本文标题:Elasticsearch 存储json 格式数据

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