Skip to content

Commit 7fd225d

Browse files
authored
Add support for RediSearch 2.2 ON JSON (#44)
1 parent 5257519 commit 7fd225d

File tree

9 files changed

+234
-15
lines changed

9 files changed

+234
-15
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
ports:
2424
- 6379:6379
2525
redisearch:
26-
image: redislabs/redisearch:2.0.9
26+
image: redislabs/redismod
2727
options: >-
2828
--health-cmd "redis-cli ping"
2929
--health-interval 10s

redisearch/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@
1515
<groupId>io.github.dengliming.redismodule</groupId>
1616
<artifactId>commons</artifactId>
1717
</dependency>
18+
19+
<dependency>
20+
<groupId>io.github.dengliming.redismodule</groupId>
21+
<artifactId>redisjson</artifactId>
22+
<scope>test</scope>
23+
</dependency>
1824
</dependencies>
1925

2026
</project>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 dengliming.
2+
* Copyright 2020-2022 dengliming.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -138,6 +138,10 @@ public RFuture<Boolean> alterIndexAsync(Field... fields) {
138138

139139
private void appendFieldArgs(List<Object> args, Field field) {
140140
args.add(field.getName());
141+
if (field.getAttribute() != null) {
142+
args.add(Keywords.AS);
143+
args.add(field.getAttribute());
144+
}
141145
args.add(field.getFieldType().name());
142146
switch (field.getFieldType()) {
143147
case TAG:
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2022 dengliming.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.github.dengliming.redismodule.redisearch.index;
18+
19+
import java.util.List;
20+
21+
public class IndexDefinition {
22+
23+
public enum DataType {
24+
HASH,
25+
JSON
26+
}
27+
28+
private final DataType dataType;
29+
30+
private boolean async;
31+
private List<String> prefixes;
32+
private String filter;
33+
private RSLanguage languageField;
34+
private RSLanguage language;
35+
private String scoreFiled;
36+
private double score = 1.0;
37+
private String payloadField;
38+
39+
public IndexDefinition() {
40+
this(DataType.HASH);
41+
}
42+
43+
public IndexDefinition(DataType dataType) {
44+
this.dataType = dataType;
45+
}
46+
47+
public DataType getDataType() {
48+
return dataType;
49+
}
50+
51+
public boolean isAsync() {
52+
return async;
53+
}
54+
55+
public IndexDefinition setAsync(boolean async) {
56+
this.async = async;
57+
return this;
58+
}
59+
60+
public List<String> getPrefixes() {
61+
return prefixes;
62+
}
63+
64+
public IndexDefinition setPrefixes(List<String> prefixes) {
65+
this.prefixes = prefixes;
66+
return this;
67+
}
68+
69+
public String getFilter() {
70+
return filter;
71+
}
72+
73+
public IndexDefinition setFilter(String filter) {
74+
this.filter = filter;
75+
return this;
76+
}
77+
78+
public RSLanguage getLanguageField() {
79+
return languageField;
80+
}
81+
82+
public IndexDefinition setLanguageField(RSLanguage languageField) {
83+
this.languageField = languageField;
84+
return this;
85+
}
86+
87+
public RSLanguage getLanguage() {
88+
return language;
89+
}
90+
91+
public IndexDefinition setLanguage(RSLanguage language) {
92+
this.language = language;
93+
return this;
94+
}
95+
96+
public String getScoreFiled() {
97+
return scoreFiled;
98+
}
99+
100+
public IndexDefinition setScoreFiled(String scoreFiled) {
101+
this.scoreFiled = scoreFiled;
102+
return this;
103+
}
104+
105+
public double getScore() {
106+
return score;
107+
}
108+
109+
public IndexDefinition setScore(double score) {
110+
this.score = score;
111+
return this;
112+
}
113+
114+
public String getPayloadField() {
115+
return payloadField;
116+
}
117+
118+
public IndexDefinition setPayloadField(String payloadField) {
119+
this.payloadField = payloadField;
120+
return this;
121+
}
122+
}

redisearch/src/main/java/io/github/dengliming/redismodule/redisearch/index/IndexOptions.java

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 dengliming.
2+
* Copyright 2020-2022 dengliming.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@ public class IndexOptions {
3333
private boolean noHL;
3434
private List<String> stopwords;
3535
private List<String> prefixes;
36+
private IndexDefinition definition;
3637

3738
public long getExpire() {
3839
return expire;
@@ -105,17 +106,60 @@ public IndexOptions prefixes(List<String> prefixes) {
105106
return this;
106107
}
107108

109+
public IndexOptions definition(IndexDefinition definition) {
110+
this.definition = definition;
111+
return this;
112+
}
113+
114+
public IndexDefinition getDefinition() {
115+
return definition;
116+
}
117+
108118
public static IndexOptions defaultOptions() {
109119
return new IndexOptions();
110120
}
111121

112122
public void build(List<Object> args) {
113-
114-
if (getPrefixes() != null) {
115-
args.add(Keywords.PREFIX.name());
116-
args.add(getPrefixes().size());
117-
args.addAll(getPrefixes());
123+
definition = getDefinition();
124+
if (definition != null) {
125+
args.add(Keywords.ON);
126+
args.add(definition.getDataType());
127+
128+
if (definition.getPrefixes() != null) {
129+
args.add(Keywords.PREFIX.name());
130+
args.add(definition.getPrefixes().size());
131+
args.addAll(definition.getPrefixes());
132+
}
133+
134+
if (definition.getFilter() != null) {
135+
args.add(Keywords.FILTER);
136+
args.add(definition.getFilter());
137+
}
138+
139+
if (definition.getLanguage() != null) {
140+
args.add(Keywords.LANGUAGE);
141+
args.add(definition.getLanguage());
142+
}
143+
144+
if (definition.getLanguageField() != null) {
145+
args.add(Keywords.LANGUAGE_FIELD);
146+
args.add(definition.getLanguageField());
147+
}
148+
149+
args.add(Keywords.SCORE);
150+
args.add(definition.getScore());
151+
152+
if (definition.getScoreFiled() != null) {
153+
args.add(Keywords.SCORE_FIELD);
154+
args.add(definition.getScoreFiled());
155+
}
156+
157+
if (definition.getPayloadField() != null) {
158+
args.add(Keywords.PAYLOAD_FIELD);
159+
args.add(definition.getPayloadField());
160+
}
118161
}
162+
119163
if (isMaxTextFields()) {
120164
args.add(Keywords.MAXTEXTFIELDS.name());
121165
}

redisearch/src/main/java/io/github/dengliming/redismodule/redisearch/index/schema/Field.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 dengliming.
2+
* Copyright 2020-2022 dengliming.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
*/
2222
public class Field {
2323
private String name;
24+
private String attribute;
2425
private boolean sortable;
2526
private boolean noIndex;
2627
private FieldType fieldType;
@@ -55,4 +56,13 @@ public boolean isNoIndex() {
5556
public FieldType getFieldType() {
5657
return fieldType;
5758
}
59+
60+
public String getAttribute() {
61+
return attribute;
62+
}
63+
64+
public Field attribute(String attribute) {
65+
this.attribute = attribute;
66+
return this;
67+
}
5868
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
* @author dengliming
2121
*/
2222
public enum Keywords {
23-
MAXTEXTFIELDS, TEMPORARY, NOOFFSETS, NOHL, FUZZY, WITHSCORES, WITHPAYLOADS, MAX, PAYLOAD, INCR, LANGUAGE, REPLACE,
23+
MAXTEXTFIELDS, TEMPORARY, NOOFFSETS, NOHL, FUZZY, WITHSCORES, WITHPAYLOADS, MAX, PAYLOAD, INCR, LANGUAGE, LANGUAGE_FIELD, REPLACE, ON,
2424
DD, FIELDS, IF, NOSAVE, KEEPDOCS, NOINDEX, SORTABLE, PHONETIC, NOSTEM, WEIGHT, SEPARATOR, ADD, SCHEMA, STOPWORDS, PREFIX, NOFREQS, NOFIELDS,
25-
NOCONTENT, VERBATIM, NOSTOPWORDS, WITHSORTKEYS, FILTER, GEOFILTER, INKEYS, INFIELDS, RETURN, SUMMARIZE, FRAGS, LEN,
26-
HIGHLIGHT, TAGS, SLOP, INORDER, EXPANDER, SCORER, EXPLAINSCORE, SORTBY, LIMIT, AVG, REDUCE, STDDEV, COUNT, COUNT_DISTINCT,
25+
NOCONTENT, VERBATIM, NOSTOPWORDS, WITHSORTKEYS, FILTER, GEOFILTER, INKEYS, INFIELDS, RETURN, SUMMARIZE, FRAGS, LEN, SCORE, SCORE_FIELD,
26+
HIGHLIGHT, TAGS, SLOP, INORDER, EXPANDER, SCORER, EXPLAINSCORE, SORTBY, LIMIT, AVG, REDUCE, STDDEV, COUNT, COUNT_DISTINCT, PAYLOAD_FIELD,
2727
COUNT_DISTINCTISH, SUM, MIN, QUANTILE, TOLIST, FIRST_VALUE, BY, RANDOM_SAMPLE, GROUPBY, LOAD, APPLY, AS, DISTANCE, INCLUDE, EXCLUDE, TERMS;
2828
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 dengliming.
2+
* Copyright 2020-2022 dengliming.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import io.github.dengliming.redismodule.common.util.TestSettings;
2020
import io.github.dengliming.redismodule.redisearch.client.RediSearchClient;
21+
import io.github.dengliming.redismodule.redisjson.client.RedisJSONClient;
2122
import org.junit.jupiter.api.AfterEach;
2223
import org.junit.jupiter.api.BeforeEach;
2324
import org.redisson.config.Config;
@@ -28,13 +29,16 @@
2829
public abstract class AbstractTest {
2930

3031
private RediSearchClient rediSearchClient;
32+
private RedisJSONClient redisJSONClient;
3133

3234
@BeforeEach
3335
public void init() {
3436
Config config = new Config();
3537
config.useSingleServer().setAddress("redis://" + TestSettings.host() + ":" + TestSettings.port());
3638
rediSearchClient = new RediSearchClient(config);
39+
redisJSONClient = new RedisJSONClient(config);
3740
rediSearchClient.flushall();
41+
redisJSONClient.flushall();
3842
}
3943

4044
@AfterEach
@@ -47,4 +51,9 @@ public void destroy() {
4751
protected RediSearchClient getRediSearchClient() {
4852
return rediSearchClient;
4953
}
54+
55+
protected RedisJSONClient getRedisJSONClient() {
56+
return redisJSONClient;
57+
}
5058
}
59+

0 commit comments

Comments
 (0)