|
| 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 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.index.query; |
| 10 | + |
| 11 | +import org.apache.lucene.search.MatchAllDocsQuery; |
| 12 | +import org.apache.lucene.search.Query; |
| 13 | +import org.elasticsearch.common.ParsingException; |
| 14 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 15 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 16 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 17 | +import org.elasticsearch.common.xcontent.ParseField; |
| 18 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 19 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 20 | +import org.elasticsearch.core.RestApiVersion; |
| 21 | +import org.elasticsearch.index.mapper.MapperService; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | + |
| 25 | +public class TypeQueryV7Builder extends AbstractQueryBuilder<TypeQueryV7Builder> { |
| 26 | + public static final String NAME = "type"; |
| 27 | + private static final ParseField VALUE_FIELD = new ParseField("value"); |
| 28 | + private static final ObjectParser<TypeQueryV7Builder, Void> PARSER = new ObjectParser<>(NAME, TypeQueryV7Builder::new); |
| 29 | + |
| 30 | + static { |
| 31 | + PARSER.declareString(QueryBuilder::queryName, |
| 32 | + AbstractQueryBuilder.NAME_FIELD.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7))); |
| 33 | + PARSER.declareFloat(QueryBuilder::boost, |
| 34 | + AbstractQueryBuilder.BOOST_FIELD.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7))); |
| 35 | + PARSER.declareString((queryBuilder, value) -> { |
| 36 | + }, |
| 37 | + VALUE_FIELD.forRestApiVersion(RestApiVersion.equalTo(RestApiVersion.V_7))); |
| 38 | + } |
| 39 | + |
| 40 | + public TypeQueryV7Builder() { |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Read from a stream. |
| 45 | + */ |
| 46 | + public TypeQueryV7Builder(StreamInput in) throws IOException { |
| 47 | + super(in); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void doWriteTo(StreamOutput out) throws IOException { |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void doXContent(XContentBuilder builder, Params params) throws IOException { |
| 56 | + builder.startObject(NAME); |
| 57 | + builder.field(VALUE_FIELD.getPreferredName(), MapperService.SINGLE_MAPPING_NAME); |
| 58 | + printBoostAndQueryName(builder); |
| 59 | + builder.endObject(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + protected Query doToQuery(SearchExecutionContext context) throws IOException { |
| 64 | + return new MatchAllDocsQuery(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected boolean doEquals(TypeQueryV7Builder other) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected int doHashCode() { |
| 74 | + return 0; |
| 75 | + } |
| 76 | + |
| 77 | + public static TypeQueryV7Builder fromXContent(XContentParser parser) throws IOException { |
| 78 | + try { |
| 79 | + return PARSER.apply(parser, null); |
| 80 | + } catch (IllegalArgumentException e) { |
| 81 | + throw new ParsingException(parser.getTokenLocation(), e.getMessage(), e); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String getWriteableName() { |
| 87 | + return NAME; |
| 88 | + } |
| 89 | +} |
0 commit comments