Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to create text/tag fields with option WITHSUFFIXTRIE #3567

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/main/java/redis/clients/jedis/search/Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public static class TextField extends Field {
private final double weight;
private final boolean nostem;
private final String phonetic;
private final boolean withSuffixTrie;

public TextField(String name) {
this(name, 1.0);
Expand All @@ -252,17 +253,27 @@ public TextField(String name, double weight, boolean sortable, boolean nostem, b
}

public TextField(String name, double weight, boolean sortable, boolean nostem, boolean noindex, String phonetic) {
this(name, weight, sortable, nostem, noindex, phonetic, false);
}

public TextField(String name, double weight, boolean sortable, boolean nostem, boolean noindex, String phonetic, boolean withSuffixTrie) {
super(name, FieldType.TEXT, sortable, noindex);
this.weight = weight;
this.nostem = nostem;
this.phonetic = phonetic;
this.withSuffixTrie = withSuffixTrie;
}

public TextField(FieldName name, double weight, boolean sortable, boolean nostem, boolean noindex, String phonetic) {
this(name, weight, sortable, nostem, noindex, phonetic, false);
}

public TextField(FieldName name, double weight, boolean sortable, boolean nostem, boolean noindex, String phonetic, boolean withSuffixTrie) {
super(name, FieldType.TEXT, sortable, noindex);
this.weight = weight;
this.nostem = nostem;
this.phonetic = phonetic;
this.withSuffixTrie = withSuffixTrie;
}

@Override
Expand All @@ -278,19 +289,23 @@ protected void addTypeArgs(CommandArguments args) {
args.add("PHONETIC");
args.add(this.phonetic);
}
if (withSuffixTrie) {
args.add("WITHSUFFIXTRIE");
}
}

@Override
public String toString() {
return "TextField{name='" + fieldName + "', type=" + type + ", sortable=" + sortable + ", noindex=" + noIndex
+ ", weight=" + weight + ", nostem=" + nostem + ", phonetic='" + phonetic + "'}";
+ ", weight=" + weight + ", nostem=" + nostem + ", phonetic='" + phonetic + ", withSuffixTrie='" + withSuffixTrie + "'}";
}
}

public static class TagField extends Field {

private final String separator;
private final boolean caseSensitive;
private final boolean withSuffixTrie;

public TagField(String name) {
this(name, null);
Expand All @@ -313,19 +328,29 @@ public TagField(String name, boolean caseSensitive, boolean sortable) {
}

public TagField(String name, String separator, boolean caseSensitive, boolean sortable) {
this(name, separator, caseSensitive, sortable, false);
}

public TagField(String name, String separator, boolean caseSensitive, boolean sortable, boolean withSuffixTrie) {
super(name, FieldType.TAG, sortable);
this.separator = separator;
this.caseSensitive = caseSensitive;
this.withSuffixTrie = withSuffixTrie;
}

public TagField(FieldName name, String separator, boolean sortable) {
this(name, separator, false, sortable);
}

public TagField(FieldName name, String separator, boolean caseSensitive, boolean sortable) {
this(name, separator, caseSensitive, sortable, false);
}

public TagField(FieldName name, String separator, boolean caseSensitive, boolean sortable, boolean withSuffixTrie) {
super(name, FieldType.TAG, sortable, false);
this.separator = separator;
this.caseSensitive = caseSensitive;
this.withSuffixTrie = withSuffixTrie;
}

@Override
Expand All @@ -337,12 +362,15 @@ public void addTypeArgs(CommandArguments args) {
if (caseSensitive) {
args.add("CASESENSITIVE");
}
if (withSuffixTrie) {
args.add("WITHSUFFIXTRIE");
}
}

@Override
public String toString() {
return "TagField{name='" + fieldName + "', type=" + type + ", sortable=" + sortable + ", noindex=" + noIndex
+ ", separator='" + separator + ", caseSensitive='" + caseSensitive + "'}";
+ ", separator='" + separator + ", caseSensitive='" + caseSensitive + ", withSuffixTrie='" + withSuffixTrie + "'}";
}
}

Expand Down
59 changes: 59 additions & 0 deletions src/test/java/redis/clients/jedis/modules/search/SearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1270,4 +1270,63 @@ public void searchIteration() throws Exception {
}
assertEquals(7, total);
}

@Test
public void testCreateIndexWithTextFieldWithSuffixTrie() {
Schema schema1 = new Schema()
.addField(new TextField("withSuffixTrie", 1.0, false, false, false, null, true));
Schema schema2 = new Schema()
.addField(new TextField("withSuffixTrie", 1.0, false, true, false, null, true));
Schema schema3 = new Schema()
.addField(new TextField("withSuffixTrie", 1.0, false, false, false, "dm:en", true));
Schema schema4 = new Schema()
.addField(new TextField("withSuffixTrie", 1.0, false, true, false, "dm:en", true));
Schema schema5 = new Schema()
.addField(new TextField("withSuffixTrie", 1.0, true, true, true, "dm:en", true));

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema1));
client.ftDropIndex(index);

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema2));
client.ftDropIndex(index);

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema3));
client.ftDropIndex(index);

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema4));
client.ftDropIndex(index);

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema5));
client.ftDropIndex(index);
}

@Test
public void testCreateIndexWithTagFieldWithSuffixTrie() {
Schema schema1 = new Schema()
.addField(new TagField("withSuffixTrie", null, false, false, true));
Schema schema2 = new Schema()
.addField(new TagField("withSuffixTrie", "|", false, false, true));
Schema schema3 = new Schema()
.addField(new TagField("withSuffixTrie", "|", true, false, true));
Schema schema4 = new Schema()
.addField(new TagField("withSuffixTrie", "|", false, true, true));
Schema schema5 = new Schema()
.addField(new TagField("withSuffixTrie", "|", true, true, true));

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema1));
client.ftDropIndex(index);

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema2));
client.ftDropIndex(index);

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema3));
client.ftDropIndex(index);

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema4));
client.ftDropIndex(index);

assertEquals("OK", client.ftCreate(index, IndexOptions.defaultOptions(), schema5));
client.ftDropIndex(index);
}

}