open ElasticsaerchGenerator for custom sub-generators, replace unmapped_type by ignore_unmapped

This commit is contained in:
Jörg Prante 2021-12-02 18:05:27 +01:00
parent 23f45e6eea
commit 9b833ee888
5 changed files with 58 additions and 47 deletions

View file

@ -42,7 +42,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
private final ElasticsearchQueryModel model;
private final ElasticsearchFilterGenerator filterGenerator;
private final ElasticsearchFilterGenerator elasticsearchFilterGenerator;
private final Stack<Node> stack;
@ -62,30 +62,33 @@ public class ElasticsearchQueryGenerator implements Visitor {
private final QueryGenerator queryGen;
private FilterGenerator filterGen;
private final FacetsGenerator facetGen;
private FacetsGenerator facetGen;
private SortGenerator sortGen;
private final SortGenerator sortGen;
private final String globalField;
private FilterGenerator filterGen;
public ElasticsearchQueryGenerator(String globalField) throws IOException {
this(globalField, new SourceGenerator(), new QueryGenerator(), new FacetsGenerator(), new SortGenerator());
}
public ElasticsearchQueryGenerator(String globalField,
SourceGenerator sourceGen,
QueryGenerator queryGen,
FacetsGenerator facetGen,
SortGenerator sortGen) throws IOException {
this.globalField = globalField;
this.from = 0;
this.size = 10;
this.model = new ElasticsearchQueryModel();
this.filterGenerator = new ElasticsearchFilterGenerator(globalField, model);
this.elasticsearchFilterGenerator = new ElasticsearchFilterGenerator(globalField, model);
this.stack = new Stack<>();
this.sourceGen = new SourceGenerator();
this.queryGen = new QueryGenerator();
this.filterGen = new FilterGenerator();
this.facetGen = new FacetsGenerator();
this.sortGen = new SortGenerator();
}
public ElasticsearchQueryModel getModel() {
return model;
this.sourceGen = sourceGen != null ? sourceGen : new SourceGenerator();
this.queryGen = queryGen != null ? queryGen : new QueryGenerator();
this.facetGen = facetGen != null ? facetGen : new FacetsGenerator();
this.sortGen = sortGen != null ? sortGen : new SortGenerator();
}
public ElasticsearchQueryGenerator setFrom(int from) {
@ -109,17 +112,17 @@ public class ElasticsearchQueryGenerator implements Visitor {
public ElasticsearchQueryGenerator filter(String filter) {
CQLParser parser = new CQLParser(filter);
parser.parse();
parser.getCQLQuery().accept(filterGenerator);
parser.getCQLQuery().accept(elasticsearchFilterGenerator);
return this;
}
public ElasticsearchQueryGenerator andfilter(String filterKey, Collection<String> filterValues) {
filterGenerator.addAndFilter(filterKey, filterValues);
elasticsearchFilterGenerator.addAndFilter(filterKey, filterValues);
return this;
}
public ElasticsearchQueryGenerator orfilter(String filterKey, Collection<String> filterValues) {
filterGenerator.addOrFilter(filterKey, filterValues);
elasticsearchFilterGenerator.addOrFilter(filterKey, filterValues);
return this;
}
@ -132,14 +135,27 @@ public class ElasticsearchQueryGenerator implements Visitor {
return this;
}
public ElasticsearchQueryModel getModel() {
return model;
}
public String getQueryResult() {
return queryGen.getResult().build();
}
public String getFilterResult() {
return filterGen != null ? filterGen.getResult().build() : null;
}
public String getFacetResult() {
return facetGen.getResult().build();
}
public String getSortRequest() {
return sortGen.getResult().build();
}
public String getSourceResult() {
return sourceGen.getResult().build();
}
@ -157,7 +173,7 @@ public class ElasticsearchQueryGenerator implements Visitor {
}
if (model.hasFilter()) {
queryGen.startFiltered();
} else if (filterGenerator.getResult().build().length() > 0) {
} else if (elasticsearchFilterGenerator.getResult().build().length() > 0) {
queryGen.startFiltered();
}
Node querynode = stack.pop();
@ -175,9 +191,9 @@ public class ElasticsearchQueryGenerator implements Visitor {
filterGen.visit(model.getFilterExpression());
filterGen.endFilter();
queryGen.end();
} else if (filterGenerator.getResult().build().length() > 0) {
} else if (elasticsearchFilterGenerator.getResult().build().length() > 0) {
queryGen.end();
JsonBuilder contentBuilder = filterGenerator.getResult();
JsonBuilder contentBuilder = elasticsearchFilterGenerator.getResult();
queryGen.getResult(). copy(contentBuilder);
queryGen.endFiltered();
}
@ -185,12 +201,11 @@ public class ElasticsearchQueryGenerator implements Visitor {
queryGen.endBoost();
}
if (model.hasFacets()) {
facetGen = new FacetsGenerator();
facetGen.visit(model.getFacetExpression());
}
queryGen.end();
if (model.getSort() != null) {
sortGen = new SortGenerator();
//sortGen = new SortGenerator();
sortGen.start();
sortGen.visit(model.getSort());
sortGen.end();

View file

@ -17,8 +17,6 @@ import java.util.Map;
*/
public class FacetsGenerator implements Visitor {
private int facetlength = 10;
private final JsonBuilder builder;
public FacetsGenerator() throws IOException {
@ -93,19 +91,18 @@ public class FacetsGenerator implements Visitor {
break;
}
default:
throw new IllegalArgumentException(
"unable to translate operator while building elasticsearch facet: " + op);
throw new IllegalArgumentException("unable to translate operator while building elasticsearch facet: " + op);
}
} catch (IOException e) {
throw new SyntaxException("internal error while building elasticsearch query", e);
}
}
public FacetsGenerator facet(String facetLimit, String facetSort) throws IOException {
public void facet(String facetLimit, String facetSort) throws IOException {
if (facetLimit == null) {
return this;
return;
}
Map<String, Integer> facetMap = parseFacet(facetLimit);
Map<String, Integer> facetMap = parseFacet(facetLimit, 10);
String[] sortSpec = facetSort != null ? facetSort.split(",") : new String[]{"recordCount", "descending"};
String order = "_count";
String dir = "desc";
@ -143,12 +140,11 @@ public class FacetsGenerator implements Visitor {
builder.endMap();
}
builder.endMap();
return this;
}
private Map<String, Integer> parseFacet(String spec) {
Map<String, Integer> m = new HashMap<String, Integer>();
m.put("*", facetlength);
private Map<String, Integer> parseFacet(String spec, int defaultFacetLimit) {
Map<String, Integer> m = new HashMap<>();
m.put("*", defaultFacetLimit);
if (spec == null || spec.length() == 0) {
return m;
}
@ -156,10 +152,10 @@ public class FacetsGenerator implements Visitor {
for (String param : params) {
int pos = param.indexOf(':');
if (pos > 0) {
int n = parseInt(param.substring(0, pos), facetlength);
int n = parseInt(param.substring(0, pos), defaultFacetLimit);
m.put(param.substring(pos + 1), n);
} else if (param.length() > 0) {
int n = parseInt(param, facetlength);
int n = parseInt(param, defaultFacetLimit);
m.put("*", n);
}
}

View file

@ -49,7 +49,7 @@ public class SortGenerator implements Visitor {
builder.beginMap()
.buildKey(node.getName())
.beginMap()
.field("unmapped_type", "string")
.field("ignore_unmapped", "true")
.field("missing", "_last")
.endMap()
.endMap();
@ -75,7 +75,7 @@ public class SortGenerator implements Visitor {
}
}
}
builder.field("unmapped_type", "string");
builder.field("ignore_unmapped", "true");
builder.field("missing", "_last");
builder.endMap();
builder.endMap();

View file

@ -69,7 +69,7 @@ identifier = 0783923126590|{"from":0,"size":10,"query":{"bool":{"should":[{"simp
identifier = "9783923126590"|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"\"9783923126590\"","fields":["identifier"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"\"9783923126590\"\"","fields":["identifier^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}
identifier = "9783923126590*"|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"\"9783923126590*\"","fields":["identifier"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"\"9783923126590*\"\"","fields":["identifier^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}
dc.identifier =/bib.identifierAuthority=isbn "0201563177"|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"\"0201563177\"","fields":["bib.identifierAuthority=isbn"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"\"0201563177\"\"","fields":["bib.identifierAuthority=isbn^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}
dc.identifier =/bib.identifierAuthority=isbn "0201563177" and dc.title=unix sortby dc.date|{"from":0,"size":10,"query":{"bool":{"must":[{"bool":{"should":[{"simple_query_string":{"query":"\"0201563177\"","fields":["bib.identifierAuthority=isbn"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"\"0201563177\"\"","fields":["bib.identifierAuthority=isbn^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["dc.title"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["dc.title^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}]}},"sort":[{"dc.date":{"unmapped_type":"string","missing":"_last"}}]}
dc.identifier =/bib.identifierAuthority=isbn "0201563177" and dc.title=unix sortby dc.date|{"from":0,"size":10,"query":{"bool":{"must":[{"bool":{"should":[{"simple_query_string":{"query":"\"0201563177\"","fields":["bib.identifierAuthority=isbn"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"\"0201563177\"\"","fields":["bib.identifierAuthority=isbn^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["dc.title"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["dc.title^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}]}},"sort":[{"dc.date":{"ignore_unmapped":"true","missing":"_last"}}]}
dc.date > 2007-09-30 and dc.date < "2007-10-30T12:34:56"|{"from":0,"size":10,"query":{"bool":{"must":[{"range":{"dc.date":{"from":"2007-09-30","include_lower":false}}},{"range":{"dc.date":{"to":"\"2007-10-30T12:34:56\"","include_upper":false}}}]}}}
date > 2007-01-01|{"from":0,"size":10,"query":{"range":{"date":{"from":"2007-01-01","include_lower":false}}}}
dc.date <= 2006-07-01|{"from":0,"size":10,"query":{"range":{"dc.date":{"to":"2006-07-01","include_upper":true}}}}
@ -106,15 +106,15 @@ pädagogik AND filter.taxonomy="0/24/313/21/*"|{"from":0,"size":10,"query":{"fil
linux and filter.creator <> "Wolfinger"|{"from":0,"size":10,"query":{"filtered":{"query":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"linux","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"linux\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}},"filter":{"not":{"term":{"creator":"Wolfinger"}}}}}}
unix and option.offset = 10 and option.length = 20|{"from":0,"size":10,"query":{"bool":{"must":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}}}}}
test and option.length = 1 and option.length = 2 and option.length = 3|{"from":0,"size":10,"query":{"bool":{"must":{"bool":{"must":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"test","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"test\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}}}}}}}
unix sortby date|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"date":{"unmapped_type":"string","missing":"_last"}}]}
unix sortby date/sort.descending|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"date":{"order":"desc","unmapped_type":"string","missing":"_last"}}]}
unix sortby date/sort.descending geo/sort.ascending|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"date":{"order":"desc","unmapped_type":"string","missing":"_last"}}]}
unix sortby geo/sort.ascending/sort.unit=km/sort.lat=50.9415016174/sort.lon=6.95853996277|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"geo":{"order":"asc","sort.unit":"km","sort.lat":"50.9415016174","sort.lon":"6.95853996277","unmapped_type":"string","missing":"_last"}}]}
unix sortby geo/sort.ascending/sort.unit=km/sort.center="(50.9415016174,6.95853996277)"|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"geo":{"order":"asc","sort.unit":"km","sort.center":"\"(50.9415016174,6.95853996277)\"","unmapped_type":"string","missing":"_last"}}]}
unix sortby date|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"date":{"ignore_unmapped":"true","missing":"_last"}}]}
unix sortby date/sort.descending|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"date":{"order":"desc","ignore_unmapped":"true","missing":"_last"}}]}
unix sortby date/sort.descending geo/sort.ascending|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"date":{"order":"desc","ignore_unmapped":"true","missing":"_last"}}]}
unix sortby geo/sort.ascending/sort.unit=km/sort.lat=50.9415016174/sort.lon=6.95853996277|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"geo":{"order":"asc","sort.unit":"km","sort.lat":"50.9415016174","sort.lon":"6.95853996277","ignore_unmapped":"true","missing":"_last"}}]}
unix sortby geo/sort.ascending/sort.unit=km/sort.center="(50.9415016174,6.95853996277)"|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}},"sort":[{"geo":{"order":"asc","sort.unit":"km","sort.center":"\"(50.9415016174,6.95853996277)\"","ignore_unmapped":"true","missing":"_last"}}]}
bib.namePersonal = meier|{"from":0,"size":10,"query":{"bool":{"should":[{"simple_query_string":{"query":"meier","fields":["bib.namePersonal"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"meier\"","fields":["bib.namePersonal^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}
unix and filter.location any "DE-929 DE-107 DE-Zw1"|{"from":0,"size":10,"query":{"filtered":{"query":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}},"filter":{"or":[{"term":{"location":"DE-929 DE-107 DE-Zw1"}}]}}}}
unix and filter.location any "DE-929 DE-107 DE-Zw1" sortby date/sort.descending|{"from":0,"size":10,"query":{"filtered":{"query":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}},"filter":{"or":[{"term":{"location":"DE-929 DE-107 DE-Zw1"}}]}}},"sort":[{"date":{"order":"desc","unmapped_type":"string","missing":"_last"}}]}
unix and option.offset = 10 and option.length = 20 and filter.location any "DE-929 DE-107 DE-Zw1" sortby date/sort.descending|{"from":0,"size":10,"query":{"filtered":{"query":{"bool":{"must":{"bool":{"must":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}}}}}},"filter":{"or":[{"term":{"location":"DE-929 DE-107 DE-Zw1"}}]}}},"sort":[{"date":{"order":"desc","unmapped_type":"string","missing":"_last"}}]}
unix and filter.location any "DE-929 DE-107 DE-Zw1" sortby date/sort.descending|{"from":0,"size":10,"query":{"filtered":{"query":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}},"filter":{"or":[{"term":{"location":"DE-929 DE-107 DE-Zw1"}}]}}},"sort":[{"date":{"order":"desc","ignore_unmapped":"true","missing":"_last"}}]}
unix and option.offset = 10 and option.length = 20 and filter.location any "DE-929 DE-107 DE-Zw1" sortby date/sort.descending|{"from":0,"size":10,"query":{"filtered":{"query":{"bool":{"must":{"bool":{"must":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}}}}}},"filter":{"or":[{"term":{"location":"DE-929 DE-107 DE-Zw1"}}]}}},"sort":[{"date":{"order":"desc","ignore_unmapped":"true","missing":"_last"}}]}
unix and facet.creator = "on"|{"from":0,"size":10,"query":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}},"aggregations":{"myfacet":"myvalue"}}
unix and facet.creator = "off"|{"from":0,"size":10,"query":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}},"aggregations":{"myfacet":"myvalue"}}
unix and facet.creator = "on" and facet.subject = "on" and facet.date = "off"|{"from":0,"size":10,"query":{"bool":{"must":{"bool":{"must":{"bool":{"must":{"bool":{"should":[{"simple_query_string":{"query":"unix","fields":["cql.allIndexes"],"analyze_wildcard":true,"default_operator":"and"}},{"simple_query_string":{"query":"\"unix\"","fields":["cql.allIndexes^2"],"default_operator":"and"}}],"minimum_should_match":"1"}}}}}}}},"aggregations":{"myfacet":"myvalue"}}

View file

@ -1,6 +1,6 @@
group = org.xbib
name = cql
version = 4.1.0
version = 4.1.1
org.gradle.warning.mode = ALL
gradle.wrapper.version = 7.3