'Elastic search, sort by number of documents a user has
I have an index, every document belongs to a user.
I want to be able to sort in such a way so that the results return
- the first matching document of each user
- the second matching document of each user
- the third... etc
This would be to prevent one user to fill up the search results.
The only thing i could think of is to basically keep a counter in each document the user has and then sort on that counter. it would also mean that every time i delete or add a document i need to update all of the documents of the user.
Is there a better way of doing this?
Solution 1:[1]
Field collapsing may work for you. https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html
If you collapse by userId, you can retrieve documents grouped by userId. And using inner_hits you can retrieve documents related to that user.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | YD9 |
