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

View file

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