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

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

There is a newer version: 4.1.2
Show newest version
package com.redis.lettucemod.search;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import io.lettuce.core.internal.LettuceAssert;

public class Document extends LinkedHashMap {

	private static final long serialVersionUID = 1L;

	private K id;
	private Double score;
	private V sortKey;
	private V payload;

	public K getId() {
		return id;
	}

	public void setId(K id) {
		this.id = id;
	}

	public Double getScore() {
		return score;
	}

	public void setScore(Double score) {
		this.score = score;
	}

	public V getSortKey() {
		return sortKey;
	}

	public void setSortKey(V sortKey) {
		this.sortKey = sortKey;
	}

	public V getPayload() {
		return payload;
	}

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

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + Objects.hash(id, payload, score, sortKey);
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Document other = (Document) obj;
		return Objects.equals(id, other.id) && Objects.equals(payload, other.payload)
				&& Objects.equals(score, other.score) && Objects.equals(sortKey, other.sortKey);
	}

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

	public static class Builder {

		private final K id;
		private double score = 1;
		private V payload;
		private Map fields = new HashMap<>();

		public Builder(K id) {
			super();
			this.id = id;
		}

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

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

		public Builder field(K name, V value) {
			fields.put(name, value);
			return this;
		}

		public Document build() {
			LettuceAssert.notNull(id, "Id is required.");
			LettuceAssert.notNull(fields, "Fields are required.");
			Document document = new Document<>();
			document.setId(id);
			document.setScore(score);
			document.setPayload(payload);
			document.putAll(fields);
			return document;
		}

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy