Search code examples
redisearch

How to get document ids from FT.AGGREGATE with Redisearch?


I would like to aggregate documents and returns only document ids.

How to do it?


Solution

  • With aggregate you sometimes may return results which has nothing to do with document id (for example if you group by some field and then return the groups sizes). This is why the aggregate result do not contains the document id. If your specific aggregation does preserve the document id then it is possible to put it inside the document itself as a none indexed field and then return it.

    For example:

    127.0.0.1:6379> FT.CREATE idx SCHEMA name TEXT SORTABLE docid TAG SORTABLE NOINDEX
    OK
    127.0.0.1:6379> FT.ADD idx doc1 1.0 FIELDS name name1 docid doc1
    OK
    127.0.0.1:6379> FT.ADD idx doc2 1.0 FIELDS name name2 docid doc2
    OK
    127.0.0.1:6379> FT.ADD idx doc3 1.0 FIELDS name name1 docid doc3
    OK
    127.0.0.1:6379> FT.AGGREGATE idx * GROUPBY 1 @name REDUCE TOLIST 1 @docid as docids
    1) (integer) 2
    2) 1) name
       2) "name2"
       3) docids
       4) 1) "doc2"
    3) 1) name
       2) "name1"
       3) docids
       4) 1) "doc1"
          2) "doc3"