美文网首页
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 格式数据

    首先我们有这样格式的json 串 建立索引,索引如下面所示 查看索引mapping 插入索引数据 检索索引 根据条...

  • 零基础带你搞定分布式爬虫(第二节)

    数据存储 json ------命名不要json.py,坑----- JSON支持数据格式: 对象(字典)。使用花...

  • JSON

    JSON数据格式 JSON的概念 JSON全称JavaScript Object Notation,通过键值对存储...

  • ELASSTICSEARCH DATA

    Elasticsearch数据的存储格式 Elastcisearch 是分布式的 文档 存储。它能存储和检索复杂的...

  • Android Studio 插件 GsonFormat :你

    前言 Json 数据存储格式在Android 开发中使用非常常见 在为 Json 格式 生成 JavaBean实体...

  • Python百宝箱

    1.对Json数据的读取、存入 json数据存储为Python字典格式,python模块中有json.pickle...

  • JavaScript JSON

    JavaScript JSON JSON 是用于存储和传输数据的格式。JSON 通常用于服务端向网页传递数据 。 ...

  • JSON(8/31)

    JavaScript JSON JSON 是用于存储和传输数据的格式。 JSON 通常用于服务端向网页传递数据 。...

  • 第二十四章 JSON

    JSON 什么是JSON呢? json是一种轻量级的数据格式的 (并不是js中独有的数据格式)是用来存储表示数据的...

  • R语言 JSON文件

    JSON文件以人类可读格式将数据存储为文本。 Json代表JavaScript Object Notation。 ...

网友评论

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

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