# How to createID and pick latest n entries from the set

**URL:** <https://discuss.elastic.co/t/how-to-createid-and-pick-latest-n-entries-from-the-set/123708>\
**Category:** Elasticsearch\
**Created:** [March 13, 2018, 9:58am UTC](https://discuss.elastic.co/t/how-to-createid-and-pick-latest-n-entries-from-the-set/123708 "2018-03-13T09:58:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kish](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kish](https://discuss.elastic.co/u/kish)\
**Post date:** [March 13, 2018, 9:58am UTC](https://discuss.elastic.co/t/how-to-createid-and-pick-latest-n-entries-from-the-set/123708/1 "2018-03-13T09:58:12Z")

</div>

in golang how to use user generated id and increment it for every new entry? I tried

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

And when data is getting stored in ES, it is getting stored in randomly say

> {  
> "took" : 5,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 4,  
> "max\_score" : 1.0,  
> "hits" : [  
> {  
> "\_index" : "kpi",  
> "\_type" : "kpi",  
> "\_id" : "AWIexPLwnbh5r7Xr9nsL",  
> "\_score" : 1.0,  
> "\_source" : {  
> "date" : "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"
> }
> ]
> }
> }
> ]
> 
> ```
> 
> }  
> }

When we observe the 'data' field, the order is  
"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",

where as the correct order should be

"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"

---

<div class="post-metadata">

**Author:** ![kish](https://avatars.discourse-cdn.com/v4/letter/k/71e660/32.png) [@kish](https://discuss.elastic.co/u/kish)\
**Post date:** [March 13, 2018, 10:06am UTC](https://discuss.elastic.co/t/how-to-createid-and-pick-latest-n-entries-from-the-set/123708/2 "2018-03-13T10:06:30Z")

</div>

there is no proper order in storing the data. For example

> curl -XGET 'localhost:9200/kpi/\_search?pretty' | grep -w date  
> "date" : "2018-03-13 15:21:45",  
> :-- --: "date" : "2018-03-13 15:23:05",  
> --:-- "date" : "2018-03-13 15:26:05",  
> --: "date" : "2018-03-13 15:20:45",  
> --:- "date" : "2018-03-13 15:21:25",
> 
> - 7 "date" : "2018-03-13 15:22:45",  
> "date" : "2018-03-13 15:24:25",  
> "date" : "2018-03-13 15:24:45",  
> "date" : "2018-03-13 15:26:25",  
> "date" : "2018-03-13 15:25:25"

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 10, 2018, 10:06am UTC](https://discuss.elastic.co/t/how-to-createid-and-pick-latest-n-entries-from-the-set/123708/3 "2018-04-10T10:06:37Z")

</div>

Closed on April 10, 2018, 10:06am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
