add null context for removing index from search clause

This commit is contained in:
Jörg Prante 2021-06-15 18:18:05 +02:00
parent f83ae36758
commit 81d29ef733
6 changed files with 23 additions and 15 deletions

View file

@ -16,11 +16,14 @@ public class Index extends AbstractNode {
} }
public Index(String name) { public Index(String name) {
this.context = "";
this.name = name; this.name = name;
int pos = name.indexOf('.'); if (name != null) {
if (pos > 0) { int pos = name.indexOf('.');
this.context = name.substring(0, pos); if (pos > 0) {
this.name = name.substring(pos + 1); this.context = name.substring(0, pos);
this.name = name.substring(pos + 1);
}
} }
} }
@ -54,7 +57,6 @@ public class Index extends AbstractNode {
@Override @Override
public String toString() { public String toString() {
return context != null ? context + "." + name : name; return context != null && !context.isEmpty() ? context + "." + name : name;
} }
} }

View file

@ -53,10 +53,10 @@ public class SearchClause extends AbstractNode {
public String toString() { public String toString() {
return query != null && query.toString().length() > 0 ? "(" + query + ")" return query != null && query.toString().length() > 0 ? "(" + query + ")"
: query != null ? "" : query != null ? ""
: index != null && index.getContext() == null && index.getName() == null && term != null ? term.toString()
: index != null && !CQLQueryModel.isVisible(index.getContext()) ? "" : index != null && !CQLQueryModel.isVisible(index.getContext()) ? ""
: index != null ? index + " " + relation + " " + term : index != null ? index + " " + relation + " " + term
: term != null ? term.toString() : term != null ? term.toString()
: ""; : "";
} }
} }

View file

@ -165,15 +165,16 @@ public final class CQLQueryModel {
} }
/** /**
* Get query of a given context. * Get query of a given context.
* *
* @param context the context * @param context the context
* @return true if visible, false if not * @return true if visible, false if not
*/ */
public static boolean isVisible(String context) { public static boolean isVisible(String context) {
return !isFacetContext(context) return context != null &&
&& !isFilterContext(context) !isFacetContext(context) &&
&& !isOptionContext(context); !isFilterContext(context) &&
!isOptionContext(context);
} }
/** /**

View file

@ -6,6 +6,8 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.LineNumberReader; import java.io.LineNumberReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -14,6 +16,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
*/ */
class QueryTest { class QueryTest {
private static final Logger logger = Logger.getLogger(QueryTest.class.getName());
@Test @Test
void testValidQueries() throws IOException { void testValidQueries() throws IOException {
test("queries.txt"); test("queries.txt");
@ -37,14 +41,15 @@ class QueryTest {
} }
ok++; ok++;
} catch (Exception e) { } catch (Exception e) {
logger.log(Level.WARNING, e.getMessage());
errors++; errors++;
} }
count++; count++;
} }
} }
lr.close(); lr.close();
assertEquals(errors, 0); assertEquals(0, errors);
assertEquals(ok, count); assertEquals(count, ok);
} }
private void validate(String line) throws Exception { private void validate(String line) throws Exception {

View file

@ -393,7 +393,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
@Override @Override
public void visit(Index node) { public void visit(Index node) {
String context = node.getContext(); 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)); Name esname = new Name(name, model.getVisibility(context));
esname.setType(model.getElasticsearchType(name)); esname.setType(model.getElasticsearchType(name));
stack.push(esname); stack.push(esname);

View file

@ -1,6 +1,6 @@
group = org.xbib group = org.xbib
name = cql name = cql
version = 3.1.0 version = 3.1.1
gradle.wrapper.version = 6.6.1 gradle.wrapper.version = 6.6.1
xbib-content.version = 3.0.0 xbib-content.version = 3.0.0