forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize how queries on
_index
are handled at rewrite time (elast…
…ic#52486) Since this change refactors rewrites, I also took it as an opportunity to adrress elastic#49254: instead of returning the same queries you would get on a keyword field when a field is unmapped, queries get rewritten to a MatchNoDocsQueryBuilder. This change exposed a couple bugs, like the fact that the percolator doesn't rewrite queries at query time, or that the significant_terms aggregation doesn't rewrite its inner filter, which I fixed. Closes elastic#49254
- Loading branch information
Showing
37 changed files
with
622 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
server/src/main/java/org/elasticsearch/index/mapper/ConstantFieldType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.index.mapper; | ||
|
||
import org.apache.lucene.search.MatchAllDocsQuery; | ||
import org.apache.lucene.search.MatchNoDocsQuery; | ||
import org.apache.lucene.search.MultiTermQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.common.Nullable; | ||
import org.elasticsearch.common.lucene.search.Queries; | ||
import org.elasticsearch.common.regex.Regex; | ||
import org.elasticsearch.index.query.QueryShardContext; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* A {@link MappedFieldType} that has the same value for all documents. | ||
* Factory methods for queries are called at rewrite time so they should be | ||
* cheap. In particular they should not read data from disk or perform a | ||
* network call. Furthermore they may only return a {@link MatchAllDocsQuery} | ||
* or a {@link MatchNoDocsQuery}. | ||
*/ | ||
public abstract class ConstantFieldType extends MappedFieldType { | ||
|
||
public ConstantFieldType() { | ||
super(); | ||
} | ||
|
||
public ConstantFieldType(ConstantFieldType other) { | ||
super(other); | ||
} | ||
|
||
@Override | ||
public final boolean isSearchable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public final boolean isAggregatable() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public final Query existsQuery(QueryShardContext context) { | ||
return new MatchAllDocsQuery(); | ||
} | ||
|
||
/** | ||
* Return whether the constant value of this field matches the provided {@code pattern} | ||
* as documented in {@link Regex#simpleMatch}. | ||
*/ | ||
protected abstract boolean matches(String pattern, QueryShardContext context); | ||
|
||
private static String valueToString(Object value) { | ||
return value instanceof BytesRef | ||
? ((BytesRef) value).utf8ToString() | ||
: value.toString(); | ||
} | ||
|
||
@Override | ||
public final Query termQuery(Object value, QueryShardContext context) { | ||
String pattern = valueToString(value); | ||
if (matches(pattern, context)) { | ||
return Queries.newMatchAllQuery(); | ||
} else { | ||
return new MatchNoDocsQuery(); | ||
} | ||
} | ||
|
||
@Override | ||
public final Query termsQuery(List<?> values, QueryShardContext context) { | ||
for (Object value : values) { | ||
String pattern = valueToString(value); | ||
if (matches(pattern, context)) { | ||
// `terms` queries are a disjunction, so one matching term is enough | ||
return Queries.newMatchAllQuery(); | ||
} | ||
} | ||
return new MatchNoDocsQuery(); | ||
} | ||
|
||
@Override | ||
public final Query prefixQuery(String prefix, | ||
@Nullable MultiTermQuery.RewriteMethod method, | ||
QueryShardContext context) { | ||
String pattern = prefix + "*"; | ||
if (matches(pattern, context)) { | ||
return Queries.newMatchAllQuery(); | ||
} else { | ||
return new MatchNoDocsQuery(); | ||
} | ||
} | ||
|
||
@Override | ||
public final Query wildcardQuery(String value, | ||
@Nullable MultiTermQuery.RewriteMethod method, | ||
QueryShardContext context) { | ||
if (matches(value, context)) { | ||
return Queries.newMatchAllQuery(); | ||
} else { | ||
return new MatchNoDocsQuery(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.