Skip to content

IndexDefinition Prefix Footgun #3890

@coldham10

Description

@coldham10

Discovered a very confusing footgun during a recent refactor with the peculiar syntax of RediSearch IndexDefinition and insufficient type checking.
Consider the following, sensible-looking code:

my_prefix = "MY:INDEXABLE:KEY:"
index_key = my_prefix + "index"

defn = IndexDefinition(prefix=my_prefix, index_type=IndexType.HASH)
redis_client.ft(index_key).create_index(my_fields, definition=defn)

What would we expect this to do?
Create an index of hashes prefixed by my_prefix ? No.
Error, because the arg prefix= expects an iterable? No..
Index every single hash prefixed by {"M", "Y", ":", "I", "N", "D", "E", "X", "A", "B", "L", "K"} ? It seems this is actually what happens.

Might I suggest IndexDefinition takes a keyword argument of prefixes= to indicate that it expects an iterable?

Or, in index_definition.py:

    def _append_prefix(self, prefix):
        """Append PREFIX."""
        if len(prefix) > 0:
            self.args.append("PREFIX")
            self.args.append(len(prefix))
            for p in prefix:
                self.args.append(p)

A quick raise RuntimeError if prefix is a string

Thanks for considering!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions