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

View file

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

View file

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