make cql.allIndexes configurable

This commit is contained in:
Jörg Prante 2021-07-30 14:45:19 +02:00
parent 81d29ef733
commit 67aa372666
4 changed files with 26 additions and 20 deletions

View file

@ -41,15 +41,18 @@ public class ElasticsearchFilterGenerator implements Visitor {
private final ElasticsearchQueryModel model; private final ElasticsearchQueryModel model;
private Stack<Node> stack; private final Stack<Node> stack;
private final String globalField;
private FilterGenerator filterGen; private FilterGenerator filterGen;
public ElasticsearchFilterGenerator() { public ElasticsearchFilterGenerator(String globalField) {
this(new ElasticsearchQueryModel()); this(globalField, new ElasticsearchQueryModel());
} }
public ElasticsearchFilterGenerator(ElasticsearchQueryModel model) { public ElasticsearchFilterGenerator(String globalField, ElasticsearchQueryModel model) {
this.globalField = globalField;
this.model = model; this.model = model;
this.stack = new Stack<>(); this.stack = new Stack<>();
try { try {
@ -84,7 +87,7 @@ public class ElasticsearchFilterGenerator implements Visitor {
node.getQuery().accept(this); node.getQuery().accept(this);
Node querynode = stack.pop(); Node querynode = stack.pop();
if (querynode instanceof Token) { if (querynode instanceof Token) {
filterGen.visit(new Expression(Operator.TERM_FILTER, new Name("cql.allIndexes"), querynode)); filterGen.visit(new Expression(Operator.TERM_FILTER, new Name(globalField), querynode));
} else if (querynode instanceof Expression) { } else if (querynode instanceof Expression) {
filterGen.visit(new Expression(Operator.QUERY_FILTER, (Expression) querynode)); filterGen.visit(new Expression(Operator.QUERY_FILTER, (Expression) querynode));
} }
@ -152,7 +155,7 @@ public class ElasticsearchFilterGenerator implements Visitor {
Node esnode = stack.pop(); Node esnode = stack.pop();
// add default context if node is a literal without a context // add default context if node is a literal without a context
if (esnode instanceof Token && TokenType.STRING.equals(esnode.getType())) { if (esnode instanceof Token && TokenType.STRING.equals(esnode.getType())) {
esnode = new Expression(Operator.ALL, new Name("cql.allIndexes"), esnode); esnode = new Expression(Operator.ALL, new Name(globalField), esnode);
} }
if (stack.isEmpty()) { if (stack.isEmpty()) {
// unary expression // unary expression
@ -162,7 +165,7 @@ public class ElasticsearchFilterGenerator implements Visitor {
Node esnode2 = stack.pop(); Node esnode2 = stack.pop();
// add default context if node is a literal without context // add default context if node is a literal without context
if (esnode2 instanceof Token && TokenType.STRING.equals(esnode2.getType())) { if (esnode2 instanceof Token && TokenType.STRING.equals(esnode2.getType())) {
esnode2 = new Expression(Operator.ALL, new Name("cql.allIndexes"), esnode2); esnode2 = new Expression(Operator.ALL, new Name(globalField), esnode2);
} }
esnode = new Expression(op, esnode2, esnode); esnode = new Expression(op, esnode2, esnode);
} }

View file

@ -69,11 +69,14 @@ public class ElasticsearchQueryGenerator implements Visitor {
private XContentBuilder sort; private XContentBuilder sort;
public ElasticsearchQueryGenerator() throws IOException { private String globalField;
public ElasticsearchQueryGenerator(String globalField) throws IOException {
this.globalField = globalField;
this.from = 0; this.from = 0;
this.size = 10; this.size = 10;
this.model = new ElasticsearchQueryModel(); this.model = new ElasticsearchQueryModel();
this.filterGenerator = new ElasticsearchFilterGenerator(model); this.filterGenerator = new ElasticsearchFilterGenerator(globalField, model);
this.stack = new Stack<>(); this.stack = new Stack<>();
this.sourceGen = new SourceGenerator(); this.sourceGen = new SourceGenerator();
this.queryGen = new QueryGenerator(); this.queryGen = new QueryGenerator();
@ -167,7 +170,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
Token token = (Token) querynode; Token token = (Token) querynode;
querynode = ".".equals(token.getString()) ? querynode = ".".equals(token.getString()) ?
new Expression(Operator.MATCH_ALL) : new Expression(Operator.MATCH_ALL) :
new Expression(Operator.EQUALS, new Name("cql.allIndexes"), querynode); new Expression(Operator.EQUALS, new Name(globalField), querynode);
} }
queryGen.visit((Expression) querynode); queryGen.visit((Expression) querynode);
if (model.hasFilter() && model.getFilterExpression() != null) { if (model.hasFilter() && model.getFilterExpression() != null) {
@ -276,7 +279,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
Node esnode = stack.pop(); Node esnode = stack.pop();
// add default context if node is a literal without a context // add default context if node is a literal without a context
if (esnode instanceof Token && TokenType.STRING.equals(esnode.getType())) { if (esnode instanceof Token && TokenType.STRING.equals(esnode.getType())) {
esnode = new Expression(Operator.EQUALS, new Name("cql.allIndexes"), esnode); esnode = new Expression(Operator.EQUALS, new Name(globalField), esnode);
} }
if (stack.isEmpty()) { if (stack.isEmpty()) {
// unary expression // unary expression
@ -286,7 +289,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
Node esnode2 = stack.pop(); Node esnode2 = stack.pop();
// add default context if node is a literal without context // add default context if node is a literal without context
if (esnode2 instanceof Token && TokenType.STRING.equals(esnode2.getType())) { if (esnode2 instanceof Token && TokenType.STRING.equals(esnode2.getType())) {
esnode2 = new Expression(Operator.EQUALS, new Name("cql.allIndexes"), esnode2); esnode2 = new Expression(Operator.EQUALS, new Name(globalField), esnode2);
} }
esnode = new Expression(op, esnode2, esnode); esnode = new Expression(op, esnode2, esnode);
} }

View file

@ -25,7 +25,7 @@ class ElasticsearchQueryTest {
String cql = "Jörg"; String cql = "Jörg";
CQLParser parser = new CQLParser(cql); CQLParser parser = new CQLParser(cql);
parser.parse(); parser.parse();
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator(); ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator("cql.allIndexes");
parser.getCQLQuery().accept(generator); parser.getCQLQuery().accept(generator);
String json = generator.getResult().string(); String json = generator.getResult().string();
assertEquals(json, "{\"term\":{\"cql.allIndexes\":\"Jörg\"}}"); assertEquals(json, "{\"term\":{\"cql.allIndexes\":\"Jörg\"}}");
@ -36,7 +36,7 @@ class ElasticsearchQueryTest {
String cql = "dc.type = electronic"; String cql = "dc.type = electronic";
CQLParser parser = new CQLParser(cql); CQLParser parser = new CQLParser(cql);
parser.parse(); parser.parse();
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator(); ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator("cql.allIndexes");
parser.getCQLQuery().accept(generator); parser.getCQLQuery().accept(generator);
String json = generator.getResult().string(); String json = generator.getResult().string();
assertEquals(json, "{\"query\":{\"term\":{\"dc.type\":\"electronic\"}}}"); assertEquals(json, "{\"query\":{\"term\":{\"dc.type\":\"electronic\"}}}");
@ -47,7 +47,7 @@ class ElasticsearchQueryTest {
String cql = "dc.type = electronic and dc.date = 2013"; String cql = "dc.type = electronic and dc.date = 2013";
CQLParser parser = new CQLParser(cql); CQLParser parser = new CQLParser(cql);
parser.parse(); parser.parse();
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator(); ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator("cql.allIndexes");
parser.getCQLQuery().accept(generator); parser.getCQLQuery().accept(generator);
String json = generator.getResult().string(); String json = generator.getResult().string();
assertEquals( assertEquals(
@ -61,7 +61,7 @@ class ElasticsearchQueryTest {
String cql = "dc.format = online and dc.type = electronic and dc.date = 2013"; String cql = "dc.format = online and dc.type = electronic and dc.date = 2013";
CQLParser parser = new CQLParser(cql); CQLParser parser = new CQLParser(cql);
parser.parse(); parser.parse();
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator(); ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator("cql.allIndexes");
parser.getCQLQuery().accept(generator); parser.getCQLQuery().accept(generator);
String json = generator.getResult().string(); String json = generator.getResult().string();
assertEquals( assertEquals(
@ -75,7 +75,7 @@ class ElasticsearchQueryTest {
String cql = "Jörg"; String cql = "Jörg";
CQLParser parser = new CQLParser(cql); CQLParser parser = new CQLParser(cql);
parser.parse(); parser.parse();
ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator(); ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator("cql.allIndexes");
generator.setBoostParams("boost", "log2p", 2.0f, "sum"); generator.setBoostParams("boost", "log2p", 2.0f, "sum");
parser.getCQLQuery().accept(generator); parser.getCQLQuery().accept(generator);
String json = generator.getSourceResult(); String json = generator.getSourceResult();
@ -92,7 +92,7 @@ class ElasticsearchQueryTest {
String cql = "dc.format = book*"; String cql = "dc.format = book*";
CQLParser parser = new CQLParser(cql); CQLParser parser = new CQLParser(cql);
parser.parse(); parser.parse();
ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator(); ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator("cql.allIndexes");
parser.getCQLQuery().accept(generator); parser.getCQLQuery().accept(generator);
String json = generator.getSourceResult(); String json = generator.getSourceResult();
assertEquals("{\"from\":0,\"size\":10,\"query\":{\"simple_query_string\":" + assertEquals("{\"from\":0,\"size\":10,\"query\":{\"simple_query_string\":" +
@ -130,7 +130,7 @@ class ElasticsearchQueryTest {
private void validate(String cql, String expected) throws Exception { private void validate(String cql, String expected) throws Exception {
CQLParser parser = new CQLParser(cql); CQLParser parser = new CQLParser(cql);
parser.parse(); parser.parse();
ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator(); ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator("cql.allIndexes");
parser.getCQLQuery().accept(generator); parser.getCQLQuery().accept(generator);
String elasticsearchQuery = generator.getSourceResult(); String elasticsearchQuery = generator.getSourceResult();
assertEquals(expected, elasticsearchQuery); assertEquals(expected, elasticsearchQuery);

View file

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