redisredis-searchredis-stack

How to add a document to key in redis-stack without being "findible" from other keys


I am beginning to use redis-stack so I created (inside redis-stack container) the next keys and documents:

FLUSHALL
FT.CREATE myIdx SCHEMA title TEXT WEIGHT 5.0 body TEXT url TEXT

Then a document to it:

FT.ADD myIdx doc1 1.0 FIELDS title "hello world" body "lorem ipsum" url "http://redis.io"

Then I see that this was successfully created:

127.0.0.1:6379> FT.SEARCH myIdx *

  1. (integer) 1
  2. "doc1"
    1. "title"
    2. "hello world"
    3. "body"
    4. "lorem ipsum"
    5. "url"
    6. "http://redis.io"

Then I created another key:

FT.CREATE myIdx2 SCHEMA tag TEXT  number TEXT address TEXT

then

FT.SEARCH myIdx2 *
  1. (integer) 1
  2. "doc1"
    1. "title"
    2. "hello world"
    3. "body"
    4. "lorem ipsum"
    5. "url"
    6. "http://redis.io"``

Maybe I am not understanding the concept about documents and keys but I was expecting to see "doc1" only in key "myIdx"

If I add another document in the second key:

FT.ADD myIdx2 doc2 1.0  FIELDS tag "second doc" body "random" url "http://dis.io"

then if i check either "myIdx" or "myIdx2" both show me the documents like it doesn't matter to which key I add the document with FT:ADD:

FT.SEARCH myIdx *:

  1. (integer) 2
  2. "doc2"
    1. "tag"
    2. "second doc"
    3. "body"
    4. "random"
    5. "url"
    6. "http://dis.io"
  3. "doc1"
    1. "body"
    2. "lorem ipsum"
    3. "url"
    4. "http://redis.io"
    5. "title"
    6. "hello world"

Can someone please explain to me why I am seeing the documents in all keys. Is it some configuration in redis-stack I need to take care of? Do i need to pass another argument to FT.ADD to make sure that the document is only "findable" in the one key I add to?

This is how I got here:

Redis-stack in a container in Docker Desktop (4.32.0) in Macos (Ventura, macbook pro M1), I simply did: docker pull redis/redis-stack:latest and then in Docker desktop I got it running. Then accessed the container through UI and input the comands after redis-cli.


Solution

  • FT.ADD is deprecated, and the right way to add documents to your index is directly with redis hashes, or JSONs if you have the module (it is part of the redis-stack) and you define your index for JSONs.

    HSET is the command that sets the hash on redis. Today, FT.ADD is redirected and calls HSET while keeping the old API behavior and options.

    In order to index only specific hashes on each index, you should use the PREFIX option when creating the index, otherwise each index will attempt to load all the hashes (or JSONs) in your database.

    For example, you can define

    FT.CREATE myIdx1 PREFIX 1 doc:idx1: …
    FT.CREATE myIdx2 PREFIX 1 doc:idx2: …
    FT.CREATE allMyDocs PREFIX 1 doc: …
    

    And then

    HSET doc:idx1:uid123 title "hello world" body "lorem ipsum" url "http://redis.io"
    HSET doc:idx2:uid456 tag "second doc" body "random" url "http://dis.io"
    

    You can also use the FILTER option to set a condition on the values in the document to decide whether to index it or not.

    Check out both options in the documentations here