mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-20 03:45:02 +00:00
Merge pull request #19510 from rjernst/mapper_keyword_positions
Mappings: Fix not_analyzed string fields to error when position_increment_gap is set
This commit is contained in:
commit
48fe4326b0
@ -163,11 +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) {
|
|
||||||
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
|
||||||
@ -183,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,
|
||||||
|
@ -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));
|
||||||
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -677,4 +677,34 @@ public class SimpleStringMappingTests extends ESSingleNodeTestCase {
|
|||||||
() -> mapper.mappers().getMapper("field").fieldType().fielddataBuilder());
|
() -> mapper.mappers().getMapper("field").fieldType().fielddataBuilder());
|
||||||
assertThat(e.getMessage(), containsString("Fielddata is disabled"));
|
assertThat(e.getMessage(), containsString("Fielddata is disabled"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testNonAnalyzedFieldPositionIncrement() throws IOException {
|
||||||
|
for (String index : Arrays.asList("no", "not_analyzed")) {
|
||||||
|
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||||
|
.startObject("properties").startObject("field")
|
||||||
|
.field("type", "string")
|
||||||
|
.field("index", index)
|
||||||
|
.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", "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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user