|
@@ -21,6 +21,7 @@ package org.elasticsearch.search;
|
|
|
|
|
|
import org.apache.lucene.search.Explanation;
|
|
|
import org.elasticsearch.ElasticsearchParseException;
|
|
|
+import org.elasticsearch.Version;
|
|
|
import org.elasticsearch.action.OriginalIndices;
|
|
|
import org.elasticsearch.common.Nullable;
|
|
|
import org.elasticsearch.common.ParseField;
|
|
@@ -46,6 +47,7 @@ import org.elasticsearch.common.xcontent.XContentParser.Token;
|
|
|
import org.elasticsearch.index.mapper.IgnoredFieldMapper;
|
|
|
import org.elasticsearch.index.mapper.MapperService;
|
|
|
import org.elasticsearch.index.mapper.SourceFieldMapper;
|
|
|
+import org.elasticsearch.index.seqno.SequenceNumbers;
|
|
|
import org.elasticsearch.index.shard.ShardId;
|
|
|
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
|
|
import org.elasticsearch.search.lookup.SourceLookup;
|
|
@@ -91,6 +93,8 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
private NestedIdentity nestedIdentity;
|
|
|
|
|
|
private long version = -1;
|
|
|
+ private long seqNo = SequenceNumbers.UNASSIGNED_SEQ_NO;
|
|
|
+ private long primaryTerm = SequenceNumbers.UNASSIGNED_PRIMARY_TERM;
|
|
|
|
|
|
private BytesReference source;
|
|
|
|
|
@@ -168,6 +172,30 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
return this.version;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public void setSeqNo(long seqNo) {
|
|
|
+ this.seqNo = seqNo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setPrimaryTerm(long primaryTerm) {
|
|
|
+ this.primaryTerm = primaryTerm;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * returns the sequence number of the last modification to the document, or {@link SequenceNumbers#UNASSIGNED_SEQ_NO}
|
|
|
+ * if not requested.
|
|
|
+ **/
|
|
|
+ public long getSeqNo() {
|
|
|
+ return this.seqNo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * returns the primary term of the last modification to the document, or {@link SequenceNumbers#UNASSIGNED_PRIMARY_TERM}
|
|
|
+ * if not requested. */
|
|
|
+ public long getPrimaryTerm() {
|
|
|
+ return this.primaryTerm;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* The index of the hit.
|
|
|
*/
|
|
@@ -393,6 +421,8 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
static final String _TYPE = "_type";
|
|
|
static final String _ID = "_id";
|
|
|
static final String _VERSION = "_version";
|
|
|
+ static final String _SEQ_NO = "_seq_no";
|
|
|
+ static final String _PRIMARY_TERM = "_primary_term";
|
|
|
static final String _SCORE = "_score";
|
|
|
static final String FIELDS = "fields";
|
|
|
static final String HIGHLIGHT = "highlight";
|
|
@@ -453,6 +483,12 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
if (version != -1) {
|
|
|
builder.field(Fields._VERSION, version);
|
|
|
}
|
|
|
+
|
|
|
+ if (seqNo != SequenceNumbers.UNASSIGNED_SEQ_NO) {
|
|
|
+ builder.field(Fields._SEQ_NO, seqNo);
|
|
|
+ builder.field(Fields._PRIMARY_TERM, primaryTerm);
|
|
|
+ }
|
|
|
+
|
|
|
if (Float.isNaN(score)) {
|
|
|
builder.nullField(Fields._SCORE);
|
|
|
} else {
|
|
@@ -537,6 +573,8 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
parser.declareField((map, value) -> map.put(Fields._SCORE, value), SearchHit::parseScore, new ParseField(Fields._SCORE),
|
|
|
ValueType.FLOAT_OR_NULL);
|
|
|
parser.declareLong((map, value) -> map.put(Fields._VERSION, value), new ParseField(Fields._VERSION));
|
|
|
+ parser.declareLong((map, value) -> map.put(Fields._SEQ_NO, value), new ParseField(Fields._SEQ_NO));
|
|
|
+ parser.declareLong((map, value) -> map.put(Fields._PRIMARY_TERM, value), new ParseField(Fields._PRIMARY_TERM));
|
|
|
parser.declareField((map, value) -> map.put(Fields._SHARD, value), (p, c) -> ShardId.fromString(p.text()),
|
|
|
new ParseField(Fields._SHARD), ValueType.STRING);
|
|
|
parser.declareObject((map, value) -> map.put(SourceFieldMapper.NAME, value), (p, c) -> parseSourceBytes(p),
|
|
@@ -588,6 +626,8 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
}
|
|
|
searchHit.score(get(Fields._SCORE, values, DEFAULT_SCORE));
|
|
|
searchHit.version(get(Fields._VERSION, values, -1L));
|
|
|
+ searchHit.setSeqNo(get(Fields._SEQ_NO, values, SequenceNumbers.UNASSIGNED_SEQ_NO));
|
|
|
+ searchHit.setPrimaryTerm(get(Fields._PRIMARY_TERM, values, SequenceNumbers.UNASSIGNED_PRIMARY_TERM));
|
|
|
searchHit.sortValues(get(Fields.SORT, values, SearchSortValues.EMPTY));
|
|
|
searchHit.highlightFields(get(Fields.HIGHLIGHT, values, null));
|
|
|
searchHit.sourceRef(get(SourceFieldMapper.NAME, values, null));
|
|
@@ -744,6 +784,10 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
type = in.readOptionalText();
|
|
|
nestedIdentity = in.readOptionalWriteable(NestedIdentity::new);
|
|
|
version = in.readLong();
|
|
|
+ if (in.getVersion().onOrAfter(Version.V_7_0_0)) {
|
|
|
+ seqNo = in.readZLong();
|
|
|
+ primaryTerm = in.readVLong();
|
|
|
+ }
|
|
|
source = in.readBytesReference();
|
|
|
if (source.length() == 0) {
|
|
|
source = null;
|
|
@@ -812,6 +856,10 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
out.writeOptionalText(type);
|
|
|
out.writeOptionalWriteable(nestedIdentity);
|
|
|
out.writeLong(version);
|
|
|
+ if (out.getVersion().onOrAfter(Version.V_7_0_0)) {
|
|
|
+ out.writeZLong(seqNo);
|
|
|
+ out.writeVLong(primaryTerm);
|
|
|
+ }
|
|
|
out.writeBytesReference(source);
|
|
|
if (explanation == null) {
|
|
|
out.writeBoolean(false);
|
|
@@ -867,6 +915,8 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
&& Objects.equals(type, other.type)
|
|
|
&& Objects.equals(nestedIdentity, other.nestedIdentity)
|
|
|
&& Objects.equals(version, other.version)
|
|
|
+ && Objects.equals(seqNo, other.seqNo)
|
|
|
+ && Objects.equals(primaryTerm, other.primaryTerm)
|
|
|
&& Objects.equals(source, other.source)
|
|
|
&& Objects.equals(fields, other.fields)
|
|
|
&& Objects.equals(getHighlightFields(), other.getHighlightFields())
|
|
@@ -880,8 +930,8 @@ public final class SearchHit implements Streamable, ToXContentObject, Iterable<D
|
|
|
|
|
|
@Override
|
|
|
public int hashCode() {
|
|
|
- return Objects.hash(id, type, nestedIdentity, version, source, fields, getHighlightFields(), Arrays.hashCode(matchedQueries),
|
|
|
- explanation, shard, innerHits, index, clusterAlias);
|
|
|
+ return Objects.hash(id, type, nestedIdentity, version, seqNo, primaryTerm, source, fields, getHighlightFields(),
|
|
|
+ Arrays.hashCode(matchedQueries), explanation, shard, innerHits, index, clusterAlias);
|
|
|
}
|
|
|
|
|
|
/**
|