All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redis.lettucemod.search.SearchOptions Maven / Gradle / Ivy

The newest version!
package com.redis.lettucemod.search;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.OptionalLong;

import com.redis.lettucemod.protocol.SearchCommandKeyword;

public class SearchOptions extends BaseSearchOptions {

	private boolean noContent;
	private boolean noStopWords;
	private boolean withScores;
	private boolean withPayloads;
	private boolean withSortKeys;
	private List> filters = new ArrayList<>();
	private Optional> geoFilter = Optional.empty();
	private List inKeys = new ArrayList<>();
	private List inFields = new ArrayList<>();
	private List returnFields = new ArrayList<>();
	private Optional> summarize = Optional.empty();
	private Optional> highlight = Optional.empty();
	private OptionalLong slop = OptionalLong.empty();
	private boolean inOrder;
	private Optional language = Optional.empty();
	private Optional expander = Optional.empty();
	private Optional scorer = Optional.empty();
	private Optional payload = Optional.empty();
	private Optional> sortBy = Optional.empty();

	public SearchOptions() {

	}

	private SearchOptions(Builder builder) {
		super(builder);
		this.noContent = builder.noContent;
		this.noStopWords = builder.noStopWords;
		this.withScores = builder.withScores;
		this.withPayloads = builder.withPayloads;
		this.withSortKeys = builder.withSortKeys;
		this.filters = builder.filters;
		this.geoFilter = builder.geoFilter;
		this.inKeys = builder.inKeys;
		this.inFields = builder.inFields;
		this.returnFields = builder.returnFields;
		this.summarize = builder.summarize;
		this.highlight = builder.highlight;
		this.slop = builder.slop;
		this.inOrder = builder.inOrder;
		this.language = builder.language;
		this.expander = builder.expander;
		this.scorer = builder.scorer;
		this.payload = builder.payload;
		this.sortBy = builder.sortBy;
	}

	public boolean isNoContent() {
		return noContent;
	}

	public void setNoContent(boolean noContent) {
		this.noContent = noContent;
	}

	public boolean isNoStopWords() {
		return noStopWords;
	}

	public void setNoStopWords(boolean noStopWords) {
		this.noStopWords = noStopWords;
	}

	public boolean isWithScores() {
		return withScores;
	}

	public void setWithScores(boolean withScores) {
		this.withScores = withScores;
	}

	public boolean isWithPayloads() {
		return withPayloads;
	}

	public void setWithPayloads(boolean withPayloads) {
		this.withPayloads = withPayloads;
	}

	public boolean isWithSortKeys() {
		return withSortKeys;
	}

	public void setWithSortKeys(boolean withSortKeys) {
		this.withSortKeys = withSortKeys;
	}

	public List> getFilters() {
		return filters;
	}

	public void setFilters(List> filters) {
		this.filters = filters;
	}

	public Optional> getGeoFilter() {
		return geoFilter;
	}

	public void setGeoFilter(GeoFilter geoFilter) {
		this.geoFilter = Optional.of(geoFilter);
	}

	public List getInKeys() {
		return inKeys;
	}

	public void setInKeys(List inKeys) {
		this.inKeys = inKeys;
	}

	public List getInFields() {
		return inFields;
	}

	public void setInFields(List inFields) {
		this.inFields = inFields;
	}

	public List getReturnFields() {
		return returnFields;
	}

	public void setReturnFields(List returnFields) {
		this.returnFields = returnFields;
	}

	public Optional> getSummarize() {
		return summarize;
	}

	public void setSummarize(Summarize summarize) {
		this.summarize = Optional.of(summarize);
	}

	public Optional> getHighlight() {
		return highlight;
	}

	public void setHighlight(Highlight highlight) {
		this.highlight = Optional.of(highlight);
	}

	public OptionalLong getSlop() {
		return slop;
	}

	public void setSlop(long slop) {
		this.slop = OptionalLong.of(slop);
	}

	public boolean isInOrder() {
		return inOrder;
	}

	public void setInOrder(boolean inOrder) {
		this.inOrder = inOrder;
	}

	public Optional getLanguage() {
		return language;
	}

	public void setLanguage(Language language) {
		this.language = Optional.of(language);
	}

	public Optional getExpander() {
		return expander;
	}

	public void setExpander(String expander) {
		this.expander = Optional.of(expander);
	}

	public Optional getScorer() {
		return scorer;
	}

	public void setScorer(String scorer) {
		this.scorer = Optional.of(scorer);
	}

	public Optional getPayload() {
		return payload;
	}

	public void setPayload(V payload) {
		this.payload = Optional.of(payload);
	}

	public Optional> getSortBy() {
		return sortBy;
	}

	public void setSortBy(SortBy sortBy) {
		this.sortBy = Optional.of(sortBy);
	}

	@Override
	public void build(SearchCommandArgs args) {
		super.build(args);
		if (noContent) {
			args.add(SearchCommandKeyword.NOCONTENT);
		}
		if (noStopWords) {
			args.add(SearchCommandKeyword.NOSTOPWORDS);
		}
		if (withScores) {
			args.add(SearchCommandKeyword.WITHSCORES);
		}
		if (withPayloads) {
			args.add(SearchCommandKeyword.WITHPAYLOADS);
		}
		if (withSortKeys) {
			args.add(SearchCommandKeyword.WITHSORTKEYS);
		}
		for (NumericFilter filter : filters) {
			args.add(SearchCommandKeyword.FILTER);
			filter.build(args);
		}
		geoFilter.ifPresent(f -> {
			args.add(SearchCommandKeyword.GEOFILTER);
			f.build(args);
		});
		if (!inKeys.isEmpty()) {
			args.add(SearchCommandKeyword.INKEYS);
			args.add(inKeys.size());
			inKeys.forEach(args::addKey);
		}
		if (!inFields.isEmpty()) {
			args.add(SearchCommandKeyword.INFIELDS);
			args.add(inFields.size());
			inFields.forEach(args::addKey);
		}
		if (!returnFields.isEmpty()) {
			args.add(SearchCommandKeyword.RETURN);
			args.add(returnFields.size());
			returnFields.forEach(args::addKey);
		}
		summarize.ifPresent(s -> {
			args.add(SearchCommandKeyword.SUMMARIZE);
			s.build(args);
		});
		highlight.ifPresent(h -> {
			args.add(SearchCommandKeyword.HIGHLIGHT);
			h.build(args);
		});
		slop.ifPresent(s -> args.add(SearchCommandKeyword.SLOP).add(s));
		if (inOrder) {
			args.add(SearchCommandKeyword.INORDER);
		}
		language.ifPresent(l -> args.add(SearchCommandKeyword.LANGUAGE).add(l.getId()));
		expander.ifPresent(e -> args.add(SearchCommandKeyword.EXPANDER).add(e));
		scorer.ifPresent(s -> args.add(SearchCommandKeyword.SCORER).add(s));
		payload.ifPresent(p -> args.add(SearchCommandKeyword.PAYLOAD).addValue(p));
		sortBy.ifPresent(s -> s.build(args));
	}

	public static Limit limit(long offset, long num) {
		return new Limit(offset, num);
	}

	public static class NumericFilter implements RediSearchArgument {

		private final K field;
		private final double min;
		private final double max;

		public NumericFilter(K field, double min, double max) {
			this.field = field;
			this.min = min;
			this.max = max;
		}

		@Override
		public void build(SearchCommandArgs args) {
			args.addKey(field);
			args.add(min);
			args.add(max);
		}

		public static  Builder field(K field) {
			return new Builder<>(field);
		}

		public static class Builder {

			private final K field;

			public Builder(K field) {
				this.field = field;
			}

			public MaxNumericFilterBuilder min(double min) {
				return new MaxNumericFilterBuilder<>(field, min);
			}

		}

		public static class MaxNumericFilterBuilder {

			private final K field;
			private final double min;

			public MaxNumericFilterBuilder(K field, double min) {
				this.field = field;
				this.min = min;
			}

			public NumericFilter max(double max) {
				return new NumericFilter<>(field, min, max);
			}
		}
	}

	public static class GeoFilter implements RediSearchArgument {

		private K field;
		private double longitude;
		private double latitude;
		private double radius;
		private String unit;

		public GeoFilter() {
		}

		public GeoFilter(K field, double longitude, double latitude, double radius, String unit) {
			this.field = field;
			this.longitude = longitude;
			this.latitude = latitude;
			this.radius = radius;
			this.unit = unit;
		}

		public K getField() {
			return field;
		}

		public void setField(K field) {
			this.field = field;
		}

		public double getLongitude() {
			return longitude;
		}

		public void setLongitude(double longitude) {
			this.longitude = longitude;
		}

		public double getLatitude() {
			return latitude;
		}

		public void setLatitude(double latitude) {
			this.latitude = latitude;
		}

		public double getRadius() {
			return radius;
		}

		public void setRadius(double radius) {
			this.radius = radius;
		}

		public String getUnit() {
			return unit;
		}

		public void setUnit(String unit) {
			this.unit = unit;
		}

		@Override
		public void build(SearchCommandArgs args) {
			args.addKey(field);
			args.add(longitude);
			args.add(latitude);
			args.add(radius);
			args.add(unit);
		}

		public static  Builder field(K field) {
			return new Builder<>(field);
		}

		public static class Builder {

			private final K field;
			private double longitude;
			private double latitude;
			private double radius;
			private String unit;

			public Builder(K field) {
				this.field = field;
			}

			public Builder longitude(double longitude) {
				this.longitude = longitude;
				return this;
			}

			public Builder latitude(double latitude) {
				this.latitude = latitude;
				return this;
			}

			public Builder radius(double radius) {
				this.radius = radius;
				return this;
			}

			public Builder unit(String unit) {
				this.unit = unit;
				return this;
			}

			public GeoFilter build() {
				return new GeoFilter<>(field, longitude, latitude, radius, unit);
			}

		}
	}

	public static class Highlight implements RediSearchArgument {

		private final List fields;
		private final Optional> tags;

		private Highlight(Builder builder) {
			this.fields = builder.fields;
			this.tags = builder.tags;
		}

		public static  Builder builder() {
			return new Builder<>();
		}

		public static class Builder {

			private List fields = new ArrayList<>();
			private Optional> tags = Optional.empty();

			public Builder field(K field) {
				this.fields.add(field);
				return this;
			}

			@SuppressWarnings("unchecked")
			public Builder fields(K... fields) {
				this.fields.addAll(Arrays.asList(fields));
				return this;
			}

			public Builder tags(Tags tags) {
				this.tags = Optional.of(tags);
				return this;
			}

			public Builder tags(V open, V close) {
				return tags(new Tags<>(open, close));
			}

			public Highlight build() {
				return new Highlight<>(this);
			}

		}

		@Override
		public void build(SearchCommandArgs args) {
			if (!fields.isEmpty()) {
				args.add(SearchCommandKeyword.FIELDS);
				args.add(fields.size());
				fields.forEach(args::addKey);
			}
			tags.ifPresent(t -> args.add(SearchCommandKeyword.TAGS).addValue(t.getOpen()).addValue(t.getClose()));
		}

		public static class Tags {

			private V open;
			private V close;

			public Tags(V open, V close) {
				super();
				this.open = open;
				this.close = close;
			}

			public V getOpen() {
				return open;
			}

			public void setOpen(V open) {
				this.open = open;
			}

			public V getClose() {
				return close;
			}

			public void setClose(V close) {
				this.close = close;
			}

		}

	}

	public static class Summarize implements RediSearchArgument {

		private List fields = new ArrayList<>();
		private OptionalLong frags = OptionalLong.empty();
		private OptionalLong length = OptionalLong.empty();
		private Optional separator = Optional.empty();

		public List getFields() {
			return fields;
		}

		public void setFields(List fields) {
			this.fields = fields;
		}

		public OptionalLong getFrags() {
			return frags;
		}

		public void setFrags(long frags) {
			this.frags = OptionalLong.of(frags);
		}

		public OptionalLong getLength() {
			return length;
		}

		public void setLength(long length) {
			this.length = OptionalLong.of(length);
		}

		public Optional getSeparator() {
			return separator;
		}

		public void setSeparator(V separator) {
			this.separator = Optional.of(separator);
		}

		@Override
		public void build(SearchCommandArgs args) {
			if (!fields.isEmpty()) {
				args.add(SearchCommandKeyword.FIELDS);
				args.add(fields.size());
				fields.forEach(args::addKey);
			}
			frags.ifPresent(f -> args.add(SearchCommandKeyword.FRAGS).add(f));
			length.ifPresent(l -> args.add(SearchCommandKeyword.LEN).add(l));
			separator.ifPresent(s -> args.add(SearchCommandKeyword.SEPARATOR).addValue(s));
		}
	}

	public static class SortBy implements RediSearchArgument {

		private final K field;
		private final Order direction;

		public SortBy(K field, Order direction) {
			this.field = field;
			this.direction = direction;
		}

		public K getField() {
			return field;
		}

		public Order getDirection() {
			return direction;
		}

		@Override
		public void build(SearchCommandArgs args) {
			args.add(SearchCommandKeyword.SORTBY).addKey(field);
			args.add(direction.getKeyword());
		}

		public static  SortBy asc(K field) {
			return new SortBy<>(field, Order.ASC);
		}

		public static  SortBy desc(K field) {
			return new SortBy<>(field, Order.DESC);
		}

	}

	public static  Builder builder() {
		return new Builder<>();
	}

	public static final class Builder extends BaseSearchOptions.Builder> {

		private boolean noContent;
		private boolean noStopWords;
		private boolean withScores;
		private boolean withPayloads;
		private boolean withSortKeys;
		private List> filters = new ArrayList<>();
		private Optional> geoFilter = Optional.empty();
		private List inKeys = new ArrayList<>();
		private List inFields = new ArrayList<>();
		private List returnFields = new ArrayList<>();
		private Optional> summarize = Optional.empty();
		private Optional> highlight = Optional.empty();
		private OptionalLong slop = OptionalLong.empty();
		private boolean inOrder;
		private Optional language = Optional.empty();
		private Optional expander = Optional.empty();
		private Optional scorer = Optional.empty();
		private Optional payload = Optional.empty();
		private Optional> sortBy = Optional.empty();

		private Builder() {
		}

		public Builder noContent() {
			return noContent(true);
		}

		public Builder noContent(boolean noContent) {
			this.noContent = noContent;
			return this;
		}

		public Builder noStopWords() {
			return noStopWords(true);
		}

		public Builder noStopWords(boolean noStopWords) {
			this.noStopWords = noStopWords;
			return this;
		}

		public Builder withScores() {
			return withScores(true);
		}

		public Builder withScores(boolean withScores) {
			this.withScores = withScores;
			return this;
		}

		public Builder withPayloads() {
			return withPayloads(true);
		}

		public Builder withPayloads(boolean withPayloads) {
			this.withPayloads = withPayloads;
			return this;
		}

		public Builder withSortKeys() {
			return withSortKeys(true);
		}

		public Builder withSortKeys(boolean withSortKeys) {
			this.withSortKeys = withSortKeys;
			return this;
		}

		public Builder filter(NumericFilter filter) {
			this.filters.add(filter);
			return this;
		}

		@SuppressWarnings("unchecked")
		public Builder filters(NumericFilter... filters) {
			this.filters.addAll(Arrays.asList(filters));
			return this;
		}

		public Builder geoFilter(GeoFilter geoFilter) {
			this.geoFilter = Optional.of(geoFilter);
			return this;
		}

		public Builder inKey(K inKey) {
			this.inKeys.add(inKey);
			return this;
		}

		@SuppressWarnings("unchecked")
		public Builder inKeys(K... inKeys) {
			this.inKeys.addAll(Arrays.asList(inKeys));
			return this;
		}

		public Builder inField(K inField) {
			this.inFields.add(inField);
			return this;
		}

		@SuppressWarnings("unchecked")
		public Builder inFields(K... inFields) {
			this.inFields.addAll(Arrays.asList(inFields));
			return this;
		}

		public Builder returnField(K returnField) {
			this.returnFields.add(returnField);
			return this;
		}

		@SuppressWarnings("unchecked")
		public Builder returnFields(K... returnFields) {
			this.returnFields.addAll(Arrays.asList(returnFields));
			return this;
		}

		public Builder summarize(Summarize summarize) {
			this.summarize = Optional.of(summarize);
			return this;
		}

		public Builder highlight(Highlight highlight) {
			this.highlight = Optional.of(highlight);
			return this;
		}

		public Builder slop(long slop) {
			this.slop = OptionalLong.of(slop);
			return this;
		}

		public Builder inOrder() {
			return inOrder(true);
		}

		public Builder inOrder(boolean inOrder) {
			this.inOrder = inOrder;
			return this;
		}

		public Builder language(Language language) {
			this.language = Optional.of(language);
			return this;
		}

		public Builder expander(String expander) {
			this.expander = Optional.of(expander);
			return this;
		}

		public Builder scorer(String scorer) {
			this.scorer = Optional.of(scorer);
			return this;
		}

		public Builder payload(V payload) {
			this.payload = Optional.of(payload);
			return this;
		}

		public Builder sortBy(SortBy sortBy) {
			this.sortBy = Optional.of(sortBy);
			return this;
		}

		public SearchOptions build() {
			return new SearchOptions<>(this);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy