From 81d29ef7331591b3b24d1f118815f614353cf5dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=CC=88rg=20Prante?= Date: Tue, 15 Jun 2021 18:18:05 +0200 Subject: [PATCH] add null context for removing index from search clause --- cql-common/src/main/java/org/xbib/cql/Index.java | 14 ++++++++------ .../src/main/java/org/xbib/cql/SearchClause.java | 2 +- .../java/org/xbib/cql/model/CQLQueryModel.java | 9 +++++---- .../src/test/java/org/xbib/cql/QueryTest.java | 9 +++++++-- .../elasticsearch/ElasticsearchQueryGenerator.java | 2 +- gradle.properties | 2 +- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/cql-common/src/main/java/org/xbib/cql/Index.java b/cql-common/src/main/java/org/xbib/cql/Index.java index 59c0edf..9a57a11 100644 --- a/cql-common/src/main/java/org/xbib/cql/Index.java +++ b/cql-common/src/main/java/org/xbib/cql/Index.java @@ -16,11 +16,14 @@ public class Index extends AbstractNode { } public Index(String name) { + this.context = ""; this.name = name; - int pos = name.indexOf('.'); - if (pos > 0) { - this.context = name.substring(0, pos); - this.name = name.substring(pos + 1); + if (name != null) { + int pos = name.indexOf('.'); + if (pos > 0) { + this.context = name.substring(0, pos); + this.name = name.substring(pos + 1); + } } } @@ -54,7 +57,6 @@ public class Index extends AbstractNode { @Override public String toString() { - return context != null ? context + "." + name : name; + return context != null && !context.isEmpty() ? context + "." + name : name; } - } diff --git a/cql-common/src/main/java/org/xbib/cql/SearchClause.java b/cql-common/src/main/java/org/xbib/cql/SearchClause.java index 7e86fd8..011adc2 100644 --- a/cql-common/src/main/java/org/xbib/cql/SearchClause.java +++ b/cql-common/src/main/java/org/xbib/cql/SearchClause.java @@ -53,10 +53,10 @@ public class SearchClause extends AbstractNode { public String toString() { return query != null && query.toString().length() > 0 ? "(" + query + ")" : query != null ? "" + : index != null && index.getContext() == null && index.getName() == null && term != null ? term.toString() : index != null && !CQLQueryModel.isVisible(index.getContext()) ? "" : index != null ? index + " " + relation + " " + term : term != null ? term.toString() : ""; } - } diff --git a/cql-common/src/main/java/org/xbib/cql/model/CQLQueryModel.java b/cql-common/src/main/java/org/xbib/cql/model/CQLQueryModel.java index 9dcb75a..6a4e721 100644 --- a/cql-common/src/main/java/org/xbib/cql/model/CQLQueryModel.java +++ b/cql-common/src/main/java/org/xbib/cql/model/CQLQueryModel.java @@ -165,15 +165,16 @@ public final class CQLQueryModel { } /** - * Get query of a given context. + * Get query of a given context. * * @param context the context * @return true if visible, false if not */ public static boolean isVisible(String context) { - return !isFacetContext(context) - && !isFilterContext(context) - && !isOptionContext(context); + return context != null && + !isFacetContext(context) && + !isFilterContext(context) && + !isOptionContext(context); } /** diff --git a/cql-common/src/test/java/org/xbib/cql/QueryTest.java b/cql-common/src/test/java/org/xbib/cql/QueryTest.java index 93ed067..f2b0855 100644 --- a/cql-common/src/test/java/org/xbib/cql/QueryTest.java +++ b/cql-common/src/test/java/org/xbib/cql/QueryTest.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; import java.nio.charset.StandardCharsets; +import java.util.logging.Level; +import java.util.logging.Logger; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -14,6 +16,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; */ class QueryTest { + private static final Logger logger = Logger.getLogger(QueryTest.class.getName()); + @Test void testValidQueries() throws IOException { test("queries.txt"); @@ -37,14 +41,15 @@ class QueryTest { } ok++; } catch (Exception e) { + logger.log(Level.WARNING, e.getMessage()); errors++; } count++; } } lr.close(); - assertEquals(errors, 0); - assertEquals(ok, count); + assertEquals(0, errors); + assertEquals(count, ok); } private void validate(String line) throws Exception { diff --git a/cql-elasticsearch/src/main/java/org/xbib/cql/elasticsearch/ElasticsearchQueryGenerator.java b/cql-elasticsearch/src/main/java/org/xbib/cql/elasticsearch/ElasticsearchQueryGenerator.java index 9aa8bbf..139ced7 100644 --- a/cql-elasticsearch/src/main/java/org/xbib/cql/elasticsearch/ElasticsearchQueryGenerator.java +++ b/cql-elasticsearch/src/main/java/org/xbib/cql/elasticsearch/ElasticsearchQueryGenerator.java @@ -393,7 +393,7 @@ public class ElasticsearchQueryGenerator implements Visitor { @Override public void visit(Index node) { String context = node.getContext(); - String name = context != null ? context + "." + node.getName() : node.getName(); + String name = context != null && !context.isEmpty() ? context + "." + node.getName() : node.getName(); Name esname = new Name(name, model.getVisibility(context)); esname.setType(model.getElasticsearchType(name)); stack.push(esname); diff --git a/gradle.properties b/gradle.properties index 77c580a..e864746 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ group = org.xbib name = cql -version = 3.1.0 +version = 3.1.1 gradle.wrapper.version = 6.6.1 xbib-content.version = 3.0.0