|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | +package org.elasticsearch.xpack.core.index.query; |
| 7 | + |
| 8 | +import org.apache.lucene.search.Query; |
| 9 | +import org.elasticsearch.common.ParseField; |
| 10 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 11 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 12 | +import org.elasticsearch.index.query.AbstractQueryBuilder; |
| 13 | +import org.elasticsearch.index.query.QueryBuilder; |
| 14 | +import org.elasticsearch.index.query.QueryShardContext; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +/** |
| 23 | + * A query that will promote selected documents (identified by ID) above matches produced by an "organic" query. In practice, some upstream |
| 24 | + * system will identify the promotions associated with a user's query string and use this object to ensure these are "pinned" to the top of |
| 25 | + * the other search results. |
| 26 | + */ |
| 27 | +public class PinnedQueryBuilder extends AbstractQueryBuilder<PinnedQueryBuilder> { |
| 28 | + public static final String NAME = "pinned"; |
| 29 | + protected final QueryBuilder organicQuery; |
| 30 | + protected final List<String> ids; |
| 31 | + protected static final ParseField IDS_FIELD = new ParseField("ids"); |
| 32 | + protected static final ParseField ORGANIC_QUERY_FIELD = new ParseField("organic"); |
| 33 | + |
| 34 | + @Override |
| 35 | + public String getWriteableName() { |
| 36 | + return NAME; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Creates a new PinnedQueryBuilder |
| 41 | + */ |
| 42 | + public PinnedQueryBuilder(QueryBuilder organicQuery, String... ids) { |
| 43 | + if (organicQuery == null) { |
| 44 | + throw new IllegalArgumentException("[" + NAME + "] organicQuery cannot be null"); |
| 45 | + } |
| 46 | + this.organicQuery = organicQuery; |
| 47 | + if (ids == null) { |
| 48 | + throw new IllegalArgumentException("[" + NAME + "] ids cannot be null"); |
| 49 | + } |
| 50 | + this.ids = new ArrayList<>(); |
| 51 | + Collections.addAll(this.ids, ids); |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + @Override |
| 57 | + protected void doWriteTo(StreamOutput out) throws IOException { |
| 58 | + out.writeStringCollection(this.ids); |
| 59 | + out.writeNamedWriteable(organicQuery); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @return the organic query set in the constructor |
| 64 | + */ |
| 65 | + public QueryBuilder organicQuery() { |
| 66 | + return this.organicQuery; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns the pinned ids for the query. |
| 71 | + */ |
| 72 | + public List<String> ids() { |
| 73 | + return Collections.unmodifiableList(this.ids); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void doXContent(XContentBuilder builder, Params params) throws IOException { |
| 79 | + builder.startObject(NAME); |
| 80 | + if (organicQuery != null) { |
| 81 | + builder.field(ORGANIC_QUERY_FIELD.getPreferredName()); |
| 82 | + organicQuery.toXContent(builder, params); |
| 83 | + } |
| 84 | + builder.startArray(IDS_FIELD.getPreferredName()); |
| 85 | + for (String value : ids) { |
| 86 | + builder.value(value); |
| 87 | + } |
| 88 | + builder.endArray(); |
| 89 | + printBoostAndQueryName(builder); |
| 90 | + builder.endObject(); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected Query doToQuery(QueryShardContext context) throws IOException { |
| 95 | + throw new UnsupportedOperationException("Client side-only class for use in HLRC"); |
| 96 | + } |
| 97 | + |
| 98 | + |
| 99 | + @Override |
| 100 | + protected int doHashCode() { |
| 101 | + return Objects.hash(ids, organicQuery); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + protected boolean doEquals(PinnedQueryBuilder other) { |
| 106 | + return Objects.equals(ids, other.ids) && Objects.equals(organicQuery, other.organicQuery) && boost == other.boost; |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments