Search in sources :

Example 1 with ESUtils.getPager

use of com.erudika.para.server.search.es.ESUtils.getPager 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;
}
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) SearchRequest(co.elastic.clients.elasticsearch.core.SearchRequest) SortOrder(co.elastic.clients.elasticsearch._types.SortOrder) ESUtils.getPager(com.erudika.para.server.search.es.ESUtils.getPager) Pager(com.erudika.para.core.utils.Pager) ParaObject(com.erudika.para.core.ParaObject) Map(java.util.Map) HashMap(java.util.HashMap) SortOptions(co.elastic.clients.elasticsearch._types.SortOptions)

Aggregations

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