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.XContentParser;
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.Document;
import org.elasticsearch.index.query.QueryParseContext;
@ -136,10 +137,14 @@ public class CategoryContextMapping extends ContextMapping<CategoryQueryContext>
IndexableField[] fields = document.getFields(fieldName);
values = new HashSet<>(fields.length);
for (IndexableField field : fields) {
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

View File

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