Elasticsearch 存储json 格式数据

2020-09-15  本文已影响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"
            }
          ]
        }
      }
    ]
  }
}

上一篇下一篇

猜你喜欢

热点阅读