Skip to content

Commit

Permalink
Merge pull request #29406 from cescoffier/redis-labs-auto-suggest
Browse files Browse the repository at this point in the history
  • Loading branch information
cescoffier authored Nov 24, 2022
2 parents d3e22a0 + 92781b2 commit 6eca42c
Show file tree
Hide file tree
Showing 24 changed files with 1,059 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/src/main/asciidoc/redis-reference.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ As mentioned above, the API is divided into groups:
- graph - `.graph()` (requires the https://redis.com/modules/redis-graph/[RedisGraph] module on the server side).
These commands are marked as experimental, as we would need feedback before making them stable.
- search - `.search()` (requires the https://redis.com/modules/redis-search/[RedisSearch] module on the server side).
- auto-suggest - `.autosuggest()` (requires the https://redis.com/modules/redis-search/[RedisSearch] module on the server side).O
These commands are marked as experimental, as we would need feedback before making them stable.

Each of these methods returns an object that lets you execute the commands related to the group.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.function.BiFunction;
import java.util.function.Function;

import io.quarkus.redis.datasource.autosuggest.ReactiveAutoSuggestCommands;
import io.quarkus.redis.datasource.bitmap.ReactiveBitMapCommands;
import io.quarkus.redis.datasource.bloom.ReactiveBloomCommands;
import io.quarkus.redis.datasource.countmin.ReactiveCountMinCommands;
Expand Down Expand Up @@ -539,6 +540,27 @@ default ReactiveSearchCommands<String> search() {
return search(String.class);
}

/**
* Gets the object to emit commands from the {@code auto-suggest} group.
* This group requires the <a href="https://redis.io/docs/stack/search/">RedisSearch module</a>.
*
* @param <K> the type of keys
* @return the object to get suggestions
*/
@Experimental("The Redis auto-suggest support is experimental")
<K> ReactiveAutoSuggestCommands<K> autosuggest(Class<K> redisKeyType);

/**
* Gets the object to emit commands from the {@code auto-suggest} group.
* This group requires the <a href="https://redis.io/docs/stack/search/">RedisSearch module</a>.
*
* @return the object to get suggestions
*/
@Experimental("The Redis auto-suggest support is experimental")
default ReactiveAutoSuggestCommands<String> autosuggest() {
return autosuggest(String.class);
}

/**
* Executes a command.
* This method is used to execute commands not offered by the API.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.function.Consumer;
import java.util.function.Function;

import io.quarkus.redis.datasource.autosuggest.AutoSuggestCommands;
import io.quarkus.redis.datasource.bitmap.BitMapCommands;
import io.quarkus.redis.datasource.bloom.BloomCommands;
import io.quarkus.redis.datasource.countmin.CountMinCommands;
Expand Down Expand Up @@ -528,6 +529,27 @@ default SearchCommands<String> search() {
return search(String.class);
}

/**
* Gets the object to emit commands from the {@code auto-suggest} group.
* This group requires the <a href="https://redis.io/docs/stack/search/">RedisSearch module</a>.
*
* @param <K> the type of keys
* @return the object to get suggestions
*/
@Experimental("The Redis auto-suggest support is experimental")
<K> AutoSuggestCommands<K> autosuggest(Class<K> redisKeyType);

/**
* Gets the object to emit commands from the {@code auto-suggest} group.
* This group requires the <a href="https://redis.io/docs/stack/search/">RedisSearch module</a>.
*
* @return the object to get suggestions
*/
@Experimental("The Redis auto-suggest support is experimental")
default AutoSuggestCommands<String> autosuggest() {
return autosuggest(String.class);
}

/**
* Gets the objects to publish and receive messages.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package io.quarkus.redis.datasource.autosuggest;

import java.util.List;

import io.quarkus.redis.datasource.RedisCommands;

/**
* Allows executing commands from the {@code auto-suggest} group (requires the Redis Search module from Redis stack).
* See <a href="https://redis.io/commands/?group=suggestion">the auto-suggest command list</a> for further information about
* these
* commands.
*
* @param <K> the type of the key
*/
public interface AutoSuggestCommands<K> extends RedisCommands {

/**
* Execute the command <a href="https://redis.io/commands/ft.sugadd/">FT.SUGADD</a>.
* Summary: Add a suggestion string to an auto-complete suggestion dictionary
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param string the suggestion string to index
* @param score the floating point number of the suggestion string's weight
* @return A uni emitting the current size of the suggestion dictionary.
*/
default long ftSugAdd(K key, String string, double score) {
return ftSugAdd(key, string, score, false);
}

/**
* Execute the command <a href="https://redis.io/commands/ft.sugadd/">FT.SUGADD</a>.
* Summary: Add a suggestion string to an auto-complete suggestion dictionary
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param string the suggestion string to index
* @param score the floating point number of the suggestion string's weight
* @param increment increments the existing entry of the suggestion by the given score, instead of replacing the score.
* This is useful for updating the dictionary based on user queries in real time.
* @return A uni emitting the current size of the suggestion dictionary.
*/
long ftSugAdd(K key, String string, double score, boolean increment);

/**
* Execute the command <a href="https://redis.io/commands/ft.sugdel/">FT.SUGDEL</a>.
* Summary: Delete a string from a suggestion index
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param string the suggestion string to index
* @return A uni emitting {@code true} if the value was found, {@code false} otherwise
*/
boolean ftSugDel(K key, String string);

/**
* Execute the command <a href="https://redis.io/commands/ft.sugget/">FT.SUGGET</a>.
* Summary: Get completion suggestions for a prefix
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param prefix is prefix to complete on.
* @return A uni emitting a list of the top suggestions matching the prefix, optionally with score after each entry.
*/
List<Suggestion> ftSugGet(K key, String prefix);

/**
* Execute the command <a href="https://redis.io/commands/ft.sugget/">FT.SUGGET</a>.
* Summary: Get completion suggestions for a prefix
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param prefix is prefix to complete on.
* @param args the extra argument, must not be {@code null}
* @return A uni emitting {@code true} if the value was found, {@code false} otherwise
*/
List<Suggestion> ftSugGet(K key, String prefix, GetArgs args);

/**
* Execute the command <a href="https://redis.io/commands/ft.suglen/">FT.SUGLEN</a>.
* Summary: Get the size of an auto-complete suggestion dictionary
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @return A uni emitting the current size of the suggestion dictionary.
*/
long ftSugLen(K key);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.quarkus.redis.datasource.autosuggest;

import static io.quarkus.redis.runtime.datasource.Validation.positive;

import java.util.ArrayList;
import java.util.List;

import io.quarkus.redis.datasource.RedisCommandExtraArguments;

public class GetArgs implements RedisCommandExtraArguments {

private boolean fuzzy;
private int max;
private boolean withScores;

/**
* Performs a fuzzy prefix search, including prefixes at Levenshtein distance of 1 from the prefix sent.
*
* @return the current {@code GetArgs}.
*/
public GetArgs fuzzy() {
this.fuzzy = true;
return this;
}

/**
* Limits the results to a maximum of num (default: 5).
*
* @param max the max number of results, must be strictly positive
* @return the current {@code GetArgs}.
*/
public GetArgs max(int max) {
positive(max, "max");
this.max = max;
return this;
}

/**
* Also to attach the score of each suggestion.
* This can be used to merge results from multiple instances.
*
* @return the current {@code GetArgs}.
*/
public GetArgs withScores() {
this.withScores = true;
return this;
}

@Override
public List<String> toArgs() {
List<String> list = new ArrayList<>();
if (fuzzy) {
list.add("FUZZY");
}
if (max > 0) {
list.add("MAX");
list.add(Integer.toString(max));
}
if (withScores) {
list.add("WITHSCORES");
}
return list;
}

public boolean hasScores() {
return withScores;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package io.quarkus.redis.datasource.autosuggest;

import java.util.List;

import io.quarkus.redis.datasource.ReactiveRedisCommands;
import io.smallrye.common.annotation.Experimental;
import io.smallrye.mutiny.Uni;

/**
* Allows executing commands from the {@code auto-suggest} group (requires the Redis Search module from Redis stack).
* See <a href="https://redis.io/commands/?group=suggestion">the auto-suggest command list</a> for further information about
* these commands.
*
* @param <K> the type of the key
*/
@Experimental("The auto-suggest group is experimental")
public interface ReactiveAutoSuggestCommands<K> extends ReactiveRedisCommands {

/**
* Execute the command <a href="https://redis.io/commands/ft.sugadd/">FT.SUGADD</a>.
* Summary: Add a suggestion string to an auto-complete suggestion dictionary
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param string the suggestion string to index
* @param score the floating point number of the suggestion string's weight
* @return A uni emitting the current size of the suggestion dictionary.
**/
default Uni<Long> ftSugAdd(K key, String string, double score) {
return ftSugAdd(key, string, score, false);
}

/**
* Execute the command <a href="https://redis.io/commands/ft.sugadd/">FT.SUGADD</a>.
* Summary: Add a suggestion string to an auto-complete suggestion dictionary
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param string the suggestion string to index
* @param score the floating point number of the suggestion string's weight
* @param increment increments the existing entry of the suggestion by the given score, instead of replacing the score.
* This is useful for updating the dictionary based on user queries in real time.
* @return A uni emitting the current size of the suggestion dictionary.
**/
Uni<Long> ftSugAdd(K key, String string, double score, boolean increment);

/**
* Execute the command <a href="https://redis.io/commands/ft.sugdel/">FT.SUGDEL</a>.
* Summary: Delete a string from a suggestion index
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param string the suggestion string to index
* @return A uni emitting {@code true} if the value was found, {@code false} otherwise
**/
Uni<Boolean> ftSugDel(K key, String string);

/**
* Execute the command <a href="https://redis.io/commands/ft.sugget/">FT.SUGGET</a>.
* Summary: Get completion suggestions for a prefix
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param prefix is prefix to complete on.
* @return A uni emitting a list of the top suggestions matching the prefix, optionally with score after each entry.
**/
Uni<List<Suggestion>> ftSugGet(K key, String prefix);

/**
* Execute the command <a href="https://redis.io/commands/ft.sugget/">FT.SUGGET</a>.
* Summary: Get completion suggestions for a prefix
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @param prefix is prefix to complete on.
* @param args the extra argument, must not be {@code null}
* @return A uni emitting {@code true} if the value was found, {@code false} otherwise
**/
Uni<List<Suggestion>> ftSugGet(K key, String prefix, GetArgs args);

/**
* Execute the command <a href="https://redis.io/commands/ft.suglen/">FT.SUGLEN</a>.
* Summary: Get the size of an auto-complete suggestion dictionary
* Group: auto-suggest
*
* @param key the suggestion dictionary key
* @return A uni emitting the current size of the suggestion dictionary.
**/
Uni<Long> ftSugLen(K key);

}
Loading

0 comments on commit 6eca42c

Please sign in to comment.