make cql.allIndexes configurable
This commit is contained in:
parent
81d29ef733
commit
67aa372666
4 changed files with 26 additions and 20 deletions
|
@ -41,15 +41,18 @@ public class ElasticsearchFilterGenerator implements Visitor {
|
|||
|
||||
private final ElasticsearchQueryModel model;
|
||||
|
||||
private Stack<Node> stack;
|
||||
private final Stack<Node> stack;
|
||||
|
||||
private final String globalField;
|
||||
|
||||
private FilterGenerator filterGen;
|
||||
|
||||
public ElasticsearchFilterGenerator() {
|
||||
this(new ElasticsearchQueryModel());
|
||||
public ElasticsearchFilterGenerator(String globalField) {
|
||||
this(globalField, new ElasticsearchQueryModel());
|
||||
}
|
||||
|
||||
public ElasticsearchFilterGenerator(ElasticsearchQueryModel model) {
|
||||
public ElasticsearchFilterGenerator(String globalField, ElasticsearchQueryModel model) {
|
||||
this.globalField = globalField;
|
||||
this.model = model;
|
||||
this.stack = new Stack<>();
|
||||
try {
|
||||
|
@ -84,7 +87,7 @@ public class ElasticsearchFilterGenerator implements Visitor {
|
|||
node.getQuery().accept(this);
|
||||
Node querynode = stack.pop();
|
||||
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) {
|
||||
filterGen.visit(new Expression(Operator.QUERY_FILTER, (Expression) querynode));
|
||||
}
|
||||
|
@ -152,7 +155,7 @@ public class ElasticsearchFilterGenerator implements Visitor {
|
|||
Node esnode = stack.pop();
|
||||
// add default context if node is a literal without a context
|
||||
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()) {
|
||||
// unary expression
|
||||
|
@ -162,7 +165,7 @@ public class ElasticsearchFilterGenerator implements Visitor {
|
|||
Node esnode2 = stack.pop();
|
||||
// add default context if node is a literal without context
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -69,11 +69,14 @@ public class ElasticsearchQueryGenerator implements Visitor {
|
|||
|
||||
private XContentBuilder sort;
|
||||
|
||||
public ElasticsearchQueryGenerator() throws IOException {
|
||||
private String globalField;
|
||||
|
||||
public ElasticsearchQueryGenerator(String globalField) throws IOException {
|
||||
this.globalField = globalField;
|
||||
this.from = 0;
|
||||
this.size = 10;
|
||||
this.model = new ElasticsearchQueryModel();
|
||||
this.filterGenerator = new ElasticsearchFilterGenerator(model);
|
||||
this.filterGenerator = new ElasticsearchFilterGenerator(globalField, model);
|
||||
this.stack = new Stack<>();
|
||||
this.sourceGen = new SourceGenerator();
|
||||
this.queryGen = new QueryGenerator();
|
||||
|
@ -167,7 +170,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
|
|||
Token token = (Token) querynode;
|
||||
querynode = ".".equals(token.getString()) ?
|
||||
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);
|
||||
if (model.hasFilter() && model.getFilterExpression() != null) {
|
||||
|
@ -276,7 +279,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
|
|||
Node esnode = stack.pop();
|
||||
// add default context if node is a literal without a context
|
||||
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()) {
|
||||
// unary expression
|
||||
|
@ -286,7 +289,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
|
|||
Node esnode2 = stack.pop();
|
||||
// add default context if node is a literal without context
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ class ElasticsearchQueryTest {
|
|||
String cql = "Jörg";
|
||||
CQLParser parser = new CQLParser(cql);
|
||||
parser.parse();
|
||||
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator();
|
||||
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator("cql.allIndexes");
|
||||
parser.getCQLQuery().accept(generator);
|
||||
String json = generator.getResult().string();
|
||||
assertEquals(json, "{\"term\":{\"cql.allIndexes\":\"Jörg\"}}");
|
||||
|
@ -36,7 +36,7 @@ class ElasticsearchQueryTest {
|
|||
String cql = "dc.type = electronic";
|
||||
CQLParser parser = new CQLParser(cql);
|
||||
parser.parse();
|
||||
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator();
|
||||
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator("cql.allIndexes");
|
||||
parser.getCQLQuery().accept(generator);
|
||||
String json = generator.getResult().string();
|
||||
assertEquals(json, "{\"query\":{\"term\":{\"dc.type\":\"electronic\"}}}");
|
||||
|
@ -47,7 +47,7 @@ class ElasticsearchQueryTest {
|
|||
String cql = "dc.type = electronic and dc.date = 2013";
|
||||
CQLParser parser = new CQLParser(cql);
|
||||
parser.parse();
|
||||
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator();
|
||||
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator("cql.allIndexes");
|
||||
parser.getCQLQuery().accept(generator);
|
||||
String json = generator.getResult().string();
|
||||
assertEquals(
|
||||
|
@ -61,7 +61,7 @@ class ElasticsearchQueryTest {
|
|||
String cql = "dc.format = online and dc.type = electronic and dc.date = 2013";
|
||||
CQLParser parser = new CQLParser(cql);
|
||||
parser.parse();
|
||||
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator();
|
||||
ElasticsearchFilterGenerator generator = new ElasticsearchFilterGenerator("cql.allIndexes");
|
||||
parser.getCQLQuery().accept(generator);
|
||||
String json = generator.getResult().string();
|
||||
assertEquals(
|
||||
|
@ -75,7 +75,7 @@ class ElasticsearchQueryTest {
|
|||
String cql = "Jörg";
|
||||
CQLParser parser = new CQLParser(cql);
|
||||
parser.parse();
|
||||
ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator();
|
||||
ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator("cql.allIndexes");
|
||||
generator.setBoostParams("boost", "log2p", 2.0f, "sum");
|
||||
parser.getCQLQuery().accept(generator);
|
||||
String json = generator.getSourceResult();
|
||||
|
@ -92,7 +92,7 @@ class ElasticsearchQueryTest {
|
|||
String cql = "dc.format = book*";
|
||||
CQLParser parser = new CQLParser(cql);
|
||||
parser.parse();
|
||||
ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator();
|
||||
ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator("cql.allIndexes");
|
||||
parser.getCQLQuery().accept(generator);
|
||||
String json = generator.getSourceResult();
|
||||
assertEquals("{\"from\":0,\"size\":10,\"query\":{\"simple_query_string\":" +
|
||||
|
@ -130,7 +130,7 @@ class ElasticsearchQueryTest {
|
|||
private void validate(String cql, String expected) throws Exception {
|
||||
CQLParser parser = new CQLParser(cql);
|
||||
parser.parse();
|
||||
ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator();
|
||||
ElasticsearchQueryGenerator generator = new ElasticsearchQueryGenerator("cql.allIndexes");
|
||||
parser.getCQLQuery().accept(generator);
|
||||
String elasticsearchQuery = generator.getSourceResult();
|
||||
assertEquals(expected, elasticsearchQuery);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
group = org.xbib
|
||||
name = cql
|
||||
version = 3.1.1
|
||||
version = 3.1.2
|
||||
|
||||
gradle.wrapper.version = 6.6.1
|
||||
xbib-content.version = 3.0.0
|
||||
|
|
Loading…
Reference in a new issue