Search in sources :

Example 1 with PROPS_PREFIX

use of com.erudika.para.server.search.es.ESUtils.PROPS_PREFIX 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);
}
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) ParaObject(com.erudika.para.core.ParaObject) FieldValue(co.elastic.clients.elasticsearch._types.FieldValue) QueryVariant(co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant) ESUtils.keyValueBoolQuery(com.erudika.para.server.search.es.ESUtils.keyValueBoolQuery) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery)

Example 2 with PROPS_PREFIX

use of com.erudika.para.server.search.es.ESUtils.PROPS_PREFIX 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));
}
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) ESUtils.keyValueBoolQuery(com.erudika.para.server.search.es.ESUtils.keyValueBoolQuery) BoolQuery(co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery)

Aggregations

FieldValue (co.elastic.clients.elasticsearch._types.FieldValue)2 SearchType (co.elastic.clients.elasticsearch._types.SearchType)2 SortOptions (co.elastic.clients.elasticsearch._types.SortOptions)2 SortOrder (co.elastic.clients.elasticsearch._types.SortOrder)2 BoolQuery (co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery)2 ChildScoreMode (co.elastic.clients.elasticsearch._types.query_dsl.ChildScoreMode)2 Like (co.elastic.clients.elasticsearch._types.query_dsl.Like)2 Query (co.elastic.clients.elasticsearch._types.query_dsl.Query)2 QueryBuilders (co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders)2 QueryVariant (co.elastic.clients.elasticsearch._types.query_dsl.QueryVariant)2 GetResponse (co.elastic.clients.elasticsearch.core.GetResponse)2 SearchRequest (co.elastic.clients.elasticsearch.core.SearchRequest)2 SearchResponse (co.elastic.clients.elasticsearch.core.SearchResponse)2 BulkOperation (co.elastic.clients.elasticsearch.core.bulk.BulkOperation)2 Hit (co.elastic.clients.elasticsearch.core.search.Hit)2 TotalHits (co.elastic.clients.elasticsearch.core.search.TotalHits)2 TotalHitsRelation (co.elastic.clients.elasticsearch.core.search.TotalHitsRelation)2 Address (com.erudika.para.core.Address)2 App (com.erudika.para.core.App)2 ParaObject (com.erudika.para.core.ParaObject)2