How to get last indexed record in Solr?

11,814

Solution 1

You could add a 'timestamp' field to your Solr schema that puts the current date/time into the record when it is added.

<field name="timestamp" type="date" indexed="true" stored="true" default="NOW" multiValued="false"/>

Then, do a sort in descending order by this field and the first record will be the latest one. A query like this should do it:-

http://localhost:8080/solr/core-name/select/q=*%3A*&start=0&rows=1&sort=timestamp+desc

Solution 2

You can sort the documents by the indexed order using the following query.

http://localhost:8983/solr/select?q=*:*&sort=_docid_ asc

or 

http://localhost:8983/solr/select?q=*:*&sort=_docid_ desc
Share:
11,814
milind_db
Author by

milind_db

Updated on June 04, 2022

Comments

  • milind_db
    milind_db almost 2 years

    I want to know how to get/search last indexed record in Apache Solr..?

    When the existing record is updated then it goes to end of all the records...so I want to get that last indexed record.

    thanks..

  • milind_db
    milind_db over 11 years
    Is there no other way...? because I have to add field in Solr indexing then... thanks for reply...
  • Paige Cook
    Paige Cook over 11 years
    I believe this to be most reliable way to do this, as the timestamp field will be added by Solr and is guaranteed to be accurate. You could go with @parvin's answer, but that would assume that your docid field is sequential and can be sorted.
  • Oyeme
    Oyeme over 8 years
    Thanks, that's what I needed.
  • Avner Levy
    Avner Levy almost 8 years
    Is it safe to count on docid cross merges? see osdir.com/ml/solr-user.lucene.apache.org/2011-12/msg01178.ht‌​ml
  • aName
    aName over 7 years
    dont use docid because it changing see that response to understand lucene.472066.n3.nabble.com/…
  • Tenaciousd93
    Tenaciousd93 over 2 years
    For me this is not the best way to get the desire result: q*:* tells solr to search in every record you have in index. In my case, with over 30 millions stored records the response took about 10 seconds to be return, even if I add rows=1. Is there an efficient way to get the last record?