Search in sources :

Example 1 with SearchResult

use of io.kestra.core.models.SearchResult in project kestra by kestra-io.

the class ElasticSearchFlowRepository method findSourceCode.

@Override
public ArrayListTotal<SearchResult<Flow>> findSourceCode(String query, Pageable pageable) {
    BoolQueryBuilder bool = this.defaultFilter().must(QueryBuilders.queryStringQuery(query).field("sourceCode"));
    SearchSourceBuilder sourceBuilder = this.searchSource(bool, Optional.empty(), pageable);
    sourceBuilder.fetchSource("*", "sourceCode");
    sourceBuilder.highlighter(new HighlightBuilder().preTags("[mark]").postTags("[/mark]").field("sourceCode"));
    SearchRequest searchRequest = searchRequest(INDEX_NAME, sourceBuilder, false);
    try {
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        return new ArrayListTotal<>(Arrays.stream(searchResponse.getHits().getHits()).map(documentFields -> {
            try {
                return new SearchResult<>(MAPPER.readValue(documentFields.getSourceAsString(), this.cls), documentFields.getHighlightFields().get("sourceCode") != null ? Arrays.stream(documentFields.getHighlightFields().get("sourceCode").getFragments()).map(Text::string).collect(Collectors.toList()) : Collections.emptyList());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).collect(Collectors.toList()), searchResponse.getHits().getTotalHits().value);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) SearchResult(io.kestra.core.models.SearchResult) IOException(java.io.IOException) ArrayListTotal(io.kestra.core.repositories.ArrayListTotal) HighlightBuilder(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

SearchResult (io.kestra.core.models.SearchResult)1 ArrayListTotal (io.kestra.core.repositories.ArrayListTotal)1 IOException (java.io.IOException)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)1 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)1 HighlightBuilder (org.opensearch.search.fetch.subphase.highlight.HighlightBuilder)1