All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polyglotted.elastic.search.Searcher Maven / Gradle / Ivy

There is a newer version: 6.8.5
Show newest version
package io.polyglotted.elastic.search;

import io.polyglotted.common.model.AuthHeader;
import io.polyglotted.elastic.client.ElasticClient;
import io.polyglotted.elastic.common.Verbose;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchScrollRequest;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;

import java.util.List;

import static io.polyglotted.common.util.BaseSerializer.MAPPER;
import static io.polyglotted.common.util.CollUtil.transformList;
import static io.polyglotted.elastic.common.DocResult.docSource;
import static io.polyglotted.elastic.search.Finder.findAllBy;
import static io.polyglotted.elastic.search.Finder.findBy;
import static io.polyglotted.elastic.search.Finder.findById;
import static io.polyglotted.elastic.search.QueryMaker.scrollRequest;
import static io.polyglotted.elastic.search.SearchUtil.buildAggs;
import static io.polyglotted.elastic.search.SearchUtil.clearScroll;
import static io.polyglotted.elastic.search.SearchUtil.getReturnedHits;
import static io.polyglotted.elastic.search.SearchUtil.getTotalHits;
import static io.polyglotted.elastic.search.SearchUtil.headerFrom;
import static io.polyglotted.elastic.search.SearchUtil.performScroll;
import static io.polyglotted.elastic.search.SearchUtil.responseBuilder;
import static org.elasticsearch.common.xcontent.XContentType.JSON;
import static org.elasticsearch.search.fetch.subphase.FetchSourceContext.FETCH_SOURCE;

@SuppressWarnings({"unused", "WeakerAccess"})
@Slf4j @RequiredArgsConstructor
public final class Searcher {
    private final ElasticClient client;

    public  List getAllBy(String repo, String model, Expression expr, int size, FetchSourceContext context,
                                ResultBuilder resultBuilder, Verbose verbose) {
        return getAllBy(null, repo, model, expr, size, context, resultBuilder, verbose);
    }

    public  List getAllBy(AuthHeader auth, String repo, String model, Expression expr, int size,
                                FetchSourceContext context, ResultBuilder builder, Verbose verbose) {
        return transformList(findAllBy(client, auth, repo, expr, size, context), docResult -> builder.buildVerbose(docSource(docResult), verbose));
    }

    public  T getById(String repo, String model, String id, ResultBuilder resultBuilder, Verbose verbose) {
        return getById(null, repo, model, id, null, FETCH_SOURCE, resultBuilder, verbose);
    }

    public  T getById(AuthHeader auth, String repo, String model, String id, ResultBuilder resultBuilder, Verbose verbose) {
        return getById(auth, repo, model, id, null, FETCH_SOURCE, resultBuilder, verbose);
    }

    public  T getById(String repo, String model, String id, String parent, ResultBuilder resultBuilder, Verbose verbose) {
        return getById(null, repo, model, id, parent, resultBuilder, verbose);
    }

    public  T getById(AuthHeader auth, String repo, String model, String id, String parent, ResultBuilder resultBuilder, Verbose verbose) {
        return getById(auth, repo, model, id, parent, FETCH_SOURCE, resultBuilder, verbose);
    }

    public  T getById(String repo, String model, String id, String parent, FetchSourceContext ctx, ResultBuilder builder, Verbose verbose) {
        return getById(null, repo, model, id, parent, ctx, builder, verbose);
    }

    public  T getById(AuthHeader auth, String repo, String model, String id, String parent,
                         FetchSourceContext ctx, ResultBuilder builder, Verbose verbose) {
        return builder.buildVerbose(docSource(findById(client, auth, repo, model, id, parent, ctx)), verbose);
    }

    public  T getByExpr(String repo, Expression expr, FetchSourceContext ctx, ResultBuilder builder, Verbose verbose) {
        return getByExpr(null, repo, expr, ctx, builder, verbose);
    }

    public  T getByExpr(AuthHeader auth, String repo, Expression expr, FetchSourceContext ctx, ResultBuilder builder, Verbose verbose) {
        return builder.buildVerbose(docSource(findBy(client, auth, repo, expr, ctx)), verbose);
    }

    public  QueryResponse searchBy(SearchRequest request, ResponseBuilder resultBuilder, Verbose verbose) {
        return searchBy(null, request, resultBuilder, verbose);
    }

    public  QueryResponse searchBy(AuthHeader auth, SearchRequest request, ResponseBuilder resultBuilder, Verbose verbose) {
        return responseBuilder(auth == null ? client.search(request) : client.search(auth, request), resultBuilder, verbose).build();
    }

    public  QueryResponse scroll(String scrollId, TimeValue scrollTime, ResponseBuilder resultBuilder, Verbose verbose) {
        return scroll(null, scrollId, scrollTime, resultBuilder, verbose);
    }

    public  QueryResponse scroll(AuthHeader auth, String scrollId, TimeValue scrollTime, ResponseBuilder resultBuilder, Verbose verbose) {
        SearchScrollRequest request = scrollRequest(scrollId, scrollTime);
        return responseBuilder(auth == null ? client.searchScroll(request) : client.searchScroll(auth, request), resultBuilder, verbose).build();
    }

    public  String searchNative(SearchRequest request, ResponseBuilder resultBuilder, boolean flattenAgg, Verbose verbose) {
        return searchNative(null, request, resultBuilder, flattenAgg, verbose);
    }

    @SneakyThrows
    public  String searchNative(AuthHeader auth, SearchRequest request, ResponseBuilder resultBuilder, boolean flattenAgg, Verbose verbose) {
        XContentBuilder result = XContentFactory.jsonBuilder().startObject();
        SearchResponse response = auth == null ? client.search(request) : client.search(auth, request);
        headerFrom(response, result);
        if (getReturnedHits(response) > 0) {
            List values = resultBuilder.buildFrom(response, verbose);
            result.rawField("results", new BytesArray(MAPPER.writeValueAsBytes(values)), JSON);
        }
        return buildAggs(response, flattenAgg, result).endObject().string();
    }

    public  boolean simpleScroll(SearchRequest request, ResponseBuilder resultBuilder, Verbose verbose, ScrollWalker walker) {
        return simpleScroll(null, request, resultBuilder, verbose, walker);
    }

    public  boolean simpleScroll(AuthHeader auth, SearchRequest request, ResponseBuilder resultBuilder, Verbose verbose, ScrollWalker walker) {
        boolean errored = false;
        SearchResponse response = auth == null ? client.search(request) : client.search(auth, request);
        log.info("performing scroll on " + getTotalHits(response) + " items");
        while (getReturnedHits(response) > 0) {
            errored = walker.walk(resultBuilder.buildFrom(response, verbose));
            if (errored) { clearScroll(client, auth, response); break; }
            response = performScroll(client, auth, response);
        }
        return errored;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy