Hi Jimi, It seems to me that you could get the results using collapsing query parse: https://lucene.apache.org/solr/guide/6_6/collapse-and-expand-results.html <https://lucene.apache.org/solr/guide/6_6/collapse-and-expand-results.html>
HTH, Emir -- Monitoring - Log Management - Alerting - Anomaly Detection Solr & Elasticsearch Consulting Support Training - http://sematext.com/ > On 5 Feb 2021, at 14:17, Hullegård, Jimi > <jimi.hulleg...@svensktnaringsliv.se> wrote: > > Hi, > > Say we have a bunch of documents in Solr, and each document has a multi value > field "category". Now I would like to get the N most recently used > categories, ordered so that the most recently used category comes first and > then in falling order. > > My simplistic solution to this would be: > > 1. Perform a search for all documents with at least one category set, sorted > by last modified date > 2. Iterate over the search results, extracting the categories used, and add > them to a list > 3. If we have N number of categories, stop iterating the results > 4. If there isn't enough categories, go to the next page in the search > results and start over at step 2 above, until N categories are found or no > more search results > > But this doesn't seem very efficient. Especially if there are lots of > documents, and N is a high number and/or only a handful of categories are > used most of the time, since it could mean one has to look through a whole > lot of documents before having enough categories. Worst case scenario: N is > higher than the total number of unique categories used, in which case one > would iterate over every single document that has a category. > > Is there a way one can construct some special query to solr to get this data > in a more efficient way? > > Regards > /Jimi > > Svenskt Näringsliv är företagsamhetens röst i Sverige. Vi samverkar med 50 > arbetsgivar- och branschorganisationer och är den gemensamma rösten för 60 > 000 företag med nästan 2 miljoner medarbetare. Vår uppgift är att tala för > alla företag och branscher, även de som ännu inte finns men som kan uppstå om > förutsättningarna är de rätta. Ett bättre företagsklimat för ett bättre > Sverige. Det är vårt uppdrag. > > Svenskt Näringsliv behandlar dina personuppgifter i enlighet med GDPR. Här > kan du läsa mer om vår behandling och dina rättigheter, > Integritetspolicy<https://www.svensktnaringsliv.se/dataskydd/integritet-och-behandling-av-personuppgifter_697219.html?utm_source=sn-email&utm_medium=email>