diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java index 03406856cb..0cb880e70f 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/query/ConditionQuery.java @@ -61,6 +61,15 @@ public class ConditionQuery extends IdQuery { public static final String INDEX_VALUE_NULL = new String(""); public static final String INDEX_VALUE_EMPTY = new String(""); + public static final Set IGNORE_SYM_SET; + static { + List 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 EMPTY_CONDITIONS = ImmutableSet.of(); // Conditions will be concated with `and` by default diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java index 12b14f6ea5..68f91c195a 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/tx/GraphIndexTransaction.java @@ -875,10 +875,9 @@ private boolean matchSearchIndexWords(String propValue, String fieldValue) { private Set segmentWords(String text) { Set 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; } diff --git a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java index 8e602e7efe..e1707c1779 100644 --- a/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java +++ b/hugegraph-test/src/main/java/com/baidu/hugegraph/core/VertexCoreTest.java @@ -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 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();