<%#
 Copyright 2013-2023 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.repository.search;

import <%= entityAbsoluteClass %>;
import <%= packageName %>.repository.<%= persistClass %>Repository;

<%_ if (reactive) { _%>
import org.springframework.data.elasticsearch.client.elc.ReactiveElasticsearchTemplate;
<%_ } else { _%>
import org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate;
<%_ } _%>
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.QueryStringQuery;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.repository.<% if (reactive) {%>Reactive<% } %>ElasticsearchRepository;
<%_ if (reactive) { _%>

import reactor.core.publisher.Flux;
<%_ } else { _%>
import org.springframework.scheduling.annotation.Async;
    <%_ if (databaseTypeSql) { _%>
import org.springframework.transaction.annotation.Transactional;
    <%_ } _%>

import java.util.stream.Stream;
<%_ } _%>
<%_ if (databaseTypeCassandra || user.primaryKey.hasUUID) { _%>

import java.util.UUID;
<%_ } _%>

import static org.springframework.data.elasticsearch.client.elc.QueryBuilders.queryStringQuery;

/**
 * Spring Data Elasticsearch repository for the User entity.
 */
public interface UserSearchRepository extends <% if (reactive) {%>Reactive<% } %>ElasticsearchRepository<<%= user.persistClass %>, <%= user.primaryKey.type %>>, UserSearchRepositoryInternal {
}
<%_ if (reactive) { _%>

interface UserSearchRepositoryInternal {
    Flux<<%= persistClass %>> search(String query);
}

class UserSearchRepositoryInternalImpl implements UserSearchRepositoryInternal {

    private final ReactiveElasticsearchTemplate reactiveElasticsearchTemplate;

    UserSearchRepositoryInternalImpl(ReactiveElasticsearchTemplate reactiveElasticsearchTemplate) {
        this.reactiveElasticsearchTemplate = reactiveElasticsearchTemplate;
    }

    @Override
    public Flux<<%= persistClass %>> search(String query) {
        NativeQuery nativeQuery = new NativeQuery(QueryStringQuery.of(qs -> qs.query(query))._toQuery());
        return reactiveElasticsearchTemplate.search(nativeQuery, User.class).map(SearchHit::getContent);
    }
}
<%_ } else { _%>

interface UserSearchRepositoryInternal {
    Stream<<%= persistClass %>> search(String query);

    @Async
    <%_ if (databaseTypeSql) { _%>
    @Transactional
    <%_ } _%>
    void index(<%= persistClass %> entity);

    @Async
    <%_ if (databaseTypeSql) { _%>
    @Transactional
    <%_ } _%>
    void deleteFromIndex(<%= persistClass %> entity);
}

class UserSearchRepositoryInternalImpl implements UserSearchRepositoryInternal {

    private final ElasticsearchTemplate elasticsearchTemplate;
    private final UserRepository repository;

    UserSearchRepositoryInternalImpl(ElasticsearchTemplate elasticsearchTemplate, UserRepository repository) {
        this.elasticsearchTemplate = elasticsearchTemplate;
        this.repository = repository;
    }

    @Override
    public Stream<<%= persistClass %>> search(String query) {
        NativeQuery nativeQuery = new NativeQuery(QueryStringQuery.of(qs -> qs.query(query))._toQuery());
        return elasticsearchTemplate
            .search(nativeQuery, User.class)
            .map(SearchHit::getContent)
            .stream();
    }

    @Override
    public void index(<%= persistClass %> entity) {
        repository.findById(entity.getId()).ifPresent(elasticsearchTemplate::save);
    }

    @Override
    public void deleteFromIndex(<%= persistClass %> entity) {
        elasticsearchTemplate.delete(entity);
    }
}
<%_ } _%>
