如何创建ID并从elasticsearch

时间:2018-03-13 10:18:59

标签: elasticsearch elasticsearch-5

golang中的

如何使用用户生成的id并为每个新条目增加它?我试过了

  Index(KpiIndex).
      Type(KpiDocType).
      Id(ids).                                                              => Id will take only stings.
      BodyJson(kpis).
      Do(elasticSearchContext)

当数据存储在ES中时,它会随机存储

  

{     "把" :5,     " TIMED_OUT" :假的,     " _shards" :{       "总" :5,       "成功" :5,       "失败" :0     },     "命中" :{       "总" :4,       " MAX_SCORE" :1.0,       "命中" :[         {           " _index" :" kpi",           " _type" :" kpi",           " _id" :" AWIexPLwnbh5r7Xr9nsL",           " _score" :1.0,           " _source" :{             "日期" :" 2018-03-13 15:20:45",             " KPI" :[               {

          "Static_limit" : 0
        },
        {
          "CpuAverageLoad" : 0,
          ...,
          "NodeType" : "kkk",
          "State" : "online"
        }
      ]
    }
  },
  {
    "_index" : "kpi",
    "_type" : "kpi",
    "_id" : "AWIexYy1_xnN0Y7Qo1HE",
    "_score" : 1.0,
    "_source" : {
      "date" : "2018-03-13 15:21:25",
      "kpi" : [
        {
          "CpuAverageLoad" : 0,
          },
        {
          "CpuAverageLoad" : 0,
          ...,
          "NodeType" : "kkk",
          "State" : "online"
        }
      ]
    }
  },
  {
    "_index" : "kpi",
    "_type" : "kpi",
    "_id" : "AWIexdrLnbh5r7Xr9nsM",
    "_score" : 1.0,
    "_source" : {
      "date" : "2018-03-13 15:21:45",
      "kpi" : [
        {

          "Static_limit" : 0
        },
        {
          "CpuAverageLoad" : 0,
          ...,
          "NodeType" : "kkk",
          "State" : "online"
        }
      ]
    }
  },
  {
    "_index" : "kpi",
    "_type" : "kpi",
    "_id" : "AWIexT52BjtG8JuPSysx",
    "_score" : 1.0,
    "_source" : {
      "date" : "2018-03-13 15:21:05",
      "kpi" : [
        {

          "Static_limit" : 0
        },
        {
          "CpuAverageLoad" : 0,
          ...,
          "NodeType" : "kkk",
          "State" : "online"
        }
      ]
    }
  }
]
     

}   }

当我们观察“数据”时字段,顺序是

"date" : "2018-03-13 15:20:45",
"date" : "2018-03-13 15:21:25",
"date" : "2018-03-13 15:21:45",
"date" : "2018-03-13 15:21:05",

正确的顺序应该是

"date" : "2018-03-13 15:20:45",
"date" : "2018-03-13 15:21:05",
"date" : "2018-03-13 15:21:25",
"date" : "2018-03-13 15:21:45"

1 个答案:

答案 0 :(得分:1)

您可以将属性用于json正文,其值为GUID。因此,将属性定义为_id属性。它是定义索引id的方式。