2014-07-15 9 views

risposta

3

Si desidera ottenere il primo _id per un tipo?

{ 
    "fields": [ 
    "_id" 
    ], 
    "query": { 
    "match_all": {} 
    }, 
    "sort": { 
    "_id": "desc" 
    }, 
    "size": 1 
} 
3

Ho testato questa query su 1.4.2. Dovrebbe funzionare con qualsiasi mappatura, perché stai solo ordinando l'ID ElasticSearch integrato. Se stai usando il tuo campo id, potrebbe o non potrebbe funzionare a seconda del tuo mapping. Prova questo e fammi sapere come funziona.

supponendo che il tipo è chiamato fruit:

{ 
    'filter': { 
    'type' : { 
     'value' : 'fruit' 
    } 
    }, 
    'sort': { 
    '_id': { 
     'order': 'desc' 
    } 
    }, 
    'size': 1 
} 
7

È inoltre possibile utilizzare Max Aggregation (dal 1,3) per ottenere il massimo valore di un determinato campo in un documento.

curl localhost:9200/myindex/mytype/_search -d 
    '{ 
    "aggs" : { 
     "max_id" : { 
     "max" : { 
      "field" : "id" 
     } 
     } 
    }, 
    "size":0 
    }' 

tornerà:

{ 
    "took":7, 
    "timed_out":false, 
    "_shards": { 
    "total":6, 
    "successful":6, 
    "failed":0 
    }, 
    "hits": { 
    "total":22, 
    "max_score":0.0, 
    "hits": [] 
    }, 
    "aggregations": { 
    "max_id": { 
     "value": 27.0 
    } 
    } 
} 

Nel mio caso la massima id è .

1
{ 
    "fields": [ 
    "_uid" 
    ], 
    "query": { 
    "match_all": {} 
    }, 
"size": 1, 
"sort": [ { "_uid" : { "order" : "desc"} } ] 
}