Search in sources :

Example 1 with QueryVariant

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;
}
Also used : Arrays(java.util.Arrays) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) Like(co.elastic.clients.elasticsearch._types.query_dsl.Like) ESUtils.getNestedKey(com.erudika.para.server.search.es.ESUtils.getNestedKey) LoggerFactory(org.slf4j.LoggerFactory) TotalHitsRelation(co.elastic.clients.elasticsearch.core.search.TotalHitsRelation) ESUtils.getRESTClient(com.erudika.para.server.search.es.ESUtils.getRESTClient) StringUtils(org.apache.commons.lang3.StringUtils) ESUtils.qs(com.erudika.para.server.search.es.ESUtils.qs) ESUtils.convertQueryStringToNestedQuery(com.erudika.para.server.search.es.ESUtils.convertQueryStringToNestedQuery) PROPS_PREFIX(com.erudika.para.server.search.es.ESUtils.PROPS_PREFIX) Map(java.util.Map) FieldValue(co.elastic.clients.elasticsearch._types.FieldValue) ESUtils.getTermsQuery(com.erudika.para.server.search.es.ESUtils.getTermsQuery) Config(com.erudika.para.core.utils.Config) ParaObject(com.erudika.para.core.ParaObject) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) ESUtils.nestedMode(com.erudika.para.server.search.es.ESUtils.nestedMode) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Tag(com.erudika.para.core.Tag) ESUtils.executeRequests(com.erudika.para.server.search.es.ESUtils.executeRequests) Optional(java.util.Optional) Para(com.erudika.para.core.utils.Para) QueryBuilders(co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders) App(com.erudika.para.core.App) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) SearchType(co.elastic.clients.elasticsearch._types.SearchType) ESUtils.getPager(com.erudika.para.server.search.es.ESUtils.getPager) HashMap(java.util.HashMap) ChildScoreMode(co.elastic.clients.elasticsearch._types.query_dsl.ChildScoreMode) Pager(com.erudika.para.core.utils.Pager) ArrayList(java.util.ArrayList) ESUtils.keyValueBoolQuery(com.erudika.para.server.search.es.ESUtils.keyValueBoolQuery) TotalHits(co.elastic.clients.elasticsearch.core.search.TotalHits) DAO(com.erudika.para.core.persistence.DAO) LinkedList(java.util.LinkedList) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery) ESUtils.nestedPropsQuery(com.erudika.para.server.search.es.ESUtils.nestedPropsQuery) Logger(org.slf4j.Logger) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) Address(com.erudika.para.core.Address) ESUtils.getValueFieldName(com.erudika.para.server.search.es.ESUtils.getValueFieldName) ESUtils.getIndexName(com.erudika.para.server.search.es.ESUtils.getIndexName) Utils(com.erudika.para.core.utils.Utils) TimeUnit(java.util.concurrent.TimeUnit) Hit(co.elastic.clients.elasticsearch.core.search.Hit) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant) SortOptions(co.elastic.clients.elasticsearch._types.SortOptions) Collections(java.util.Collections) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant)

Example 2 with QueryVariant

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;
}
Also used : Arrays(java.util.Arrays) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) Like(co.elastic.clients.elasticsearch._types.query_dsl.Like) ESUtils.getNestedKey(com.erudika.para.server.search.es.ESUtils.getNestedKey) LoggerFactory(org.slf4j.LoggerFactory) TotalHitsRelation(co.elastic.clients.elasticsearch.core.search.TotalHitsRelation) ESUtils.getRESTClient(com.erudika.para.server.search.es.ESUtils.getRESTClient) StringUtils(org.apache.commons.lang3.StringUtils) ESUtils.qs(com.erudika.para.server.search.es.ESUtils.qs) ESUtils.convertQueryStringToNestedQuery(com.erudika.para.server.search.es.ESUtils.convertQueryStringToNestedQuery) PROPS_PREFIX(com.erudika.para.server.search.es.ESUtils.PROPS_PREFIX) Map(java.util.Map) FieldValue(co.elastic.clients.elasticsearch._types.FieldValue) ESUtils.getTermsQuery(com.erudika.para.server.search.es.ESUtils.getTermsQuery) Config(com.erudika.para.core.utils.Config) ParaObject(com.erudika.para.core.ParaObject) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) ESUtils.nestedMode(com.erudika.para.server.search.es.ESUtils.nestedMode) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Tag(com.erudika.para.core.Tag) ESUtils.executeRequests(com.erudika.para.server.search.es.ESUtils.executeRequests) Optional(java.util.Optional) Para(com.erudika.para.core.utils.Para) QueryBuilders(co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders) App(com.erudika.para.core.App) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) SearchType(co.elastic.clients.elasticsearch._types.SearchType) ESUtils.getPager(com.erudika.para.server.search.es.ESUtils.getPager) HashMap(java.util.HashMap) ChildScoreMode(co.elastic.clients.elasticsearch._types.query_dsl.ChildScoreMode) Pager(com.erudika.para.core.utils.Pager) ArrayList(java.util.ArrayList) ESUtils.keyValueBoolQuery(com.erudika.para.server.search.es.ESUtils.keyValueBoolQuery) TotalHits(co.elastic.clients.elasticsearch.core.search.TotalHits) DAO(com.erudika.para.core.persistence.DAO) LinkedList(java.util.LinkedList) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery) ESUtils.nestedPropsQuery(com.erudika.para.server.search.es.ESUtils.nestedPropsQuery) Logger(org.slf4j.Logger) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) Address(com.erudika.para.core.Address) ESUtils.getValueFieldName(com.erudika.para.server.search.es.ESUtils.getValueFieldName) ESUtils.getIndexName(com.erudika.para.server.search.es.ESUtils.getIndexName) Utils(com.erudika.para.core.utils.Utils) TimeUnit(java.util.concurrent.TimeUnit) Hit(co.elastic.clients.elasticsearch.core.search.Hit) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant) SortOptions(co.elastic.clients.elasticsearch._types.SortOptions) Collections(java.util.Collections) Objects(java.util.Objects) FieldValue(co.elastic.clients.elasticsearch._types.FieldValue) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant) LinkedList(java.util.LinkedList)

Example 3 with QueryVariant

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);
}
Also used : Arrays(java.util.Arrays) BulkOperation(co.elastic.clients.elasticsearch.core.bulk.BulkOperation) Like(co.elastic.clients.elasticsearch._types.query_dsl.Like) ESUtils.getNestedKey(com.erudika.para.server.search.es.ESUtils.getNestedKey) LoggerFactory(org.slf4j.LoggerFactory) TotalHitsRelation(co.elastic.clients.elasticsearch.core.search.TotalHitsRelation) ESUtils.getRESTClient(com.erudika.para.server.search.es.ESUtils.getRESTClient) StringUtils(org.apache.commons.lang3.StringUtils) ESUtils.qs(com.erudika.para.server.search.es.ESUtils.qs) ESUtils.convertQueryStringToNestedQuery(com.erudika.para.server.search.es.ESUtils.convertQueryStringToNestedQuery) PROPS_PREFIX(com.erudika.para.server.search.es.ESUtils.PROPS_PREFIX) Map(java.util.Map) FieldValue(co.elastic.clients.elasticsearch._types.FieldValue) ESUtils.getTermsQuery(com.erudika.para.server.search.es.ESUtils.getTermsQuery) Config(com.erudika.para.core.utils.Config) ParaObject(com.erudika.para.core.ParaObject) GetResponse(co.elastic.clients.elasticsearch.core.GetResponse) ESUtils.nestedMode(com.erudika.para.server.search.es.ESUtils.nestedMode) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Tag(com.erudika.para.core.Tag) ESUtils.executeRequests(com.erudika.para.server.search.es.ESUtils.executeRequests) Optional(java.util.Optional) Para(com.erudika.para.core.utils.Para) QueryBuilders(co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders) App(com.erudika.para.core.App) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) SearchResponse(co.elastic.clients.elasticsearch.core.SearchResponse) SearchType(co.elastic.clients.elasticsearch._types.SearchType) ESUtils.getPager(com.erudika.para.server.search.es.ESUtils.getPager) HashMap(java.util.HashMap) ChildScoreMode(co.elastic.clients.elasticsearch._types.query_dsl.ChildScoreMode) Pager(com.erudika.para.core.utils.Pager) ArrayList(java.util.ArrayList) ESUtils.keyValueBoolQuery(com.erudika.para.server.search.es.ESUtils.keyValueBoolQuery) TotalHits(co.elastic.clients.elasticsearch.core.search.TotalHits) DAO(com.erudika.para.core.persistence.DAO) LinkedList(java.util.LinkedList) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery) ESUtils.nestedPropsQuery(com.erudika.para.server.search.es.ESUtils.nestedPropsQuery) Logger(org.slf4j.Logger) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) Address(com.erudika.para.core.Address) ESUtils.getValueFieldName(com.erudika.para.server.search.es.ESUtils.getValueFieldName) ESUtils.getIndexName(com.erudika.para.server.search.es.ESUtils.getIndexName) Utils(com.erudika.para.core.utils.Utils) TimeUnit(java.util.concurrent.TimeUnit) Hit(co.elastic.clients.elasticsearch.core.search.Hit) Query(co.elastic.clients.elasticsearch._types.query_dsl.Query) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant) SortOptions(co.elastic.clients.elasticsearch._types.SortOptions) Collections(java.util.Collections) Address(com.erudika.para.core.Address) ESUtils.getPager(com.erudika.para.server.search.es.ESUtils.getPager) Pager(com.erudika.para.core.utils.Pager) Objects(java.util.Objects) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with QueryVariant

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);
}
Also used : QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant)

Example 5 with QueryVariant

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();
}
Also used : Matcher(java.util.regex.Matcher) Term(org.apache.lucene.index.Term) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant) ParaObject(com.erudika.para.core.ParaObject) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

QueryVariant (co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant)15 BoolQuery (co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery)12 ParaObject (com.erudika.para.core.ParaObject)11 FieldValue (co.elastic.clients.elasticsearch._types.FieldValue)10 SortOptions (co.elastic.clients.elasticsearch._types.SortOptions)10 SortOrder (co.elastic.clients.elasticsearch._types.SortOrder)10 ChildScoreMode (co.elastic.clients.elasticsearch._types.query_dsl.ChildScoreMode)10 QueryBuilders (co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders)10 BulkOperation (co.elastic.clients.elasticsearch.core.bulk.BulkOperation)10 App (com.erudika.para.core.App)10 DAO (com.erudika.para.core.persistence.DAO)10 Config (com.erudika.para.core.utils.Config)10 Pager (com.erudika.para.core.utils.Pager)10 Para (com.erudika.para.core.utils.Para)10 Utils (com.erudika.para.core.utils.Utils)10 ArrayList (java.util.ArrayList)10 SearchType (co.elastic.clients.elasticsearch._types.SearchType)7 Like (co.elastic.clients.elasticsearch._types.query_dsl.Like)7 Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)7 HashMap (java.util.HashMap)7