use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ESUtils method deleteByQuery.
/**
* Executes a delete_by_query request to ES and refreshes the index.
* @param appid the appid / index alias
* @param fb query
* @param cb callback
* @return number of unindexed documents.
*/
public static long deleteByQuery(String appid, QueryVariant fb, Consumer<DeleteByQueryResponse> cb) {
int batchSize = Para.getConfig().getConfigInt("unindex_batch_size", 1000);
boolean isSharingIndex = !App.isRoot(appid) && StringUtils.startsWith(appid, " ");
String indexName = getIndexName(appid);
DeleteByQueryRequest.Builder deleteByQueryReq = new DeleteByQueryRequest.Builder();
deleteByQueryReq.index(indexName);
deleteByQueryReq.conflicts(Conflicts.Proceed);
deleteByQueryReq.query(fb._toQuery());
// deleteByQueryReq.BatchSize(batchSize);
// parallelize operation?
deleteByQueryReq.slices(1L);
deleteByQueryReq.scroll(Time.of(t -> t.time("10m")));
deleteByQueryReq.refresh(true);
if (isSharingIndex) {
deleteByQueryReq.routing(indexName);
}
if (cb != null) {
// getRESTClient().deleteByQueryAsync(deleteByQueryReq, RequestOptions.DEFAULT, cb);
getAsyncRESTClient().deleteByQuery(deleteByQueryReq.build()).thenAccept(cb);
} else {
DeleteByQueryResponse res;
try {
res = getRESTClient().deleteByQuery(deleteByQueryReq.build());
if (!res.failures().isEmpty()) {
logger.warn("Failures in deleteByQuery() - {}", res.failures().iterator().next().cause().reason());
}
return res.total();
} catch (IOException ex) {
logger.error(null, ex);
}
}
return 0L;
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ESUtils method fuzzy.
private static ObjectBuilder<?> fuzzy(Query q) {
ObjectBuilder<?> qb;
String field = ((FuzzyQuery) q).getTerm().field();
String value = ((FuzzyQuery) q).getTerm().text();
if (StringUtils.isBlank(field)) {
QueryVariant kQuery = QueryBuilders.matchAll().build();
QueryVariant vQuery = QueryBuilders.fuzzy().field(getValueFieldName(value)).value(v -> v.stringValue(value)).build();
QueryVariant nested = (QueryVariant) nestedPropsQuery(QueryBuilders.bool().must(kQuery._toQuery(), vQuery._toQuery()).build());
qb = QueryBuilders.bool().should(nested._toQuery(), QueryBuilders.multiMatch().query(value).build()._toQuery());
} else if (field.matches(PROPS_REGEX)) {
qb = nestedPropsQuery(keyValueBoolQuery(field, QueryBuilders.fuzzy().field(getValueFieldName(value)).value(v -> v.stringValue(value)).build()));
} else {
qb = QueryBuilders.fuzzy().field(field).value(v -> v.stringValue(value));
}
return qb;
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ESUtils method wildcard.
private static ObjectBuilder<?> wildcard(Query q) {
ObjectBuilder<?> qb;
String field = ((WildcardQuery) q).getTerm().field();
String value = ((WildcardQuery) q).getTerm().text();
if (StringUtils.isBlank(field)) {
QueryVariant kQuery = QueryBuilders.matchAll().build();
QueryVariant vQuery = QueryBuilders.wildcard().field(getValueFieldName(value)).value(value).build();
QueryVariant nested = (QueryVariant) nestedPropsQuery(QueryBuilders.bool().must(kQuery._toQuery(), vQuery._toQuery()).build());
qb = QueryBuilders.bool().should(nested._toQuery(), QueryBuilders.multiMatch().query(value).build()._toQuery());
} else if (field.matches(PROPS_REGEX)) {
qb = nestedPropsQuery(keyValueBoolQuery(field, QueryBuilders.wildcard().field(getValueFieldName(value)).value(value).build()));
} else {
qb = QueryBuilders.wildcard().field(field).value(value);
}
return qb;
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ESUtils method keyValueBoolQuery.
/**
* @param k field name
* @param v field value
* @param query query object
* @return a composite query: bool(match(key) AND match(value))
*/
static QueryVariant keyValueBoolQuery(String k, String v, QueryVariant query) {
if (StringUtils.isBlank(k) || (query == null && StringUtils.isBlank(v))) {
return QueryBuilders.matchAll().build();
}
QueryVariant kQuery = QueryBuilders.match().field(PROPS_PREFIX + "k").query(b -> b.stringValue(getNestedKey(k))).operator(Operator.And).build();
QueryVariant vQuery = (query == null) ? QueryBuilders.match().field(getValueFieldName(v)).query(b -> b.stringValue(v)).operator(Operator.And).build() : query;
if ("*".equals(v) || query instanceof MatchAllQuery) {
return QueryBuilders.bool().must(kQuery._toQuery()).build();
}
return QueryBuilders.bool().must(kQuery._toQuery(), vQuery._toQuery()).build();
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ESUtils method prefix.
private static ObjectBuilder<?> prefix(Query q) {
ObjectBuilder<?> qb;
String field = ((PrefixQuery) q).getPrefix().field();
String value = ((PrefixQuery) q).getPrefix().text();
if (StringUtils.isBlank(field)) {
QueryVariant kQuery = QueryBuilders.matchAll().build();
QueryVariant vQuery = QueryBuilders.prefix().field(getValueFieldName(value)).value(value).build();
QueryVariant nested = (QueryVariant) nestedPropsQuery(QueryBuilders.bool().must(kQuery._toQuery(), vQuery._toQuery()).build());
qb = QueryBuilders.bool().should(nested._toQuery(), QueryBuilders.multiMatch().query(value).build()._toQuery());
} else if (field.matches(PROPS_REGEX)) {
qb = nestedPropsQuery(keyValueBoolQuery(field, QueryBuilders.prefix().field(getValueFieldName(value)).value(value).build()));
} else {
qb = QueryBuilders.prefix().field(field).value(value);
}
return qb;
}
Aggregations