Skip to content

Commit

Permalink
fix: Char covert to String on remove specified unicode (#1664)
Browse files Browse the repository at this point in the history
  • Loading branch information
coderzc authored Dec 1, 2021
1 parent 5aab8b4 commit 83180fb
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ public class ConditionQuery extends IdQuery {
public static final String INDEX_VALUE_NULL = new String("<null>");
public static final String INDEX_VALUE_EMPTY = new String("<empty>");

public static final Set<String> IGNORE_SYM_SET;
static {
List<String> list = new ArrayList<>(INDEX_SYM_MAX - INDEX_SYM_MIN);
for (char ch = INDEX_SYM_MIN; ch <= INDEX_SYM_MAX; ch++) {
list.add(String.valueOf(ch));
}
IGNORE_SYM_SET = ImmutableSet.copyOf(list);
}

private static final Set<Condition> EMPTY_CONDITIONS = ImmutableSet.of();

// Conditions will be concated with `and` by default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,10 +875,9 @@ private boolean matchSearchIndexWords(String propValue, String fieldValue) {

private Set<String> segmentWords(String text) {
Set<String> words = this.textAnalyzer.segment(text);
for (char ch = ConditionQuery.INDEX_SYM_MIN;
ch <= ConditionQuery.INDEX_SYM_MAX; ch++) {
words.remove(String.valueOf(ch));
}

// Ignore unicode \u0000 to \u0003
words.removeAll(ConditionQuery.IGNORE_SYM_SET);
return words;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8663,6 +8663,77 @@ public void testQueryByHasIdEmptyListInPage() {
Assert.assertNull(page);
}

@Test
public void testQueryBySearchIndexWithSpecialSymbol() {
HugeGraph graph = graph();

graph.schema().indexLabel("personByName").onV("person")
.by("name").search().ifNotExist().create();

Vertex vertex = graph.addVertex(T.label, "person", "name",
"xyz\u0002abc", "city", "Hongkong",
"age", 15);
Vertex vertex2 = graph.addVertex(T.label, "person", "name",
"\u0002", "city", "Hongkong",
"age", 15);
Vertex vertex3 = graph.addVertex(T.label, "person", "name",
"xyz\u0003abc", "city", "Hongkong",
"age", 15);
Vertex vertex4 = graph.addVertex(T.label, "person", "name",
"\u0003", "city", "Hongkong",
"age", 15);
Vertex vertex5 = graph.addVertex(T.label, "person", "name",
"xyz\u0001abc", "city", "Hongkong",
"age", 15);
Vertex vertex6 = graph.addVertex(T.label, "person", "name",
"\u0001", "city", "Hongkong",
"age", 15);
graph.tx().commit();

GraphTraversalSource g = graph.traversal();

List<Vertex> vertices;
vertices = g.V().has("name", Text.contains("abc")).toList();
Assert.assertEquals(3, vertices.size());
Assert.assertTrue(vertices.contains(vertex));
Assert.assertTrue(vertices.contains(vertex3));
Assert.assertTrue(vertices.contains(vertex5));

vertices = g.V().has("name", Text.contains("\u0002")).toList();
Assert.assertEquals(0, vertices.size());

vertices = g.V().has("name", Text.contains("\u0003")).toList();
Assert.assertEquals(0, vertices.size());

vertices = g.V().has("name", Text.contains("\u0001")).toList();
Assert.assertEquals(0, vertices.size());

if (graph.backend().equals("postgresql")) {
Assert.assertThrows(BackendException.class, () -> {
graph.addVertex(T.label, "person", "name",
"\u0000", "city", "Hongkong",
"age", 15);
graph.tx().commit();
}, e -> {
graph.tx().rollback();
Assert.assertContains("invalid byte sequence for encoding " +
"\"UTF8\": 0x00",
e.getCause().getMessage());
});
} else {
graph.addVertex(T.label, "person", "name",
"xyz\u0000abc", "city", "Hongkong",
"age", 15);

graph.addVertex(T.label, "person", "name",
"\u0000", "city", "Hongkong",
"age", 15);

vertices = g.V().has("name", Text.contains("\u0000")).toList();
Assert.assertEquals(0, vertices.size());
}
}

private void init10Vertices() {
HugeGraph graph = graph();

Expand Down

0 comments on commit 83180fb

Please sign in to comment.