From ca9f8cc7a737a97d72463820842066322fb7350e Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Thu, 2 Nov 2017 15:59:20 +0000 Subject: [PATCH 1/2] Uses norms for exists query if enabled This change means that for indexes created from 6.1.0, if normas are enabled we will not write the field name to the `_field_names` field and for an exists query we will instead use the NormsFieldExistsQuery which was added in Lucene 7.1.0. If norms are not enabled or if the index was created before 6.1.0 `_field_names` will be used as before. --- .../elasticsearch/index/mapper/TextFieldMapper.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java b/core/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java index 24c3443658aa8..ae99f743fe57f 100644 --- a/core/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java +++ b/core/src/main/java/org/elasticsearch/index/mapper/TextFieldMapper.java @@ -23,7 +23,7 @@ import org.apache.lucene.index.IndexOptions; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.Term; -import org.apache.lucene.search.DocValuesFieldExistsQuery; +import org.apache.lucene.search.NormsFieldExistsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.elasticsearch.common.settings.Settings; @@ -280,10 +280,10 @@ public String typeName() { @Override public Query existsQuery(QueryShardContext context) { - if (hasDocValues()) { - return new DocValuesFieldExistsQuery(name()); - } else { + if (omitNorms()) { return new TermQuery(new Term(FieldNamesFieldMapper.NAME, name())); + } else { + return new NormsFieldExistsQuery(name()); } } @@ -345,7 +345,9 @@ protected void parseCreateField(ParseContext context, List field if (fieldType().indexOptions() != IndexOptions.NONE || fieldType().stored()) { Field field = new Field(fieldType().name(), value, fieldType()); fields.add(field); - createFieldNamesField(context, fields); + if (fieldType().omitNorms()) { + createFieldNamesField(context, fields); + } } } From 1715c09969220c397bd21894b8f9c1db6b9732c9 Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Thu, 2 Nov 2017 16:48:55 +0000 Subject: [PATCH 2/2] Fixes tests --- .../index/mapper/FieldNamesFieldMapperTests.java | 3 ++- .../elasticsearch/index/query/ExistsQueryBuilderTests.java | 5 +++++ .../elasticsearch/index/query/RangeQueryBuilderTests.java | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/core/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java index 70022dc632619..3655f04fcbba1 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/FieldNamesFieldMapperTests.java @@ -26,6 +26,7 @@ import org.elasticsearch.test.ESSingleNodeTestCase; import java.util.Arrays; +import java.util.Collections; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -87,7 +88,7 @@ public void testInjectIntoDocDuringParsing() throws Exception { .bytes(), XContentType.JSON)); - assertFieldNames(set("a"), doc); + assertFieldNames(Collections.emptySet(), doc); } public void testExplicitEnabled() throws Exception { diff --git a/core/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java index 40d3f90dd4efe..d4547eee26f89 100644 --- a/core/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java @@ -24,6 +24,7 @@ import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.MatchNoDocsQuery; +import org.apache.lucene.search.NormsFieldExistsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.elasticsearch.Version; @@ -113,6 +114,10 @@ protected void doAssertLuceneQuery(ExistsQueryBuilder queryBuilder, Query query, assertThat(constantScoreQuery.getQuery(), instanceOf(DocValuesFieldExistsQuery.class)); DocValuesFieldExistsQuery dvExistsQuery = (DocValuesFieldExistsQuery) constantScoreQuery.getQuery(); assertEquals(field, dvExistsQuery.getField()); + } else if (context.getQueryShardContext().getMapperService().fullName(field).omitNorms() == false) { + assertThat(constantScoreQuery.getQuery(), instanceOf(NormsFieldExistsQuery.class)); + NormsFieldExistsQuery normsExistsQuery = (NormsFieldExistsQuery) constantScoreQuery.getQuery(); + assertEquals(field, normsExistsQuery.getField()); } else { assertThat(constantScoreQuery.getQuery(), instanceOf(TermQuery.class)); TermQuery termQuery = (TermQuery) constantScoreQuery.getQuery(); diff --git a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index ad3946a675560..2230436b18ef4 100644 --- a/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -26,6 +26,7 @@ import org.apache.lucene.search.DocValuesFieldExistsQuery; import org.apache.lucene.search.IndexOrDocValuesQuery; import org.apache.lucene.search.MatchNoDocsQuery; +import org.apache.lucene.search.NormsFieldExistsQuery; import org.apache.lucene.search.PointRangeQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; @@ -129,6 +130,9 @@ protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, if (context.mapperService().getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) && context.mapperService().fullName(queryBuilder.fieldName()).hasDocValues()) { expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(queryBuilder.fieldName())); + } else if (context.mapperService().getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) + && context.mapperService().fullName(queryBuilder.fieldName()).omitNorms() == false) { + expectedQuery = new ConstantScoreQuery(new NormsFieldExistsQuery(queryBuilder.fieldName())); } else { expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, queryBuilder.fieldName()))); }