add keyword fields for exact search via field name

This commit is contained in:
Jörg Prante 2021-11-02 15:08:05 +01:00
parent a52894f663
commit 04d19b3ed7
2 changed files with 120 additions and 89 deletions

View file

@ -142,10 +142,18 @@ public class QueryGenerator implements Visitor {
if (!visible) {
return;
}
String field = arg1.toString();
switch (op) {
case EQUALS: {
String field = arg1.toString();
String value = arg2 != null ? arg2.toString() : ""; // with quote
if (field.endsWith("Keyword")) {
// exact search
builder.beginMap()
.beginMap("term")
.field(field, value)
.endMap()
.endMap();
} else {
// with phrase boost
builder.beginMap("bool")
.beginCollection("should")
@ -167,11 +175,21 @@ public class QueryGenerator implements Visitor {
.endCollection()
.field("minimum_should_match", "1")
.endMap();
}
break;
}
case NOT_EQUALS: {
String field = arg1.toString();
String value = arg2 != null ? arg2.toString() : ""; // with quote
if (field.endsWith("Keyword")) {
// exact search
builder.beginMap("bool")
.beginMap("must_not")
.beginMap("term")
.field(field, value)
.endMap()
.endMap()
.endMap();
} else {
builder.beginMap("bool")
.beginMap("must_not")
.beginMap("simple_query_string")
@ -182,11 +200,17 @@ public class QueryGenerator implements Visitor {
.endMap()
.endMap()
.endMap();
}
break;
}
case ALL: {
String field = arg1.toString();
String value = tok2 != null ? tok2.getString() : ""; // always unquoted
if (field.endsWith("Keyword")) {
// exact search
builder.beginMap("term")
.field(field, value)
.endMap();
} else {
// with phrase boost
builder.beginMap("bool")
.beginCollection("should")
@ -208,11 +232,17 @@ public class QueryGenerator implements Visitor {
.endCollection()
.field("minimum_should_match", "1")
.endMap();
}
break;
}
case ANY: {
String field = arg1.toString();
String value = tok2 != null ? tok2.getString() : ""; // always unquoted
if (field.endsWith("Keyword")) {
// exact search
builder.beginMap("term")
.field(field, value)
.endMap();
} else {
// with phrase boost
builder.beginMap("bool")
.beginCollection("should")
@ -232,11 +262,11 @@ public class QueryGenerator implements Visitor {
.endCollection()
.field("minimum_should_match", "1")
.endMap();
}
break;
}
case PHRASE: {
if (tok2 != null) {
String field = arg1.toString();
String value = tok2.isQuoted() ? tok2.getString() : arg2.toString();
if (tok2.isAll()) {
builder.beginMap("match_all").endMap();
@ -244,19 +274,25 @@ public class QueryGenerator implements Visitor {
builder.beginMap("wildcard").field(field, value).endMap();
} else if (tok2.isBoundary()) {
builder.beginMap("prefix").field(field, value).endMap();
} else {
if (field.endsWith("Keyword")) {
// exact search
builder.beginMap("term")
.field(field, value)
.endMap();
} else {
builder.beginMap("simple_query_string")
.field("query", value)
.field("fields",Collections.singletonList(field))
.field("fields", Collections.singletonList(field))
.field("analyze_wildcard", true)
.field("default_operator", "and")
.endMap();
}
}
}
break;
}
case RANGE_GREATER_THAN: {
String field = arg1.toString();
String value = arg2 != null ? arg2.toString() : "";
builder.beginMap("range").beginMap(field)
.field("from", value)
@ -265,7 +301,6 @@ public class QueryGenerator implements Visitor {
break;
}
case RANGE_GREATER_OR_EQUAL: {
String field = arg1.toString();
String value = arg2 != null ? arg2.toString() : "";
builder.beginMap("range").beginMap(field)
.field("from", value)
@ -274,7 +309,6 @@ public class QueryGenerator implements Visitor {
break;
}
case RANGE_LESS_THAN: {
String field = arg1.toString();
String value = arg2 != null ? arg2.toString() : "";
builder.beginMap("range").beginMap(field)
.field("to", value)
@ -283,7 +317,6 @@ public class QueryGenerator implements Visitor {
break;
}
case RANGE_LESS_OR_EQUALS: {
String field = arg1.toString();
String value = arg2 != null ? arg2.toString() : "";
builder.beginMap("range").beginMap(field)
.field("to", value)
@ -293,7 +326,6 @@ public class QueryGenerator implements Visitor {
}
case RANGE_WITHIN: {
// borders are inclusive
String field = arg1.toString();
String value = arg2 != null ? arg2.toString() : "";
String from = null;
String to = null;
@ -397,7 +429,6 @@ public class QueryGenerator implements Visitor {
break;
}
case PROX: {
String field = arg1.toString();
// we assume a default of 10 words is enough for proximity
String value = arg2 != null ? arg2 + "~10" : "";
builder.beginMap("field").field(field, value).endMap();

View file

@ -1,6 +1,6 @@
group = org.xbib
name = cql
version = 4.0.0
version = 4.0.1
gradle.wrapper.version = 6.6.1
xbib-datastructures.version = 1.0.0