com.github.wz2cool.elasticsearch.operator.MultiMatchOperator Maven / Gradle / Ivy
package com.github.wz2cool.elasticsearch.operator;
import com.github.wz2cool.elasticsearch.cache.EntityCache;
import com.github.wz2cool.elasticsearch.helper.CommonsHelper;
import com.github.wz2cool.elasticsearch.lambda.GetPropertyFunction;
import com.github.wz2cool.elasticsearch.lambda.GetStringPropertyFunction;
import com.github.wz2cool.elasticsearch.model.ColumnInfo;
import com.github.wz2cool.elasticsearch.model.FilterMode;
import com.github.wz2cool.elasticsearch.model.PropertyInfo;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class MultiMatchOperator {
private static final float DEFAULT_BOOST = 1.0f;
private final Map fieldMap = new HashMap<>();
private String analyzer;
private Integer slop;
private String fuzziness;
private Integer prefixLength;
private Integer maxExpansions;
private String minimumShouldMatch;
private Float tieBreaker;
private Boolean lenient;
private Float cutoffFrequency;
private Boolean fuzzyTranspositions;
private MultiMatchQueryBuilder.Type type;
public FilterMode getDefaultFilterMode() {
return FilterMode.MUST;
}
public MultiMatchOperator() {
}
public MultiMatchOperator(GetStringPropertyFunction[] getPropertyFuncs) {
for (GetStringPropertyFunction getPropertyFunc : getPropertyFuncs) {
field(getPropertyFunc);
}
}
/**
* See also {@link MultiMatchQueryBuilder#field(String field)}
*/
public MultiMatchOperator field(GetStringPropertyFunction getPropertyFunc) {
final ColumnInfo columnInfo = getColumnInfo(getPropertyFunc);
fieldMap.put(columnInfo.getColumnName(), DEFAULT_BOOST);
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#field(String field)}
*/
public MultiMatchOperator field(GetStringPropertyFunction getPropertyFunc, float boost) {
final ColumnInfo columnInfo = getColumnInfo(getPropertyFunc);
fieldMap.put(columnInfo.getColumnName(), boost);
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#field(String field)}
*/
public MultiMatchOperator field(GetPropertyFunction getP1Func, GetStringPropertyFunction getPropertyFunc) {
return field(getP1Func, getPropertyFunc, DEFAULT_BOOST);
}
/**
* See also {@link MultiMatchQueryBuilder#field(String field, float boost)}
*/
public MultiMatchOperator field(GetPropertyFunction getP1Func, GetStringPropertyFunction getPropertyFunc, float boost) {
final ColumnInfo columnInfo = getColumnInfo(getP1Func);
final ColumnInfo columnInfo1 = getColumnInfo(getPropertyFunc);
String columnName = columnInfo.getColumnName() + "." + columnInfo1.getColumnName();
fieldMap.put(columnName, boost);
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#minimumShouldMatch(String minimumShouldMatch)}
*/
public MultiMatchOperator minimumShouldMatch(String minimumShouldMatch) {
this.minimumShouldMatch = minimumShouldMatch;
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#analyzer(String analyzer)}
*/
public MultiMatchOperator analyzer(String analyzer) {
this.analyzer = analyzer;
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#slop(int slop)}
*/
public MultiMatchOperator slop(int slop) {
this.slop = slop;
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#fuzziness(Object fuzziness)}
*/
public MultiMatchOperator fuzziness(String fuzziness) {
this.fuzziness = fuzziness;
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#prefixLength(int prefixLength)}
*/
public MultiMatchOperator prefixLength(Integer prefixLength) {
this.prefixLength = prefixLength;
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#tieBreaker(float tieBreaker)}
*/
public MultiMatchOperator tieBreaker(Float tieBreaker) {
this.tieBreaker = tieBreaker;
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#cutoffFrequency(float cutoff)}
*/
public MultiMatchOperator cutoffFrequency(Float cutoff) {
this.cutoffFrequency = cutoff;
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#fuzzyTranspositions(boolean fuzzyTranspositions)}
*/
public MultiMatchOperator fuzzyTranspositions(Boolean fuzzyTranspositions) {
this.fuzzyTranspositions = fuzzyTranspositions;
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#maxExpansions(int)}
*/
public MultiMatchOperator maxExpansions(Integer maxExpansions) {
this.maxExpansions = maxExpansions;
return this;
}
/**
* See also {@link MultiMatchQueryBuilder#lenient(boolean)}
*/
public MultiMatchOperator lenient(Boolean lenient) {
this.lenient = lenient;
return this;
}
public MultiMatchOperator type(MultiMatchQueryBuilder.Type type) {
this.type = type;
return this;
}
public QueryBuilder buildQuery(String value) {
final MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(value);
for (Map.Entry entry : fieldMap.entrySet()) {
multiMatchQueryBuilder.field(entry.getKey(), entry.getValue());
}
if (Objects.nonNull(analyzer)) {
multiMatchQueryBuilder.analyzer(analyzer);
}
if (Objects.nonNull(slop)) {
multiMatchQueryBuilder.slop(slop);
}
if (Objects.nonNull(fuzziness)) {
multiMatchQueryBuilder.fuzziness(fuzziness);
}
if (Objects.nonNull(prefixLength)) {
multiMatchQueryBuilder.prefixLength(prefixLength);
}
if (Objects.nonNull(maxExpansions)) {
multiMatchQueryBuilder.maxExpansions(maxExpansions);
}
if (Objects.nonNull(minimumShouldMatch)) {
multiMatchQueryBuilder.minimumShouldMatch(minimumShouldMatch);
}
if (Objects.nonNull(tieBreaker)) {
multiMatchQueryBuilder.tieBreaker(tieBreaker);
}
if (Objects.nonNull(lenient)) {
multiMatchQueryBuilder.lenient(lenient);
}
if (Objects.nonNull(cutoffFrequency)) {
multiMatchQueryBuilder.cutoffFrequency(cutoffFrequency);
}
if (Objects.nonNull(fuzzyTranspositions)) {
multiMatchQueryBuilder.fuzzyTranspositions(fuzzyTranspositions);
}
if (Objects.nonNull(type)) {
multiMatchQueryBuilder.type(type);
}
return multiMatchQueryBuilder;
}
private ColumnInfo getColumnInfo(GetPropertyFunction getPropertyFunc) {
final PropertyInfo propertyInfo = CommonsHelper.getPropertyInfo(getPropertyFunc);
return EntityCache.getInstance().getColumnInfo(propertyInfo.getOwnerClass(), propertyInfo.getPropertyName());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy