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,101 +142,131 @@ 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
// with phrase boost if (field.endsWith("Keyword")) {
builder.beginMap("bool") // exact search
.beginCollection("should") builder.beginMap()
.beginMap() .beginMap("term")
.beginMap("simple_query_string") .field(field, value)
.field("query", value) .endMap()
.field("fields", Collections.singletonList(field)) .endMap();
.field("analyze_wildcard", true) } else {
.field("default_operator", "and") // with phrase boost
.endMap() builder.beginMap("bool")
.endMap() .beginCollection("should")
.beginMap() .beginMap()
.beginMap("simple_query_string") .beginMap("simple_query_string")
.field("query", "\"" + value + "\"") .field("query", value)
.field("fields", Collections.singletonList(field + "^2")) .field("fields", Collections.singletonList(field))
.field("default_operator", "and") .field("analyze_wildcard", true)
.endMap() .field("default_operator", "and")
.endMap() .endMap()
.endCollection() .endMap()
.field("minimum_should_match", "1") .beginMap()
.endMap(); .beginMap("simple_query_string")
.field("query", "\"" + value + "\"")
.field("fields", Collections.singletonList(field + "^2"))
.field("default_operator", "and")
.endMap()
.endMap()
.endCollection()
.field("minimum_should_match", "1")
.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
builder.beginMap("bool") if (field.endsWith("Keyword")) {
.beginMap("must_not") // exact search
.beginMap("simple_query_string") builder.beginMap("bool")
.field("query", value) .beginMap("must_not")
.field("fields", Collections.singletonList(field)) .beginMap("term")
.field("analyze_wildcard", true) .field(field, value)
.field("default_operator", "and") .endMap()
.endMap() .endMap()
.endMap() .endMap();
.endMap(); } else {
builder.beginMap("bool")
.beginMap("must_not")
.beginMap("simple_query_string")
.field("query", value)
.field("fields", Collections.singletonList(field))
.field("analyze_wildcard", true)
.field("default_operator", "and")
.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
// with phrase boost if (field.endsWith("Keyword")) {
builder.beginMap("bool") // exact search
.beginCollection("should") builder.beginMap("term")
.beginMap() .field(field, value)
.beginMap("simple_query_string") .endMap();
.field("query", value) } else {
.field("fields", Collections.singletonList(field)) // with phrase boost
.field("analyze_wildcard", true) builder.beginMap("bool")
.field("default_operator", "and") .beginCollection("should")
.endMap() .beginMap()
.endMap() .beginMap("simple_query_string")
.beginMap() .field("query", value)
.beginMap("simple_query_string") .field("fields", Collections.singletonList(field))
.field("query", "\"" + value + "\"") .field("analyze_wildcard", true)
.field("fields", Collections.singletonList(field + "^2")) .field("default_operator", "and")
.field("default_operator", "and") .endMap()
.endMap() .endMap()
.endMap() .beginMap()
.endCollection() .beginMap("simple_query_string")
.field("minimum_should_match", "1") .field("query", "\"" + value + "\"")
.endMap(); .field("fields", Collections.singletonList(field + "^2"))
.field("default_operator", "and")
.endMap()
.endMap()
.endCollection()
.field("minimum_should_match", "1")
.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
// with phrase boost if (field.endsWith("Keyword")) {
builder.beginMap("bool") // exact search
.beginCollection("should") builder.beginMap("term")
.beginMap() .field(field, value)
.beginMap("simple_query_string") .endMap();
.field("query", value) } else {
.field("fields", Collections.singletonList(field)) // with phrase boost
.field("analyze_wildcard", true) builder.beginMap("bool")
.endMap() .beginCollection("should")
.endMap() .beginMap()
.beginMap() .beginMap("simple_query_string")
.beginMap("simple_query_string") .field("query", value)
.field("query", "\"" + value + "\"") .field("fields", Collections.singletonList(field))
.field("fields", Collections.singletonList(field + "^2")) .field("analyze_wildcard", true)
.endMap() .endMap()
.endMap() .endMap()
.endCollection() .beginMap()
.field("minimum_should_match", "1") .beginMap("simple_query_string")
.endMap(); .field("query", "\"" + value + "\"")
.field("fields", Collections.singletonList(field + "^2"))
.endMap()
.endMap()
.endCollection()
.field("minimum_should_match", "1")
.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();
@ -245,18 +275,24 @@ public class QueryGenerator implements Visitor {
} else if (tok2.isBoundary()) { } else if (tok2.isBoundary()) {
builder.beginMap("prefix").field(field, value).endMap(); builder.beginMap("prefix").field(field, value).endMap();
} else { } else {
builder.beginMap("simple_query_string") if (field.endsWith("Keyword")) {
.field("query", value) // exact search
.field("fields",Collections.singletonList(field)) builder.beginMap("term")
.field("analyze_wildcard", true) .field(field, value)
.field("default_operator", "and") .endMap();
.endMap(); } else {
builder.beginMap("simple_query_string")
.field("query", value)
.field("fields", Collections.singletonList(field))
.field("analyze_wildcard", true)
.field("default_operator", "and")
.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