Sounds like something Postgres would be very good at, using window functions or (probably recursive) CTEs; its query language is much richer than both ElasticSearch and MongoDB.<p>Postgres&#x27; text indexing isn&#x27;t as advanced as ElasticSearch&#x27;s (and indexing performance is definitely lower), but it&#x27;s not bad at all. If you only need basic term support and not actual term vectors, you can use plain Postgres arrays, which have all sorts of supported operators, and can be translated into tables and back to perform some really neat in-memory queries.<p>One downside, of course, is that you don&#x27;t get any sharding for free, and if your dataset is large enough you might have to manually partition your tables, either using Postgres partitions or by explicitly sharding (e.g., using pg_shard).