not able to query with numeric fields


I have a usecase and wanted to know if redis search would be a good fit.

have a list of articles, each of which belong to a category, publication, and has a timestamp. A user can select a category, publication(s), and can check out the latest articles. Further, the user
can scroll to the next set of articles before that time.The sql query for that essentially boils down to

select * from articles where catid=1 and publication in (11, 12) where timestamp < 'timestamp when user visited last article' order by timestamp desc limit 10

So, i tried creating a very simple index, with numeric fields.> FT.CREATE myIdx SCHEMA catid NUMERIC pubid NUMERIC ts NUMERIC SORTABLE json TEXT
OK> FT.ADD myIdx doc1 1.0 FIELDS catid 1 pubid 11 ts 123456 json value1
OK> FT.ADD myIdx doc2 1.0 FIELDS catid 1 pubid 12 ts 123457 json value2
OK> FT.ADD myIdx doc3 1.0 FIELDS catid 2 pubid 12 ts 123458 json value3
OK> FT.SEARCH myIdx @pubid:[10 13]
(error) 13]> FT.SEARCH myIdx @pubid:[10]
(error) Syntax error at offset 10 near β€˜10’> FT.SEARCH myIdx @pubid:10

  1. (integer) 0> FT.SEARCH myIdx @pubid:11
  2. (integer) 0> FT.SEARCH myIdx @json:value3
  3. (integer) 1
  4. β€œdoc3”
    1. β€œcatid”
    2. β€œ2”
    3. β€œpubid”
    4. β€œ12”
    5. β€œts”
    6. β€œ123458”
    7. β€œjson”
    8. β€œvalue3”>

As the above shows i am not able to query with the numeric fields(pubid=11). It is probably something silly mistake.

Hi Tuco-

Classic mistake. You were on the right track - just add in the quote marks because the query is a single argument.

FT.SEARCH myIdx β€œ@pubid:[10 13]”

If you want to find by an exact value then set the upper and lower bounds to the same value:

FT.SEARCH myIdx β€œ@pubid:[11 11]”

Hope this helps!


1 Like

Thanks Kyle, it worked.

Also, should the IN clause in a SQL be implemented using the multiple OR conditions?

The below query seems to work well for me.

FT.SEARCH myIdx β€œ(@catid:[1 1] | @catid:[2 2]) @pubid:[12 12] @ts:[123455 inf]” RETURN 2 json ts SORTBY ts DESC LIMIT 0 2

It is essentially β€œWHERE catid in (1, 2) AND pubid=12 AND ts > 123455”