com.github.wz2cool.elasticsearch.query.DynamicQuery Maven / Gradle / Ivy
package com.github.wz2cool.elasticsearch.query;
import com.github.wz2cool.elasticsearch.cache.EntityCache;
import com.github.wz2cool.elasticsearch.core.HighlightResultMapper;
import com.github.wz2cool.elasticsearch.helper.CommonsHelper;
import com.github.wz2cool.elasticsearch.lambda.GetCommonPropertyFunction;
import com.github.wz2cool.elasticsearch.lambda.GetPropertyFunction;
import com.github.wz2cool.elasticsearch.model.ColumnInfo;
import com.github.wz2cool.elasticsearch.model.PropertyInfo;
import com.github.wz2cool.elasticsearch.model.QueryMode;
import org.apache.commons.lang3.ArrayUtils;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.ScoreSortBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.data.elasticsearch.core.query.FetchSourceFilter;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SourceFilter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
@SuppressWarnings("java:S3740")
public class DynamicQuery extends BaseFilterGroup> implements IElasticsearchQuery {
private final Class clazz;
private final HighlightResultMapper highlightResultMapper = new HighlightResultMapper();
private final HighlightBuilder highlightBuilder = new HighlightBuilder();
private final List sortBuilders = new ArrayList<>();
private final QueryMode queryMode;
private String[] selectedColumns = new String[]{};
private String[] ignoredColumns = new String[]{};
public String[] getSelectedColumns() {
return selectedColumns;
}
public void setSelectedColumns(String[] selectedColumns) {
this.selectedColumns = selectedColumns;
}
public String[] getIgnoredColumns() {
return ignoredColumns;
}
public void setIgnoredColumns(String[] ignoredColumns) {
this.ignoredColumns = ignoredColumns;
}
private DynamicQuery(Class clazz, QueryMode queryMode) {
this.clazz = clazz;
this.queryMode = queryMode;
}
public static DynamicQuery createQuery(Class clazz) {
return new DynamicQuery<>(clazz, QueryMode.QUERY);
}
public static DynamicQuery createQuery(Class clazz, QueryMode queryMode) {
return new DynamicQuery<>(clazz, queryMode);
}
public DynamicQuery scoreMapping(BiConsumer setScorePropertyFunc) {
highlightResultMapper.registerScoreMapping(this.clazz, setScorePropertyFunc);
return this;
}
public DynamicQuery highlightMapping(GetPropertyFunction getSearchPropertyFunc,
BiConsumer setHighLightPropertyFunc) {
final PropertyInfo propertyInfo = CommonsHelper.getPropertyInfo(getSearchPropertyFunc);
final ColumnInfo columnInfo = EntityCache.getInstance().getColumnInfo(propertyInfo.getOwnerClass(), propertyInfo.getPropertyName());
highlightBuilder.field(columnInfo.getColumnName());
highlightResultMapper.registerHitMapping(this.clazz, getSearchPropertyFunc, setHighLightPropertyFunc);
return this;
}
public DynamicQuery orderByScore(SortOrder sortOrder) {
return orderByScore(true, sortOrder);
}
public DynamicQuery orderByScore(boolean enable, SortOrder sortOrder) {
if (enable) {
final ScoreSortBuilder order = new ScoreSortBuilder().order(sortOrder);
this.sortBuilders.add(order);
}
return this;
}
public DynamicQuery orderBy(GetCommonPropertyFunction getPropertyFunc, SortOrder sortOrder) {
return orderBy(true, getPropertyFunc, sortOrder);
}
public DynamicQuery orderBy(boolean enable, GetCommonPropertyFunction getPropertyFunc, SortOrder sortOrder) {
if (enable) {
final PropertyInfo propertyInfo = CommonsHelper.getPropertyInfo(getPropertyFunc);
ColumnInfo columnInfo = EntityCache.getInstance().getColumnInfo(propertyInfo.getOwnerClass(), propertyInfo.getPropertyName());
final FieldSortBuilder order = new FieldSortBuilder(columnInfo.getColumnName()).order(sortOrder);
this.sortBuilders.add(order);
}
return this;
}
@SafeVarargs
public final DynamicQuery select(GetPropertyFunction... getPropertyFunctions) {
String[] newSelectProperties = new String[getPropertyFunctions.length];
for (int i = 0; i < getPropertyFunctions.length; i++) {
newSelectProperties[i] = getColumnName(getPropertyFunctions[i]);
}
this.addSelectedColumns(newSelectProperties);
return this;
}
@SafeVarargs
public final DynamicQuery ignore(GetPropertyFunction... getPropertyFunctions) {
String[] newIgnoreProperties = new String[getPropertyFunctions.length];
for (int i = 0; i < getPropertyFunctions.length; i++) {
newIgnoreProperties[i] = getColumnName(getPropertyFunctions[i]);
}
this.ignoreSelectedColumns(newIgnoreProperties);
return this;
}
public HighlightResultMapper getHighlightResultMapper() {
return highlightResultMapper;
}
public HighlightBuilder getHighlightBuilder() {
return highlightBuilder;
}
public List getSortBuilders() {
return sortBuilders;
}
public Class getClazz() {
return clazz;
}
public QueryMode getQueryMode() {
return queryMode;
}
@Override
public NativeSearchQuery buildNativeSearch() {
NativeSearchQueryBuilder esQuery = new NativeSearchQueryBuilder();
if (getQueryMode() == QueryMode.QUERY) {
esQuery.withQuery(getFilterQuery());
} else {
esQuery.withFilter(getFilterQuery());
}
for (SortBuilder sortBuilder : getSortBuilders()) {
esQuery.withSort(sortBuilder);
}
final Optional sourceFilterOptional = getSourceFilter();
sourceFilterOptional.ifPresent(esQuery::withSourceFilter);
esQuery.withHighlightBuilder(getHighlightBuilder());
return esQuery.build();
}
public void addSelectedColumns(String... newSelectedProperties) {
setSelectedColumns(ArrayUtils.addAll(selectedColumns, newSelectedProperties));
}
public void ignoreSelectedColumns(String... newIgnoreProperties) {
setIgnoredColumns(ArrayUtils.addAll(ignoredColumns, newIgnoreProperties));
}
private Optional getSourceFilter() {
if (ArrayUtils.isEmpty(this.selectedColumns) && ArrayUtils.isEmpty(this.ignoredColumns)) {
return Optional.empty();
}
final FetchSourceFilter fetchSourceFilter = new FetchSourceFilter(this.selectedColumns, this.ignoredColumns);
return Optional.of(fetchSourceFilter);
}
}