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, Map<String, ?> terms) {
if (StringUtils.isBlank(appid) || terms == null || terms.isEmpty()) {
return 0L;
}
Long count = 0L;
QueryVariant query = getTermsQuery(terms, true);
if (query != null) {
if (!StringUtils.isBlank(type)) {
query = QueryBuilders.bool().must(query._toQuery(), QueryBuilders.term().field(Config._TYPE).value(v -> v.stringValue(type)).build()._toQuery()).build();
}
try {
Query q = query._toQuery();
count = getRESTClient().count(b -> b.index(getIndexName(appid)).query(q)).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 findTermInListInternal.
public static <P extends ParaObject> List<P> findTermInListInternal(String appid, String type, String field, List<?> terms, Pager... pager) {
if (StringUtils.isBlank(field) || terms == null) {
return Collections.emptyList();
}
QueryVariant qb;
if (nestedMode() && field.startsWith(PROPS_PREFIX)) {
QueryVariant bfb = null;
BoolQuery.Builder fb = QueryBuilders.bool();
for (Object term : terms) {
bfb = keyValueBoolQuery(field, String.valueOf(term));
fb.should(bfb._toQuery());
}
qb = (QueryVariant) nestedPropsQuery(terms.size() > 1 ? fb.build() : bfb).build();
} else {
List<FieldValue> values = terms.stream().filter(Objects::nonNull).map(v -> FieldValue.of(fv -> fv.stringValue(v.toString()))).collect(Collectors.toList());
qb = QueryBuilders.terms().field(field).terms(t -> t.value(values)).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 ES method findSimilarInternal.
public static <P extends ParaObject> List<P> findSimilarInternal(String appid, String type, String filterKey, String[] fields, String liketext, Pager... pager) {
if (StringUtils.isBlank(liketext)) {
return Collections.emptyList();
}
QueryVariant qb;
String matchPercent = "70%";
if (fields == null || fields.length == 0) {
qb = QueryBuilders.moreLikeThis().like(l -> l.text(liketext)).minDocFreq(1).minTermFreq(1).minimumShouldMatch(matchPercent).build();
} else {
boolean containsNestedProps = Arrays.stream(fields).anyMatch((f) -> StringUtils.startsWith(f, PROPS_PREFIX));
if (nestedMode() && containsNestedProps) {
BoolQuery.Builder bqb = QueryBuilders.bool();
for (String field : fields) {
QueryVariant kQuery = QueryBuilders.match().field(PROPS_PREFIX + "k").query(v -> v.stringValue(getNestedKey(field))).build();
QueryVariant vQuery = QueryBuilders.moreLikeThis().fields(PROPS_PREFIX + "v").like(Like.of(l -> l.text(liketext))).minDocFreq(1).minTermFreq(1).minimumShouldMatch(matchPercent).build();
QueryVariant nested = (QueryVariant) nestedPropsQuery(QueryBuilders.bool().must(kQuery._toQuery(), vQuery._toQuery()).build()).build();
bqb.should(nested._toQuery());
}
qb = bqb.build();
} else {
qb = QueryBuilders.moreLikeThis().fields(Arrays.asList(fields)).like(l -> l.text(liketext)).minDocFreq(1).minTermFreq(1).minimumShouldMatch(matchPercent).build();
}
}
if (!StringUtils.isBlank(filterKey)) {
qb = QueryBuilders.bool().mustNot(QueryBuilders.term().field(Config._ID).value(v -> v.stringValue(filterKey)).build()._toQuery()).filter(qb._toQuery()).build();
}
return searchQuery(appid, searchQueryRaw(appid, type, qb, pager));
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ES method searchQueryRaw.
/**
* Executes an ElasticSearch query. This is the core method of the class.
* @param appid name of the {@link com.erudika.para.core.App}
* @param type type of object
* @param query the search query builder
* @param pager a {@link com.erudika.para.core.utils.Pager}
* @return a list of search results
*/
protected static SearchResponse<Map> searchQueryRaw(String appid, String type, QueryVariant query, Pager... pager) {
if (StringUtils.isBlank(appid)) {
return null;
}
Pager page = ESUtils.getPager(pager);
SortOrder order = page.isDesc() ? SortOrder.Desc : SortOrder.Asc;
int max = page.getLimit();
int pageNum = (int) page.getPage();
int start = (pageNum < 1 || pageNum > Para.getConfig().maxPages()) ? 0 : (pageNum - 1) * max;
if (query == null) {
query = QueryBuilders.matchAll().build();
}
if (!StringUtils.isBlank(type)) {
query = QueryBuilders.bool().must(query._toQuery(), QueryBuilders.term().field(Config._TYPE).value(v -> v.stringValue(type)).build()._toQuery()).build();
}
SearchResponse<Map> hits = null;
String debugQuery = "";
try {
SearchRequest.Builder search = new SearchRequest.Builder();
search.index(getIndexName(appid)).searchType(SearchType.DfsQueryThenFetch).query(query._toQuery()).size(max).trackTotalHits(ESUtils.getTrackTotalHits());
if (pageNum <= 1 && !StringUtils.isBlank(page.getLastKey())) {
search.searchAfter(page.getLastKey());
search.from(0);
search.sort(SortOptions.of(b -> b.field(f -> f.field("_docid").order(order))));
} else {
search.from(start);
for (SortOptions sortField : ESUtils.getSortFieldsFromPager(page)) {
search.sort(sortField);
}
}
debugQuery = search.toString();
logger.debug("Elasticsearch query: {}", debugQuery);
hits = getRESTClient().search(search.build(), Map.class);
page.setCount(Optional.ofNullable(hits.hits().total()).orElse(TotalHits.of(t -> t.relation(TotalHitsRelation.Eq).value(page.getCount()))).value());
if (hits.hits().hits().size() > 0) {
Object id = hits.hits().hits().get(hits.hits().hits().size() - 1).source().get("_docid");
if (id != null) {
page.setLastKey(id.toString());
}
}
} catch (Exception e) {
Throwable cause = e.getCause();
String msg = cause != null ? cause.getMessage() : e.getMessage();
logger.debug("No search results for type '{}' in app '{}': {}.\nQuery: {}", type, appid, msg, debugQuery);
}
return hits;
}
use of co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant in project para-search-elasticsearch by Erudika.
the class ESUtils method term.
private static ObjectBuilder<?> term(Query q) {
ObjectBuilder<?> qb;
String field = ((org.apache.lucene.search.TermQuery) q).getTerm().field();
String value = ((org.apache.lucene.search.TermQuery) q).getTerm().text();
if (StringUtils.isBlank(field)) {
QueryVariant kQuery = QueryBuilders.matchAll().build();
QueryVariant vQuery = QueryBuilders.multiMatch().query(value).build();
QueryVariant nested = (QueryVariant) nestedPropsQuery(QueryBuilders.bool().must(kQuery._toQuery(), vQuery._toQuery()).build()).build();
qb = QueryBuilders.bool().should(nested._toQuery(), QueryBuilders.multiMatch().query(value).build()._toQuery());
} else if (field.matches(PROPS_REGEX)) {
qb = nestedPropsQuery(keyValueBoolQuery(field, value));
} else {
qb = QueryBuilders.term().field(field).value(v -> v.stringValue(value));
}
return qb;
}
Aggregations