jkazuop
jkazuop

Reputation: 1

Ruby Elasticsearch API: Returning the latest entry to an index

I've enabled the "_timestamp" field in the index mapping and I successfully retrieved the latest entry to an index using the elasticsearch REST API. The body of the POST request I used looks like this:

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

Now I'm trying to translate this into the Ruby elasticsearch-api syntax... This is what I have so far:

client = Elasticsearch::Client.new host: 'blahblahblah:9200'
json = client.search index: 'index', 
                type: 'type',
                body: { query: { match_all: {} }},
                sort: '_timestamp',
                size: 1

I've tried several variations on the above code, but nothing seems to return the newest entry. I can't find many examples online using the Ruby elasticsearch API syntax, so any help would be greatly appreciated!

If there is a way to return the latest entry without using the "_timestamp" field, I am open to trying that as well!

Upvotes: 0

Views: 373

Answers (1)

jkazuop
jkazuop

Reputation: 1

I finally found the correct syntax:

json = client.search index: 'index',
                    type: 'type',
                    body: { query: { match_all: {} }, size: 1, sort: [ { _timestamp: { order: "desc"} } ] }

Upvotes: 0

Related Questions