replace match_phrase with simple query

This commit is contained in:
Jörg Prante 2021-09-16 18:08:58 +02:00
parent 67aa372666
commit 5abd384404
3 changed files with 21 additions and 21 deletions

View file

@ -69,7 +69,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
private XContentBuilder sort;
private String globalField;
private final String globalField;
public ElasticsearchQueryGenerator(String globalField) throws IOException {
this.globalField = globalField;
@ -170,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(globalField), querynode);
new Expression(Operator.ALL, new Name(globalField), querynode);
}
queryGen.visit((Expression) querynode);
if (model.hasFilter() && model.getFilterExpression() != null) {
@ -279,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(globalField), esnode);
esnode = new Expression(Operator.ALL, new Name(globalField), esnode);
}
if (stack.isEmpty()) {
// unary expression
@ -289,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(globalField), esnode2);
esnode2 = new Expression(Operator.ALL, new Name(globalField), esnode2);
}
esnode = new Expression(op, esnode2, esnode);
}

View file

@ -122,11 +122,8 @@ public class QueryGenerator implements Visitor {
Operator op = node.getOperator();
switch (op.getArity()) {
case 0: {
switch (op) {
case MATCH_ALL: {
if (op == Operator.MATCH_ALL) {
builder.startObject("match_all").endObject();
break;
}
}
break;
}
@ -149,7 +146,7 @@ public class QueryGenerator implements Visitor {
switch (op) {
case EQUALS: {
String field = arg1.toString();
String value = arg2 != null ? arg2.toString() : "";
String value = arg2 != null ? arg2.toString() : ""; // with quote
builder.startObject("simple_query_string")
.field("query", value)
.field("fields", new String[]{field})
@ -160,20 +157,22 @@ public class QueryGenerator implements Visitor {
}
case NOT_EQUALS: {
String field = arg1.toString();
String value = arg2 != null ? arg2.toString() : "";
builder.startObject("bool").startObject("must_not");
builder.startObject("simple_query_string")
String value = arg2 != null ? arg2.toString() : ""; // with quote
builder.startObject("bool")
.startObject("must_not")
.startObject("simple_query_string")
.field("query", value)
.field("fields", new String[]{field})
.field("analyze_wildcard", true)
.field("default_operator", "and")
.endObject()
.endObject()
.endObject();
builder.endObject().endObject();
break;
}
case ALL: {
String field = arg1.toString();
String value = tok2 != null ? tok2.getString() : "";
String value = tok2 != null ? tok2.getString() : ""; // always unquoted
builder.startObject("simple_query_string")
.field("query", value)
.field("fields", new String[]{field})
@ -184,7 +183,7 @@ public class QueryGenerator implements Visitor {
}
case ANY: {
String field = arg1.toString();
String value = tok2 != null ? tok2.getString() : "";
String value = tok2 != null ? tok2.getString() : ""; // always unquoted
builder.startObject("simple_query_string")
.field("query", value)
.field("fields", new String[]{field})
@ -204,11 +203,11 @@ public class QueryGenerator implements Visitor {
} else if (tok2.isBoundary()) {
builder.startObject("prefix").field(field, value).endObject();
} else {
builder.startObject("match_phrase")
.startObject(field)
builder.startObject("simple_query_string")
.field("query", value)
.field("slop", 0)
.endObject()
.field("fields", new String[]{field})
.field("analyze_wildcard", true)
.field("default_operator", "and")
.endObject();
}
}
@ -373,4 +372,5 @@ public class QueryGenerator implements Visitor {
throw new SyntaxException("internal error while building elasticsearch query", e);
}
}
}

View file

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