xyz.erupt.es.process.EruptElasticSearchProcess Maven / Gradle / Ivy
package xyz.erupt.es.process;
import lombok.SneakyThrows;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import xyz.erupt.annotation.query.Condition;
import xyz.erupt.core.config.GsonFactory;
import xyz.erupt.core.invoke.DataProcessorManager;
import xyz.erupt.core.query.Column;
import xyz.erupt.core.query.EruptQuery;
import xyz.erupt.core.service.IEruptDataService;
import xyz.erupt.core.view.EruptModel;
import xyz.erupt.core.view.Page;
import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;
/**
* @author YuePeng
* date 2020-03-06.
*/
@Service
public class EruptElasticSearchProcess implements IEruptDataService {
public static final String ELASTICSEARCH_PROCESS = "es";
static {
DataProcessorManager.register(ELASTICSEARCH_PROCESS, EruptElasticSearchProcess.class);
}
@Resource
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Override
public Object findDataById(EruptModel eruptModel, Object id) {
return elasticsearchRestTemplate.get(id.toString(), eruptModel.getClazz());
}
@SneakyThrows
@Override
public Page queryList(EruptModel eruptModel, Page page, EruptQuery eruptQuery) {
NativeSearchQueryBuilder nativeSearchQuery = new NativeSearchQueryBuilder();
this.addQueryCondition(eruptQuery, nativeSearchQuery);
// .withQuery(QueryBuilders.multiMatchQuery("哈哈哈哈", "content", "type"))
// .withQuery(QueryBuilders.matchQuery("content", "ははab"))
// .withQuery(QueryBuilders.matchPhrasePrefixQuery("str", "abc"))
// .withQuery(QueryBuilders.rangeQuery("date").lte(new Date()));
page.setTotal(elasticsearchRestTemplate.count(nativeSearchQuery.build(), eruptModel.getClazz()));
// nativeSearchQuery.withSorts(SortBuilder.);
nativeSearchQuery.withPageable(PageRequest.of(page.getPageIndex() - 1, page.getPageSize()));
SearchHits> search = elasticsearchRestTemplate.search(nativeSearchQuery.build(), eruptModel.getClazz());
List