-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Generalize how queries on _index
are handled at rewrite time
#52486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 34 commits
db1a079
c253ce7
abf27f1
002fe96
c6738d8
b7b894e
f5f288e
a86ad8a
a5949d6
c9c43aa
6b907c2
779d128
51dc118
8da5b6c
af4a55a
d1b71f4
f8ddbf7
d346303
c378cdf
59fd6b0
4592af2
ab70f10
0b29a43
936f883
44a7170
7639d34
fba9ca3
a284be9
f9a23e2
b312407
437315d
3f94930
7cd5c6d
51f7b3c
dc0bf44
15af22f
2b00751
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* 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.Query; | ||
import org.elasticsearch.index.query.QueryShardContext; | ||
|
||
/** | ||
* 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(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
package org.elasticsearch.index.query; | ||
|
||
import org.apache.lucene.search.BoostQuery; | ||
import org.apache.lucene.search.MatchNoDocsQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.apache.lucene.search.spans.SpanBoostQuery; | ||
import org.apache.lucene.search.spans.SpanQuery; | ||
|
@@ -103,7 +104,7 @@ public final Query toQuery(QueryShardContext context) throws IOException { | |
if (boost != DEFAULT_BOOST) { | ||
if (query instanceof SpanQuery) { | ||
query = new SpanBoostQuery((SpanQuery) query, boost); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For my knowledge, I'm wondering why this change (and the ones to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made this change as a way to keep tests simple. For instance here is what
This test only worked because most queries on unmapped fields would create the same query as on a
But this fails if the inner query is wrapped in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the clear explanation! |
||
} else if (query instanceof MatchNoDocsQuery == false) { | ||
query = new BoostQuery(query, boost); | ||
} | ||
} | ||
|
@@ -232,7 +233,7 @@ static Collection<Query> toQueries(Collection<QueryBuilder> queryBuilders, Query | |
IOException { | ||
List<Query> queries = new ArrayList<>(queryBuilders.size()); | ||
for (QueryBuilder queryBuilder : queryBuilders) { | ||
Query query = queryBuilder.toQuery(context); | ||
Query query = queryBuilder.rewrite(context).toQuery(context); | ||
if (query != null) { | ||
queries.add(query); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,15 +19,13 @@ | |
|
||
package org.elasticsearch.index.query; | ||
|
||
import org.apache.lucene.search.MatchNoDocsQuery; | ||
import org.apache.lucene.search.Query; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.ParsingException; | ||
import org.elasticsearch.common.Strings; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.lucene.search.Queries; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
|
@@ -136,17 +134,28 @@ public String getWriteableName() { | |
return NAME; | ||
} | ||
|
||
@Override | ||
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException { | ||
if (ids.isEmpty()) { | ||
return new MatchNoneQueryBuilder(); | ||
} | ||
QueryShardContext context = queryRewriteContext.convertToShardContext(); | ||
if (context != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small comment, could just be |
||
if (context.fieldMapper(IdFieldMapper.NAME) == null) { | ||
// no mappings yet | ||
return new MatchNoneQueryBuilder(); | ||
} | ||
} | ||
return super.doRewrite(queryRewriteContext); | ||
} | ||
|
||
@Override | ||
protected Query doToQuery(QueryShardContext context) throws IOException { | ||
MappedFieldType idField = context.fieldMapper(IdFieldMapper.NAME); | ||
if (idField == null) { | ||
return new MatchNoDocsQuery("No mappings"); | ||
} | ||
if (this.ids.isEmpty()) { | ||
return Queries.newMatchNoDocsQuery("Missing ids in \"" + this.getName() + "\" query."); | ||
} else { | ||
return idField.termsQuery(new ArrayList<>(ids), context); | ||
if (idField == null || ids.isEmpty()) { | ||
throw new IllegalStateException("Rewrite first"); | ||
} | ||
return idField.termsQuery(new ArrayList<>(ids), context); | ||
} | ||
|
||
@Override | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks unused, I think it can just be deleted?