Migrate TermsQuery to TermInSetQuery (#23229)

This commit is contained in:
Martin Scholz 2017-02-21 08:49:43 +01:00 committed by Adrien Grand
parent 64bcb74f4b
commit 3e292d5245
13 changed files with 46 additions and 35 deletions

View File

@ -23,7 +23,7 @@ import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.MultiTermQuery;
@ -114,12 +114,12 @@ public class IdFieldMapper extends MetadataFieldMapper {
@Override @Override
public Query termQuery(Object value, @Nullable QueryShardContext context) { public Query termQuery(Object value, @Nullable QueryShardContext context) {
final BytesRef[] uids = Uid.createUidsForTypesAndId(context.queryTypes(), value); final BytesRef[] uids = Uid.createUidsForTypesAndId(context.queryTypes(), value);
return new TermsQuery(UidFieldMapper.NAME, uids); return new TermInSetQuery(UidFieldMapper.NAME, uids);
} }
@Override @Override
public Query termsQuery(List values, @Nullable QueryShardContext context) { public Query termsQuery(List values, @Nullable QueryShardContext context) {
return new TermsQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(context.queryTypes(), values)); return new TermInSetQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(context.queryTypes(), values));
} }
} }

View File

@ -22,7 +22,7 @@ package org.elasticsearch.index.mapper;
import java.util.List; import java.util.List;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.MultiTermQuery; import org.apache.lucene.search.MultiTermQuery;
import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.PrefixQuery;
@ -53,7 +53,7 @@ public abstract class StringFieldType extends TermBasedFieldType {
for (int i = 0; i < bytesRefs.length; i++) { for (int i = 0; i < bytesRefs.length; i++) {
bytesRefs[i] = indexedValueForSearch(values.get(i)); bytesRefs[i] = indexedValueForSearch(values.get(i));
} }
return new TermsQuery(name(), bytesRefs); return new TermInSetQuery(name(), bytesRefs);
} }
@Override @Override

View File

@ -22,9 +22,9 @@ package org.elasticsearch.index.mapper;
import java.util.List; import java.util.List;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsQuery;
import org.apache.lucene.search.BoostQuery; import org.apache.lucene.search.BoostQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version; import org.elasticsearch.Version;
@ -66,7 +66,7 @@ abstract class TermBasedFieldType extends MappedFieldType {
for (int i = 0; i < bytesRefs.length; i++) { for (int i = 0; i < bytesRefs.length; i++) {
bytesRefs[i] = indexedValueForSearch(values.get(i)); bytesRefs[i] = indexedValueForSearch(values.get(i));
} }
return new TermsQuery(name(), bytesRefs); return new TermInSetQuery(name(), bytesRefs);
} }
} }

View File

@ -26,13 +26,13 @@ import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermContext; import org.apache.lucene.index.TermContext;
import org.apache.lucene.queries.TermsQuery;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.Lucene;
@ -172,7 +172,7 @@ public class TypeFieldMapper extends MetadataFieldMapper {
* Specialization for a disjunction over many _type * Specialization for a disjunction over many _type
*/ */
public static class TypesQuery extends Query { public static class TypesQuery extends Query {
// Same threshold as TermsQuery // Same threshold as TermInSetQuery
private static final int BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD = 16; private static final int BOOLEAN_REWRITE_TERM_COUNT_THRESHOLD = 16;
private final BytesRef[] types; private final BytesRef[] types;
@ -220,7 +220,7 @@ public class TypeFieldMapper extends MetadataFieldMapper {
} }
return new ConstantScoreQuery(bq.build()); return new ConstantScoreQuery(bq.build());
} }
return new TermsQuery(CONTENT_TYPE, types); return new TermInSetQuery(CONTENT_TYPE, types);
} }
@Override @Override

View File

@ -19,8 +19,8 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.apache.lucene.queries.TermsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermInSetQuery;
import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.common.ParseField; import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.ParsingException;
@ -175,7 +175,7 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> {
Collections.addAll(typesForQuery, types); Collections.addAll(typesForQuery, types);
} }
query = new TermsQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(typesForQuery, ids)); query = new TermInSetQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(typesForQuery, ids));
} }
return query; return query;
} }

View File

@ -21,10 +21,10 @@ package org.elasticsearch.index.query;
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.index.Fields; import org.apache.lucene.index.Fields;
import org.apache.lucene.queries.TermsQuery;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.ExceptionsHelper; import org.elasticsearch.ExceptionsHelper;
@ -1165,7 +1165,7 @@ public class MoreLikeThisQueryBuilder extends AbstractQueryBuilder<MoreLikeThisQ
uids.add(createUidAsBytes(item.type(), item.id())); uids.add(createUidAsBytes(item.type(), item.id()));
} }
if (!uids.isEmpty()) { if (!uids.isEmpty()) {
TermsQuery query = new TermsQuery(UidFieldMapper.NAME, uids.toArray(new BytesRef[uids.size()])); TermInSetQuery query = new TermInSetQuery(UidFieldMapper.NAME, uids.toArray(new BytesRef[uids.size()]));
boolQuery.add(query, BooleanClause.Occur.MUST_NOT); boolQuery.add(query, BooleanClause.Occur.MUST_NOT);
} }
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder; import org.apache.lucene.util.BytesRefBuilder;
@ -253,7 +253,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
if (o instanceof BytesRef) { if (o instanceof BytesRef) {
b = (BytesRef) o; b = (BytesRef) o;
} else { } else {
builder.copyChars(o.toString()); builder.copyChars(o.toString());
b = builder.get(); b = builder.get();
} }
bytesOut.writeBytes(b.bytes, b.offset, b.length); bytesOut.writeBytes(b.bytes, b.offset, b.length);
@ -410,7 +410,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
for (int i = 0; i < filterValues.length; i++) { for (int i = 0; i < filterValues.length; i++) {
filterValues[i] = BytesRefs.toBytesRef(values.get(i)); filterValues[i] = BytesRefs.toBytesRef(values.get(i));
} }
return new TermsQuery(fieldName, filterValues); return new TermInSetQuery(fieldName, filterValues);
} }
} }

View File

@ -31,7 +31,7 @@ import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.NoMergePolicy; import org.apache.lucene.index.NoMergePolicy;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
@ -136,19 +136,19 @@ public class FreqTermsEnumTests extends ESTestCase {
// now go over each doc, build the relevant references and filter // now go over each doc, build the relevant references and filter
reader = DirectoryReader.open(iw); reader = DirectoryReader.open(iw);
List<Term> filterTerms = new ArrayList<>(); List<BytesRef> filterTerms = new ArrayList<>();
for (int docId = 0; docId < reader.maxDoc(); docId++) { for (int docId = 0; docId < reader.maxDoc(); docId++) {
Document doc = reader.document(docId); Document doc = reader.document(docId);
addFreqs(doc, referenceAll); addFreqs(doc, referenceAll);
if (!deletedIds.contains(doc.getField("id").stringValue())) { if (!deletedIds.contains(doc.getField("id").stringValue())) {
addFreqs(doc, referenceNotDeleted); addFreqs(doc, referenceNotDeleted);
if (randomBoolean()) { if (randomBoolean()) {
filterTerms.add(new Term("id", doc.getField("id").stringValue())); filterTerms.add(new BytesRef(doc.getField("id").stringValue()));
addFreqs(doc, referenceFilter); addFreqs(doc, referenceFilter);
} }
} }
} }
filter = new TermsQuery(filterTerms); filter = new TermInSetQuery("id",filterTerms);
} }
private void addFreqs(Document doc, Map<String, FreqHolder> reference) { private void addFreqs(Document doc, Map<String, FreqHolder> reference) {

View File

@ -28,10 +28,11 @@ import org.apache.lucene.analysis.Tokenizer;
import org.apache.lucene.analysis.core.WhitespaceTokenizer; import org.apache.lucene.analysis.core.WhitespaceTokenizer;
import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.search.RegexpQuery;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.Lucene;
import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.AnalyzerScope;
@ -41,7 +42,9 @@ import org.elasticsearch.index.mapper.MappedFieldType.Relation;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
public class KeywordFieldTypeTests extends FieldTypeTestCase { public class KeywordFieldTypeTests extends FieldTypeTestCase {
@ -110,7 +113,10 @@ public class KeywordFieldTypeTests extends FieldTypeTestCase {
MappedFieldType ft = createDefaultFieldType(); MappedFieldType ft = createDefaultFieldType();
ft.setName("field"); ft.setName("field");
ft.setIndexOptions(IndexOptions.DOCS); ft.setIndexOptions(IndexOptions.DOCS);
assertEquals(new TermsQuery(new Term("field", "foo"), new Term("field", "bar")), List<BytesRef> terms = new ArrayList<>();
terms.add(new BytesRef("foo"));
terms.add(new BytesRef("bar"));
assertEquals(new TermInSetQuery("field", terms),
ft.termsQuery(Arrays.asList("foo", "bar"), null)); ft.termsQuery(Arrays.asList("foo", "bar"), null));
ft.setIndexOptions(IndexOptions.NONE); ft.setIndexOptions(IndexOptions.NONE);

View File

@ -18,15 +18,18 @@
*/ */
package org.elasticsearch.index.mapper; package org.elasticsearch.index.mapper;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import org.apache.lucene.document.LongPoint; import org.apache.lucene.document.LongPoint;
import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.FuzzyQuery; import org.apache.lucene.search.FuzzyQuery;
import org.apache.lucene.search.RegexpQuery; import org.apache.lucene.search.RegexpQuery;
import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.TextFieldMapper; import org.elasticsearch.index.mapper.TextFieldMapper;
@ -86,7 +89,10 @@ public class TextFieldTypeTests extends FieldTypeTestCase {
MappedFieldType ft = createDefaultFieldType(); MappedFieldType ft = createDefaultFieldType();
ft.setName("field"); ft.setName("field");
ft.setIndexOptions(IndexOptions.DOCS); ft.setIndexOptions(IndexOptions.DOCS);
assertEquals(new TermsQuery(new Term("field", "foo"), new Term("field", "bar")), List<BytesRef> terms = new ArrayList<>();
terms.add(new BytesRef("foo"));
terms.add(new BytesRef("bar"));
assertEquals(new TermInSetQuery("field", terms),
ft.termsQuery(Arrays.asList("foo", "bar"), null)); ft.termsQuery(Arrays.asList("foo", "bar"), null));
ft.setIndexOptions(IndexOptions.NONE); ft.setIndexOptions(IndexOptions.NONE);

View File

@ -20,7 +20,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import com.carrotsearch.randomizedtesting.generators.RandomPicks; import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.ConstantScoreQuery;
@ -240,10 +240,9 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
assertThat(booleanQuery.clauses().size(), equalTo(2)); assertThat(booleanQuery.clauses().size(), equalTo(2));
//check the inner ids query, we have to call rewrite to get to check the type it's executed against //check the inner ids query, we have to call rewrite to get to check the type it's executed against
assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST)); assertThat(booleanQuery.clauses().get(0).getOccur(), equalTo(BooleanClause.Occur.MUST));
assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(TermsQuery.class)); assertThat(booleanQuery.clauses().get(0).getQuery(), instanceOf(TermInSetQuery.class));
TermsQuery termsQuery = (TermsQuery) booleanQuery.clauses().get(0).getQuery(); TermInSetQuery termsQuery = (TermInSetQuery) booleanQuery.clauses().get(0).getQuery();
// we need to rewrite once for TermsQuery -> TermInSetQuery and than againt TermInSetQuery -> ConstantScoreQuery Query rewrittenTermsQuery = termsQuery.rewrite(null);
Query rewrittenTermsQuery = termsQuery.rewrite(null).rewrite(null);
assertThat(rewrittenTermsQuery, instanceOf(ConstantScoreQuery.class)); assertThat(rewrittenTermsQuery, instanceOf(ConstantScoreQuery.class));
ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) rewrittenTermsQuery; ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) rewrittenTermsQuery;
assertThat(constantScoreQuery.getQuery(), instanceOf(BooleanQuery.class)); assertThat(constantScoreQuery.getQuery(), instanceOf(BooleanQuery.class));

View File

@ -20,7 +20,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.cluster.metadata.MetaData;
@ -76,7 +76,7 @@ public class IdsQueryBuilderTests extends AbstractQueryTestCase<IdsQueryBuilder>
if (queryBuilder.ids().size() == 0) { if (queryBuilder.ids().size() == 0) {
assertThat(query, instanceOf(MatchNoDocsQuery.class)); assertThat(query, instanceOf(MatchNoDocsQuery.class));
} else { } else {
assertThat(query, instanceOf(TermsQuery.class)); assertThat(query, instanceOf(TermInSetQuery.class));
} }
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.index.query; package org.elasticsearch.index.query;
import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.TermInSetQuery;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.ConstantScoreQuery;
import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.MatchNoDocsQuery;
@ -110,7 +110,7 @@ public class TermsQueryBuilderTests extends AbstractQueryTestCase<TermsQueryBuil
MatchNoDocsQuery matchNoDocsQuery = (MatchNoDocsQuery) query; MatchNoDocsQuery matchNoDocsQuery = (MatchNoDocsQuery) query;
assertThat(matchNoDocsQuery.toString(), containsString("No terms supplied for \"terms\" query.")); assertThat(matchNoDocsQuery.toString(), containsString("No terms supplied for \"terms\" query."));
} else { } else {
assertThat(query, either(instanceOf(TermsQuery.class)) assertThat(query, either(instanceOf(TermInSetQuery.class))
.or(instanceOf(PointInSetQuery.class)) .or(instanceOf(PointInSetQuery.class))
.or(instanceOf(ConstantScoreQuery.class))); .or(instanceOf(ConstantScoreQuery.class)));
if (query instanceof ConstantScoreQuery) { if (query instanceof ConstantScoreQuery) {
@ -131,7 +131,7 @@ public class TermsQueryBuilderTests extends AbstractQueryTestCase<TermsQueryBuil
terms = queryBuilder.values(); terms = queryBuilder.values();
} }
TermsQuery expected = new TermsQuery(queryBuilder.fieldName(), TermInSetQuery expected = new TermInSetQuery(queryBuilder.fieldName(),
terms.stream().filter(Objects::nonNull).map(Object::toString).map(BytesRef::new).collect(Collectors.toList())); terms.stream().filter(Objects::nonNull).map(Object::toString).map(BytesRef::new).collect(Collectors.toList()));
assertEquals(expected, query); assertEquals(expected, query);
} }