Add tests for disabling positions and copy the check to text fields

This commit is contained in:
Ryan Ernst 2016-07-19 19:07:56 -07:00
parent c85cb37cc4
commit 0f2d7a84a8
4 changed files with 69 additions and 18 deletions

View File

@ -163,14 +163,6 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc
@Override @Override
public StringFieldMapper build(BuilderContext context) { public StringFieldMapper build(BuilderContext context) {
if (positionIncrementGap != POSITION_INCREMENT_GAP_USE_ANALYZER) {
if (fieldType.tokenized() == false && fieldType.indexOptions() != IndexOptions.NONE) {
throw new IllegalArgumentException("Cannot set position_increment_gap on not_analyzed field [" + fieldType.name() + "]");
}
fieldType.setIndexAnalyzer(new NamedAnalyzer(fieldType.indexAnalyzer(), positionIncrementGap));
fieldType.setSearchAnalyzer(new NamedAnalyzer(fieldType.searchAnalyzer(), positionIncrementGap));
fieldType.setSearchQuoteAnalyzer(new NamedAnalyzer(fieldType.searchQuoteAnalyzer(), positionIncrementGap));
}
// if the field is not analyzed, then by default, we should omit norms and have docs only // if the field is not analyzed, then by default, we should omit norms and have docs only
// index options, as probably what the user really wants // index options, as probably what the user really wants
// if they are set explicitly, we will use those values // if they are set explicitly, we will use those values
@ -186,6 +178,15 @@ public class StringFieldMapper extends FieldMapper implements AllFieldMapper.Inc
fieldType.setIndexOptions(IndexOptions.DOCS); fieldType.setIndexOptions(IndexOptions.DOCS);
} }
} }
if (positionIncrementGap != POSITION_INCREMENT_GAP_USE_ANALYZER) {
if (fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
throw new IllegalArgumentException("Cannot set position_increment_gap on field ["
+ name + "] without positions enabled");
}
fieldType.setIndexAnalyzer(new NamedAnalyzer(fieldType.indexAnalyzer(), positionIncrementGap));
fieldType.setSearchAnalyzer(new NamedAnalyzer(fieldType.searchAnalyzer(), positionIncrementGap));
fieldType.setSearchQuoteAnalyzer(new NamedAnalyzer(fieldType.searchQuoteAnalyzer(), positionIncrementGap));
}
setupFieldType(context); setupFieldType(context);
StringFieldMapper fieldMapper = new StringFieldMapper( StringFieldMapper fieldMapper = new StringFieldMapper(
name, fieldType(), defaultFieldType, positionIncrementGap, ignoreAbove, name, fieldType(), defaultFieldType, positionIncrementGap, ignoreAbove,

View File

@ -119,6 +119,10 @@ public class TextFieldMapper extends FieldMapper implements AllFieldMapper.Inclu
@Override @Override
public TextFieldMapper build(BuilderContext context) { public TextFieldMapper build(BuilderContext context) {
if (positionIncrementGap != POSITION_INCREMENT_GAP_USE_ANALYZER) { if (positionIncrementGap != POSITION_INCREMENT_GAP_USE_ANALYZER) {
if (fieldType.indexOptions().compareTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS) < 0) {
throw new IllegalArgumentException("Cannot set position_increment_gap on field ["
+ name + "] without positions enabled");
}
fieldType.setIndexAnalyzer(new NamedAnalyzer(fieldType.indexAnalyzer(), positionIncrementGap)); fieldType.setIndexAnalyzer(new NamedAnalyzer(fieldType.indexAnalyzer(), positionIncrementGap));
fieldType.setSearchAnalyzer(new NamedAnalyzer(fieldType.searchAnalyzer(), positionIncrementGap)); fieldType.setSearchAnalyzer(new NamedAnalyzer(fieldType.searchAnalyzer(), positionIncrementGap));
fieldType.setSearchQuoteAnalyzer(new NamedAnalyzer(fieldType.searchQuoteAnalyzer(), positionIncrementGap)); fieldType.setSearchQuoteAnalyzer(new NamedAnalyzer(fieldType.searchQuoteAnalyzer(), positionIncrementGap));

View File

@ -44,6 +44,7 @@ import org.elasticsearch.test.ESSingleNodeTestCase;
import org.junit.Before; import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -474,4 +475,32 @@ public class TextFieldMapperTests extends ESSingleNodeTestCase {
Exception e = expectThrows(MapperParsingException.class, () -> parser.parse("type", new CompressedXContent(mapping))); Exception e = expectThrows(MapperParsingException.class, () -> parser.parse("type", new CompressedXContent(mapping)));
assertEquals("[analyzer] must not have a [null] value", e.getMessage()); assertEquals("[analyzer] must not have a [null] value", e.getMessage());
} }
public void testNotIndexedFieldPositionIncrement() throws IOException {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field")
.field("type", "text")
.field("index", false)
.field("position_increment_gap", 10)
.endObject().endObject().endObject().endObject().string();
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> parser.parse("type", new CompressedXContent(mapping)));
assertEquals("Cannot set position_increment_gap on field [field] without positions enabled", e.getMessage());
}
public void testAnalyzedFieldPositionIncrementWithoutPositions() throws IOException {
for (String indexOptions : Arrays.asList("docs", "freqs")) {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field")
.field("type", "text")
.field("index_options", indexOptions)
.field("position_increment_gap", 10)
.endObject().endObject().endObject().endObject().string();
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> parser.parse("type", new CompressedXContent(mapping)));
assertEquals("Cannot set position_increment_gap on field [field] without positions enabled", e.getMessage());
}
}
} }

View File

@ -678,16 +678,33 @@ public class SimpleStringMappingTests extends ESSingleNodeTestCase {
assertThat(e.getMessage(), containsString("Fielddata is disabled")); assertThat(e.getMessage(), containsString("Fielddata is disabled"));
} }
public void testKeywordFieldPositionIncrement() throws IOException { public void testNonAnalyzedFieldPositionIncrement() throws IOException {
for (String index : Arrays.asList("no", "not_analyzed")) {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field") .startObject("properties").startObject("field")
.field("type", "string") .field("type", "string")
.field("index", "not_analyzed") .field("index", index)
.field("position_increment_gap", 10) .field("position_increment_gap", 10)
.endObject().endObject().endObject().endObject().string(); .endObject().endObject().endObject().endObject().string();
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> parser.parse("type", new CompressedXContent(mapping))); () -> parser.parse("type", new CompressedXContent(mapping)));
assertThat(e.getMessage(), containsString("Cannot set position_increment_gap on not_analyzed field")); assertEquals("Cannot set position_increment_gap on field [field] without positions enabled", e.getMessage());
}
}
public void testAnalyzedFieldPositionIncrementWithoutPositions() throws IOException {
for (String indexOptions : Arrays.asList("docs", "freqs")) {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
.startObject("properties").startObject("field")
.field("type", "string")
.field("index_options", indexOptions)
.field("position_increment_gap", 10)
.endObject().endObject().endObject().endObject().string();
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
() -> parser.parse("type", new CompressedXContent(mapping)));
assertEquals("Cannot set position_increment_gap on field [field] without positions enabled", e.getMessage());
}
} }
} }