use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ES method getCountInternal.
public static Long getCountInternal(String appid, String type) {
if (StringUtils.isBlank(appid)) {
return 0L;
}
QueryVariant query;
if (!StringUtils.isBlank(type)) {
query = QueryBuilders.bool().must(QueryBuilders.term().field(Config._TYPE).value(v -> v.stringValue(type)).build()._toQuery()).build();
} else {
query = QueryBuilders.matchAll().build();
}
Long count = 0L;
try {
count = getRESTClient().count(b -> b.index(getIndexName(appid)).query(query._toQuery())).count();
} catch (Exception e) {
Throwable cause = e.getCause();
String msg = cause != null ? cause.getMessage() : e.getMessage();
logger.warn("Could not count results in index '{}': {}", appid, msg);
}
return count;
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ES method findByIdsInternal.
@SuppressWarnings("unchecked")
public static <P extends ParaObject> List<P> findByIdsInternal(String appid, List<String> ids) {
List<P> list = new LinkedList<P>();
if (ids == null || ids.isEmpty()) {
return list;
}
try {
List<FieldValue> values = ids.stream().filter(Objects::nonNull).map(v -> FieldValue.of(fv -> fv.stringValue(v))).collect(Collectors.toList());
QueryVariant qb = QueryBuilders.terms().field(Config._ID).terms(t -> t.value(values)).build();
return searchQuery(appid, null, qb);
} catch (Exception e) {
logger.warn(null, e);
}
return list;
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ES method findNearbyInternal.
public static <P extends ParaObject> List<P> findNearbyInternal(String appid, String type, String query, int radius, double lat, double lng, Pager... pager) {
if (StringUtils.isBlank(type) || StringUtils.isBlank(appid)) {
return Collections.emptyList();
}
if (StringUtils.isBlank(query)) {
query = "*";
}
// find nearby Address objects
Pager page = getPager(pager);
QueryVariant qb1 = QueryBuilders.geoDistance().field("latlng").location(l -> l.latlon(ll -> ll.lat(lat).lon(lng))).distance(Integer.toString(radius) + " km").build();
SearchResponse<Map> hits1 = searchQueryRaw(appid, Utils.type(Address.class), qb1, page);
// will cause problems if not cleared
page.setLastKey(null);
if (hits1 == null) {
return Collections.emptyList();
}
if (type.equals(Utils.type(Address.class))) {
return searchQuery(appid, hits1);
}
// then find their parent objects
List<String> parentIds = hits1.hits().hits().stream().filter(Objects::nonNull).map(h -> (String) h.source().get(Config._PARENTID)).collect(Collectors.toList());
QueryVariant qb2 = QueryBuilders.bool().must(QueryBuilders.queryString().query(qs(query)).build()._toQuery()).filter(QueryBuilders.ids().values(parentIds).build()._toQuery()).build();
SearchResponse<Map> hits2 = searchQueryRaw(appid, type, qb2, page);
return searchQuery(appid, hits2);
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ES method findNestedQueryInternal.
public static <P extends ParaObject> List<P> findNestedQueryInternal(String appid, String type, String field, String query, Pager... pager) {
if (StringUtils.isBlank(query) || StringUtils.isBlank(field)) {
return Collections.emptyList();
}
String queryString = "nstd." + field + ":" + query;
QueryVariant qb = QueryBuilders.nested().path("nstd").query(QueryBuilders.queryString().query(qs(queryString)).build()._toQuery()).scoreMode(ChildScoreMode.Avg).build();
return searchQuery(appid, type, qb, pager);
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ESUtils method getTermsQuery.
/**
* Creates a term filter for a set of terms.
* @param terms some terms
* @param mustMatchAll if true all terms must match ('AND' operation)
* @return the filter
*/
static QueryVariant getTermsQuery(Map<String, ?> terms, boolean mustMatchAll) {
BoolQuery.Builder fb = QueryBuilders.bool();
int addedTerms = 0;
boolean noop = true;
QueryVariant bfb = null;
for (Map.Entry<String, ?> term : terms.entrySet()) {
Object val = term.getValue();
if (!StringUtils.isBlank(term.getKey()) && val != null && Utils.isBasicType(val.getClass())) {
String stringValue = val.toString();
if (StringUtils.isBlank(stringValue)) {
continue;
}
Matcher matcher = Pattern.compile(".*(<|>|<=|>=)$").matcher(term.getKey().trim());
if (matcher.matches()) {
bfb = range(matcher.group(1), term.getKey(), stringValue);
} else {
if (nestedMode()) {
bfb = (QueryVariant) term(new org.apache.lucene.search.TermQuery(new Term(term.getKey(), stringValue))).build();
} else {
bfb = QueryBuilders.term().field(term.getKey()).value(v -> v.stringValue(stringValue)).build();
}
}
if (mustMatchAll) {
fb.must(bfb._toQuery());
} else {
fb.should(bfb._toQuery());
}
addedTerms++;
noop = false;
}
}
if (addedTerms == 1 && bfb != null) {
return bfb;
}
return noop ? null : fb.build();
}
Aggregations