|
| 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.search; |
| 7 | + |
| 8 | +import org.apache.lucene.search.IndexSearcher; |
| 9 | +import org.apache.lucene.search.Query; |
| 10 | +import org.apache.lucene.search.ScoreMode; |
| 11 | +import org.apache.lucene.search.Weight; |
| 12 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 13 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 14 | +import org.elasticsearch.common.lucene.search.Queries; |
| 15 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 16 | +import org.elasticsearch.index.query.AbstractQueryBuilder; |
| 17 | +import org.elasticsearch.index.query.QueryShardContext; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | + |
| 21 | +class ThrowingQueryBuilder extends AbstractQueryBuilder<ThrowingQueryBuilder> { |
| 22 | + public static final String NAME = "throw"; |
| 23 | + |
| 24 | + private final long randomUID; |
| 25 | + private final RuntimeException failure; |
| 26 | + private final int shardId; |
| 27 | + |
| 28 | + /** |
| 29 | + * Creates a {@link ThrowingQueryBuilder} with the provided <code>randomUID</code>. |
| 30 | + */ |
| 31 | + ThrowingQueryBuilder(long randomUID, RuntimeException failure, int shardId) { |
| 32 | + super(); |
| 33 | + this.randomUID = randomUID; |
| 34 | + this.failure = failure; |
| 35 | + this.shardId = shardId; |
| 36 | + } |
| 37 | + |
| 38 | + ThrowingQueryBuilder(StreamInput in) throws IOException { |
| 39 | + super(in); |
| 40 | + this.randomUID = in.readLong(); |
| 41 | + this.failure = in.readException(); |
| 42 | + this.shardId = in.readVInt(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void doWriteTo(StreamOutput out) throws IOException { |
| 47 | + out.writeLong(randomUID); |
| 48 | + out.writeException(failure); |
| 49 | + out.writeVInt(shardId); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void doXContent(XContentBuilder builder, Params params) throws IOException { |
| 54 | + builder.startObject(NAME); |
| 55 | + builder.endObject(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected Query doToQuery(QueryShardContext context) { |
| 60 | + final Query delegate = Queries.newMatchAllQuery(); |
| 61 | + return new Query() { |
| 62 | + @Override |
| 63 | + public Weight createWeight(IndexSearcher searcher, ScoreMode scoreMode, float boost) throws IOException { |
| 64 | + if (context.getShardId() == shardId) { |
| 65 | + throw failure; |
| 66 | + } |
| 67 | + return delegate.createWeight(searcher, scoreMode, boost); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String toString(String field) { |
| 72 | + return delegate.toString(field); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean equals(Object obj) { |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public int hashCode() { |
| 82 | + return 0; |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected boolean doEquals(ThrowingQueryBuilder other) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected int doHashCode() { |
| 94 | + return 0; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String getWriteableName() { |
| 99 | + return NAME; |
| 100 | + } |
| 101 | +} |
0 commit comments