use of org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder in project elasticsearch by elastic.
the class ContextCompletionSuggestSearchIT method testContextRegex.
public void testContextRegex() throws Exception {
LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
map.put("cat", ContextBuilder.category("cat").field("cat").build());
boolean addAnotherContext = randomBoolean();
if (addAnotherContext) {
map.put("type", ContextBuilder.category("type").field("type").build());
}
final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
createIndexAndMapping(mapping);
int numDocs = 10;
List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
for (int i = 0; i < numDocs; i++) {
XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "sugg" + i + "estion").field("weight", i + 1).endObject().field("cat", "cat" + i % 2);
if (addAnotherContext) {
source.field("type", "type" + i % 3);
}
source.endObject();
indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
}
indexRandom(true, indexRequestBuilders);
CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).regex("sugg.*es");
assertSuggestions("foo", prefix, "sugg9estion", "sugg8estion", "sugg7estion", "sugg6estion", "sugg5estion");
}
Aggregations