Skip to content

SearchArgs.returnField with alias produces malformed redis command #3528

@garry-mcfly

Description

@garry-mcfly

Bug Report

The commands generated in io.lettuce.core.search.arguments.SearchArgs#build for returnFieldswith an alias is wrong. As stated in https://redis.io/docs/latest/commands/ft.search the format is RETURN {num} {identifier} AS {property} where num is the number of attributes following the keyword. So AS and the alias must be counted too.

Current Behavior

The current codes just append the alias as an additional key. Also it does not correctly increment the attribute counter.

Unit test
    @Test
    void returnFieldsWithAlias() {
        var options = SearchArgs.<String, String>builder()
                .returnField("as_is")
                .returnField("$.field", "alias")
                .build();

        var args = new CommandArgs<>(new StringCodec());
        options.build(args);
        // buggy implementation returns "RETURN 2 key<as_is> key<$.field> key<alias> DIALECT "
        assertEquals("RETURN 4 key<as_is> key<$.field> AS key<alias> DIALECT 2", args.toCommandString());
    }

Expected behavior/code

SearchArgs.returnField creates a valid redis command.

Environment

  • Lettuce version(s): 7.1.0.RELEASE, 6.8.1.Final
  • Redis version: 7.4.2

Possible Solution

Possible solution may look like

        if (!returnFields.isEmpty()) {
            args.add(CommandKeyword.RETURN);
            int count = 0;
            for (Optional<K> value : returnFields.values()) {
                count+=value.isPresent() ? 3 : 1;
            }
            args.add(count);
            returnFields.forEach((field, as) -> {
                args.addKey(field);
                as.ifPresent(key -> {
                    args.add(CommandKeyword.AS);
                    args.addKey(key);
                });
            });
        }

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions