Search in sources :

Example 16 with CompletionMappingBuilder

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");
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)16 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)16 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)16 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)16 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)16 GeoContextMapping (org.elasticsearch.search.suggest.completion.context.GeoContextMapping)16 ArrayList (java.util.ArrayList)15 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 GeoPoint (org.elasticsearch.common.geo.GeoPoint)15 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)13 HashMap (java.util.HashMap)2 List (java.util.List)2 ToXContent (org.elasticsearch.common.xcontent.ToXContent)2 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 GeoQueryContext (org.elasticsearch.search.suggest.completion.context.GeoQueryContext)1