Skip to content

Commit eff2b0e

Browse files
committed
Fix redisearch no working with page option.
1 parent 36c0433 commit eff2b0e

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

redisearch/src/main/java/io/github/dengliming/redismodule/redisearch/RediSearch.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import io.github.dengliming.redismodule.redisearch.index.schema.TagField;
3333
import io.github.dengliming.redismodule.redisearch.index.schema.TextField;
3434
import io.github.dengliming.redismodule.redisearch.protocol.Keywords;
35+
import io.github.dengliming.redismodule.redisearch.protocol.decoder.SearchResultDecoder;
36+
import io.github.dengliming.redismodule.redisearch.protocol.decoder.StringMapInfoDecoder;
3537
import io.github.dengliming.redismodule.redisearch.search.MisspelledTerm;
3638
import io.github.dengliming.redismodule.redisearch.search.SearchOptions;
3739
import io.github.dengliming.redismodule.redisearch.search.SearchResult;
@@ -40,6 +42,8 @@
4042
import org.redisson.api.RFuture;
4143
import org.redisson.client.codec.Codec;
4244
import org.redisson.client.codec.StringCodec;
45+
import org.redisson.client.protocol.RedisCommand;
46+
import org.redisson.client.protocol.decoder.ListMultiDecoder2;
4347
import org.redisson.command.CommandAsyncExecutor;
4448

4549
import java.util.ArrayList;
@@ -68,8 +72,6 @@
6872
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_INFO;
6973
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_LIST;
7074
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_MGET;
71-
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SEARCH;
72-
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SEARCH_WITH_SCORES;
7375
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SPELLCHECK;
7476
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SUGADD;
7577
import static io.github.dengliming.redismodule.redisearch.protocol.RedisCommands.FT_SUGDEL;
@@ -616,10 +618,13 @@ public RFuture<SearchResult> searchAsync(String query, SearchOptions searchOptio
616618
args.add(getName());
617619
args.add(query);
618620
searchOptions.build(args);
621+
RedisCommand command = new RedisCommand<>("FT.SEARCH", new ListMultiDecoder2(new SearchResultDecoder(
622+
searchOptions.isWithScores(), searchOptions.isNoContent()
623+
), new StringMapInfoDecoder()));
619624
return commandExecutor.readAsync(
620625
getName(),
621626
StringCodec.INSTANCE,
622-
searchOptions.isWithScores() ? FT_SEARCH_WITH_SCORES : FT_SEARCH,
627+
command,
623628
args.toArray()
624629
);
625630
}

redisearch/src/main/java/io/github/dengliming/redismodule/redisearch/protocol/RedisCommands.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import io.github.dengliming.redismodule.redisearch.protocol.decoder.AggregateDecoder;
2020
import io.github.dengliming.redismodule.redisearch.protocol.decoder.MisspelledTermDecoder;
21-
import io.github.dengliming.redismodule.redisearch.protocol.decoder.SearchResultDecoder;
2221
import io.github.dengliming.redismodule.redisearch.protocol.decoder.StringMapInfoDecoder;
2322
import org.redisson.client.protocol.RedisCommand;
2423
import org.redisson.client.protocol.convertor.BooleanReplayConvertor;
@@ -47,8 +46,6 @@ public interface RedisCommands {
4746
RedisCommand FT_ALIASDEL = new RedisCommand<>("FT.ALIASDEL", new BooleanReplayConvertor());
4847

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

5451
RedisCommand FT_EXPLAIN = new RedisCommand<>("FT.EXPLAIN");

redisearch/src/main/java/io/github/dengliming/redismodule/redisearch/protocol/decoder/SearchResultDecoder.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,18 @@
3030
*/
3131
public class SearchResultDecoder implements MultiDecoder<SearchResult> {
3232

33-
private boolean withScores;
33+
private final boolean withScores;
34+
private final boolean noContent;
3435

35-
public SearchResultDecoder() {
36-
}
37-
38-
public SearchResultDecoder(boolean withScores) {
36+
public SearchResultDecoder(boolean withScores, boolean noContent) {
3937
this.withScores = withScores;
38+
this.noContent = noContent;
4039
}
4140

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

4846
List<Document> documents = new ArrayList<>(total.intValue());
4947
// Checks the document size. DocumentSize equals to 2 means only key and parts. DocumentSize equals to 3 means

redisearch/src/main/java/io/github/dengliming/redismodule/redisearch/search/SearchOptions.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public SearchOptions() {
5252
this.filters = new LinkedList<>();
5353
}
5454

55+
public boolean isNoContent() {
56+
return noContent;
57+
}
58+
5559
public SearchOptions noContent() {
5660
this.noContent = true;
5761
return this;
@@ -137,6 +141,11 @@ public SearchOptions page(Page page) {
137141
return this;
138142
}
139143

144+
public SearchOptions page(int offset, int num) {
145+
this.page = new Page(offset, num);
146+
return this;
147+
}
148+
140149
public SearchOptions scorer(String scorer) {
141150
this.scorer = scorer;
142151
return this;

redisearch/src/test/java/io/github/dengliming/redismodule/redisearch/RediSearchTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,9 @@ public void testSearch() {
160160
fields2.put("content", "hello world");
161161
assertThat(rediSearch.addDocument(new Document(String.format("doc2"), 0.2d, fields2), new DocumentOptions())).isTrue();
162162

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

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

0 commit comments

Comments
 (0)