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

org.elasticsearch.index.rankeval.RatedDocument Maven / Gradle / Ivy

There is a newer version: 7.17.25
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.index.rankeval;

import org.elasticsearch.common.Strings;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Objects;

/**
 * Represents a document (specified by its _index/_id) and its corresponding rating
 * with respect to a specific search query.
 * 

* The json structure of this element in a request: *

 * {
 *   "_index": "my_index",
 *   "_id": "doc1",
 *   "rating": 0
 * }
 * 
* */ public class RatedDocument implements Writeable, ToXContentObject { static final ParseField RATING_FIELD = new ParseField("rating"); static final ParseField DOC_ID_FIELD = new ParseField("_id"); static final ParseField INDEX_FIELD = new ParseField("_index"); private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>( "rated_document", a -> new RatedDocument((String) a[0], (String) a[1], (Integer) a[2]) ); static { PARSER.declareString(ConstructingObjectParser.constructorArg(), INDEX_FIELD); PARSER.declareString(ConstructingObjectParser.constructorArg(), DOC_ID_FIELD); PARSER.declareInt(ConstructingObjectParser.constructorArg(), RATING_FIELD); } private final int rating; private final DocumentKey key; public RatedDocument(String index, String id, int rating) { this.key = new DocumentKey(index, id); this.rating = rating; } RatedDocument(StreamInput in) throws IOException { this.key = new DocumentKey(in.readString(), in.readString()); this.rating = in.readVInt(); } public DocumentKey getKey() { return this.key; } public String getIndex() { return key.getIndex(); } public String getDocID() { return key.getDocId(); } public int getRating() { return rating; } @Override public void writeTo(StreamOutput out) throws IOException { out.writeString(key.getIndex()); out.writeString(key.getDocId()); out.writeVInt(rating); } static RatedDocument fromXContent(XContentParser parser) { return PARSER.apply(parser, null); } @Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { builder.startObject(); builder.field(INDEX_FIELD.getPreferredName(), key.getIndex()); builder.field(DOC_ID_FIELD.getPreferredName(), key.getDocId()); builder.field(RATING_FIELD.getPreferredName(), rating); builder.endObject(); return builder; } @Override public String toString() { return Strings.toString(this); } @Override public final boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } RatedDocument other = (RatedDocument) obj; return Objects.equals(key, other.key) && Objects.equals(rating, other.rating); } @Override public final int hashCode() { return Objects.hash(key, rating); } /** * a joint document key consisting of the documents index and id */ static class DocumentKey { private final String docId; private final String index; DocumentKey(String index, String docId) { if (Strings.isNullOrEmpty(index)) { throw new IllegalArgumentException("Index must be set for each rated document"); } if (Strings.isNullOrEmpty(docId)) { throw new IllegalArgumentException("DocId must be set for each rated document"); } this.index = index; this.docId = docId; } String getIndex() { return index; } String getDocId() { return docId; } @Override public final boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || getClass() != obj.getClass()) { return false; } DocumentKey other = (DocumentKey) obj; return Objects.equals(index, other.index) && Objects.equals(docId, other.docId); } @Override public final int hashCode() { return Objects.hash(index, docId); } @Override public String toString() { return "{\"_index\":\"" + index + "\",\"_id\":\"" + docId + "\"}"; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy