Fix context suggester to read values from keyword type field (#24200)

Closes #24129
This commit is contained in:
Masaru Hasegawa 2017-05-31 18:35:01 +09:00 committed by Jim Ferenczi
parent 258be2b135
commit a77b38cdd1
2 changed files with 21 additions and 12 deletions

View File

@ -25,6 +25,7 @@ import org.elasticsearch.Version;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentParser.Token; import org.elasticsearch.common.xcontent.XContentParser.Token;
import org.elasticsearch.index.mapper.KeywordFieldMapper;
import org.elasticsearch.index.mapper.ParseContext; import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.ParseContext.Document; import org.elasticsearch.index.mapper.ParseContext.Document;
import org.elasticsearch.index.query.QueryParseContext; import org.elasticsearch.index.query.QueryParseContext;
@ -136,10 +137,14 @@ public class CategoryContextMapping extends ContextMapping<CategoryQueryContext>
IndexableField[] fields = document.getFields(fieldName); IndexableField[] fields = document.getFields(fieldName);
values = new HashSet<>(fields.length); values = new HashSet<>(fields.length);
for (IndexableField field : fields) { for (IndexableField field : fields) {
values.add(field.stringValue()); if (field.fieldType() instanceof KeywordFieldMapper.KeywordFieldType) {
values.add(field.binaryValue().utf8ToString());
} else {
values.add(field.stringValue());
}
} }
} }
return (values == null) ? Collections.<CharSequence>emptySet() : values; return (values == null) ? Collections.emptySet() : values;
} }
@Override @Override

View File

@ -19,7 +19,6 @@
package org.elasticsearch.search.suggest; package org.elasticsearch.search.suggest;
import com.carrotsearch.randomizedtesting.generators.RandomStrings; import com.carrotsearch.randomizedtesting.generators.RandomStrings;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs; import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.index.IndexResponse;
@ -660,37 +659,42 @@ public class ContextCompletionSuggestSearchIT extends ESIntegTestCase {
.field("preserve_separators", completionMappingBuilder.preserveSeparators) .field("preserve_separators", completionMappingBuilder.preserveSeparators)
.field("preserve_position_increments", completionMappingBuilder.preservePositionIncrements); .field("preserve_position_increments", completionMappingBuilder.preservePositionIncrements);
List<String> categoryContextFields = new ArrayList<>();
if (completionMappingBuilder.contextMappings != null) { if (completionMappingBuilder.contextMappings != null) {
mapping = mapping.startArray("contexts"); mapping.startArray("contexts");
for (Map.Entry<String, ContextMapping> contextMapping : completionMappingBuilder.contextMappings.entrySet()) { for (Map.Entry<String, ContextMapping> contextMapping : completionMappingBuilder.contextMappings.entrySet()) {
mapping = mapping.startObject() mapping.startObject()
.field("name", contextMapping.getValue().name()) .field("name", contextMapping.getValue().name())
.field("type", contextMapping.getValue().type().name()); .field("type", contextMapping.getValue().type().name());
switch (contextMapping.getValue().type()) { switch (contextMapping.getValue().type()) {
case CATEGORY: case CATEGORY:
final String fieldName = ((CategoryContextMapping) contextMapping.getValue()).getFieldName(); final String fieldName = ((CategoryContextMapping) contextMapping.getValue()).getFieldName();
if (fieldName != null) { if (fieldName != null) {
mapping = mapping.field("path", fieldName); mapping.field("path", fieldName);
categoryContextFields.add(fieldName);
} }
break; break;
case GEO: case GEO:
final String name = ((GeoContextMapping) contextMapping.getValue()).getFieldName(); final String name = ((GeoContextMapping) contextMapping.getValue()).getFieldName();
mapping = mapping mapping.field("precision", ((GeoContextMapping) contextMapping.getValue()).getPrecision());
.field("precision", ((GeoContextMapping) contextMapping.getValue()).getPrecision());
if (name != null) { if (name != null) {
mapping.field("path", name); mapping.field("path", name);
} }
break; break;
} }
mapping = mapping.endObject(); mapping.endObject();
} }
mapping = mapping.endArray(); mapping.endArray();
} }
mapping = mapping.endObject() mapping.endObject();
.endObject().endObject() for (String fieldName : categoryContextFields) {
mapping.startObject(fieldName)
.field("type", randomBoolean() ? "keyword" : "text")
.endObject(); .endObject();
}
mapping.endObject().endObject().endObject();
assertAcked(client().admin().indices().prepareCreate(INDEX) assertAcked(client().admin().indices().prepareCreate(INDEX)
.setSettings(Settings.builder().put(indexSettings()).put(settings)) .setSettings(Settings.builder().put(indexSettings()).put(settings))