|
| 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", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.index.query; |
| 11 | + |
| 12 | +import org.apache.lucene.index.Term; |
| 13 | +import org.apache.lucene.search.AutomatonQuery; |
| 14 | +import org.apache.lucene.search.Query; |
| 15 | +import org.apache.lucene.util.automaton.Automaton; |
| 16 | +import org.elasticsearch.TransportVersion; |
| 17 | +import org.elasticsearch.common.Strings; |
| 18 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 19 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.UnsupportedEncodingException; |
| 23 | +import java.util.Objects; |
| 24 | + |
| 25 | +/** |
| 26 | + * Implements an Automaton query, which matches documents based on a Lucene Automaton. |
| 27 | + * It does not support serialization or XContent representation. |
| 28 | + */ |
| 29 | +public class AutomatonQueryBuilder extends AbstractQueryBuilder<AutomatonQueryBuilder> implements MultiTermQueryBuilder { |
| 30 | + private final String fieldName; |
| 31 | + private final Automaton automaton; |
| 32 | + private final String description; |
| 33 | + |
| 34 | + public AutomatonQueryBuilder(String fieldName, Automaton automaton, String description) { |
| 35 | + if (Strings.isEmpty(fieldName)) { |
| 36 | + throw new IllegalArgumentException("field name is null or empty"); |
| 37 | + } |
| 38 | + if (automaton == null) { |
| 39 | + throw new IllegalArgumentException("automaton cannot be null"); |
| 40 | + } |
| 41 | + this.fieldName = fieldName; |
| 42 | + this.automaton = automaton; |
| 43 | + this.description = description; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public String fieldName() { |
| 48 | + return fieldName; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public String getWriteableName() { |
| 53 | + throw new UnsupportedOperationException("AutomatonQueryBuilder does not support getWriteableName"); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getName() { |
| 58 | + return "AutomatonQueryBuilder"; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void doWriteTo(StreamOutput out) throws IOException { |
| 63 | + throw new UnsupportedEncodingException("AutomatonQueryBuilder does not support doWriteTo"); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void doXContent(XContentBuilder builder, Params params) throws IOException { |
| 68 | + throw new UnsupportedEncodingException("AutomatonQueryBuilder does not support doXContent"); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected Query doToQuery(SearchExecutionContext context) throws IOException { |
| 73 | + return new AutomatonQueryWithDescription(new Term(fieldName), automaton, description); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected int doHashCode() { |
| 78 | + return Objects.hash(fieldName, automaton, description); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected boolean doEquals(AutomatonQueryBuilder other) { |
| 83 | + return Objects.equals(fieldName, other.fieldName) |
| 84 | + && Objects.equals(automaton, other.automaton) |
| 85 | + && Objects.equals(description, other.description); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public TransportVersion getMinimalSupportedVersion() { |
| 90 | + throw new UnsupportedOperationException("AutomatonQueryBuilder does not support getMinimalSupportedVersion"); |
| 91 | + } |
| 92 | +} |
0 commit comments