Skip to content

Commit

Permalink
Fix redisearch no working with page option.
Browse files Browse the repository at this point in the history
  • Loading branch information
dengliming committed Nov 14, 2022
1 parent 36c0433 commit eff2b0e
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import io.github.dengliming.redismodule.redisearch.index.schema.TagField;
import io.github.dengliming.redismodule.redisearch.index.schema.TextField;
import io.github.dengliming.redismodule.redisearch.protocol.Keywords;
import io.github.dengliming.redismodule.redisearch.protocol.decoder.SearchResultDecoder;
import io.github.dengliming.redismodule.redisearch.protocol.decoder.StringMapInfoDecoder;
import io.github.dengliming.redismodule.redisearch.search.MisspelledTerm;
import io.github.dengliming.redismodule.redisearch.search.SearchOptions;
import io.github.dengliming.redismodule.redisearch.search.SearchResult;
Expand All @@ -40,6 +42,8 @@
import org.redisson.api.RFuture;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.decoder.ListMultiDecoder2;
import org.redisson.command.CommandAsyncExecutor;

import java.util.ArrayList;
Expand Down Expand Up @@ -68,8 +72,6 @@
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_INFO;
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_LIST;
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_MGET;
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SEARCH;
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SEARCH_WITH_SCORES;
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SPELLCHECK;
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SUGADD;
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SUGDEL;
Expand Down Expand Up @@ -616,10 +618,13 @@ public RFuture<SearchResult> searchAsync(String query, SearchOptions searchOptio
args.add(getName());
args.add(query);
searchOptions.build(args);
RedisCommand command = new RedisCommand<>("FT.SEARCH", new ListMultiDecoder2(new SearchResultDecoder(
searchOptions.isWithScores(), searchOptions.isNoContent()
), new StringMapInfoDecoder()));
return commandExecutor.readAsync(
getName(),
StringCodec.INSTANCE,
searchOptions.isWithScores() ? FT_SEARCH_WITH_SCORES : FT_SEARCH,
command,
args.toArray()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import io.github.dengliming.redismodule.redisearch.protocol.decoder.AggregateDecoder;
import io.github.dengliming.redismodule.redisearch.protocol.decoder.MisspelledTermDecoder;
import io.github.dengliming.redismodule.redisearch.protocol.decoder.SearchResultDecoder;
import io.github.dengliming.redismodule.redisearch.protocol.decoder.StringMapInfoDecoder;
import org.redisson.client.protocol.RedisCommand;
import org.redisson.client.protocol.convertor.BooleanReplayConvertor;
Expand Down Expand Up @@ -47,8 +46,6 @@ public interface RedisCommands {
RedisCommand FT_ALIASDEL = new RedisCommand<>("FT.ALIASDEL", new BooleanReplayConvertor());

RedisCommand FT_INFO = new RedisCommand<>("FT.INFO", new ListMultiDecoder2(new StringMapInfoDecoder(), new CodecDecoder(), new CodecDecoder()));
RedisCommand FT_SEARCH = new RedisCommand<>("FT.SEARCH", new ListMultiDecoder2(new SearchResultDecoder(), new StringMapInfoDecoder()));
RedisCommand FT_SEARCH_WITH_SCORES = new RedisCommand<>("FT.SEARCH", new ListMultiDecoder2(new SearchResultDecoder(true), new StringMapInfoDecoder()));
RedisCommand FT_AGGREGATE = new RedisCommand<>("FT.AGGREGATE", new ListMultiDecoder2(new AggregateDecoder(), new ObjectMapReplayDecoder()));

RedisCommand FT_EXPLAIN = new RedisCommand<>("FT.EXPLAIN");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,18 @@
*/
public class SearchResultDecoder implements MultiDecoder<SearchResult> {

private boolean withScores;
private final boolean withScores;
private final boolean noContent;

public SearchResultDecoder() {
}

public SearchResultDecoder(boolean withScores) {
public SearchResultDecoder(boolean withScores, boolean noContent) {
this.withScores = withScores;
this.noContent = noContent;
}

@Override
public SearchResult decode(List<Object> parts, State state) {
Long total = (Long) parts.get(0);
int documentSize = withScores ? 3 : 2;
boolean noContent = total.longValue() == parts.size() - 1;

List<Document> documents = new ArrayList<>(total.intValue());
// Checks the document size. DocumentSize equals to 2 means only key and parts. DocumentSize equals to 3 means
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public SearchOptions() {
this.filters = new LinkedList<>();
}

public boolean isNoContent() {
return noContent;
}

public SearchOptions noContent() {
this.noContent = true;
return this;
Expand Down Expand Up @@ -137,6 +141,11 @@ public SearchOptions page(Page page) {
return this;
}

public SearchOptions page(int offset, int num) {
this.page = new Page(offset, num);
return this;
}

public SearchOptions scorer(String scorer) {
this.scorer = scorer;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,9 @@ public void testSearch() {
fields2.put("content", "hello world");
assertThat(rediSearch.addDocument(new Document(String.format("doc2"), 0.2d, fields2), new DocumentOptions())).isTrue();

SearchResult searchResult = rediSearch.search("Hi", new SearchOptions().noContent());
SearchResult searchResult = rediSearch.search("*", new SearchOptions().noContent().page(0, 1));
assertThat(searchResult.getTotal()).isEqualTo(2);
assertThat(searchResult.getDocuments().size()).isEqualTo(1);

searchResult = rediSearch.search("OOOO", new SearchOptions().noStopwords().language(RSLanguage.ENGLISH));
assertThat(searchResult.getTotal()).isEqualTo(1);
Expand Down Expand Up @@ -219,8 +220,6 @@ public void testDict() {
public void testSynonym() {
RediSearch rediSearch = getRediSearchClient().getRediSearch("testSynonym");
assertThat(rediSearch.createIndex(new Schema().addField(new TextField("title")))).isTrue();
Map<String, Object> fields = new HashMap<>();
fields.put("title", "Hi~");
long gid = rediSearch.addSynonym("a", "b", "c");
assertThat(gid).isGreaterThanOrEqualTo(0);

Expand Down

0 comments on commit eff2b0e

Please sign in to comment.