From be3c5b44e039d402a8545c8f72f98f8f4d3d068b Mon Sep 17 00:00:00 2001 From: Britta Weber Date: Mon, 17 Mar 2014 15:04:33 +0100 Subject: [PATCH] category type should be called "category" instead of "field" in context suggester Change according to documentation. --- .../context/CategoryContextMapping.java | 2 +- .../suggest/ContextSuggestSearchTests.java | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/search/suggest/context/CategoryContextMapping.java b/src/main/java/org/elasticsearch/search/suggest/context/CategoryContextMapping.java index d529848c2e0..3e4f2571169 100644 --- a/src/main/java/org/elasticsearch/search/suggest/context/CategoryContextMapping.java +++ b/src/main/java/org/elasticsearch/search/suggest/context/CategoryContextMapping.java @@ -43,7 +43,7 @@ import java.util.*; */ public class CategoryContextMapping extends ContextMapping { - protected static final String TYPE = "field"; + protected static final String TYPE = "category"; private static final String FIELD_FIELDNAME = "path"; private static final String DEFAULT_FIELDNAME = "_type"; diff --git a/src/test/java/org/elasticsearch/search/suggest/ContextSuggestSearchTests.java b/src/test/java/org/elasticsearch/search/suggest/ContextSuggestSearchTests.java index c2f8b454b01..6d99b3cfb9e 100644 --- a/src/test/java/org/elasticsearch/search/suggest/ContextSuggestSearchTests.java +++ b/src/test/java/org/elasticsearch/search/suggest/ContextSuggestSearchTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.suggest; import com.carrotsearch.randomizedtesting.generators.RandomStrings; +import com.google.common.collect.Sets; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.action.suggest.SuggestRequestBuilder; import org.elasticsearch.action.suggest.SuggestResponse; @@ -248,6 +249,63 @@ public class ContextSuggestSearchTests extends ElasticsearchIntegrationTest { assertPrefixSuggestions(2, "w", "Whitemane, Kofi"); } + @Test + public void testTypeCategoryIsActuallyCalledCategory() throws Exception { + XContentBuilder mapping = jsonBuilder(); + mapping.startObject().startObject(TYPE).startObject("properties") + .startObject("suggest_field").field("type", "completion") + .startObject("context").startObject("color").field("type", "category").endObject().endObject() + .endObject() + .endObject().endObject().endObject(); + assertAcked(prepareCreate(INDEX).addMapping(TYPE, mapping)); + ensureYellow(); + XContentBuilder doc1 = jsonBuilder(); + doc1.startObject().startObject("suggest_field") + .field("input", "backpack_red") + .startObject("context").field("color", "red", "all_colors").endObject() + .endObject().endObject(); + XContentBuilder doc2 = jsonBuilder(); + doc2.startObject().startObject("suggest_field") + .field("input", "backpack_green") + .startObject("context").field("color", "green", "all_colors").endObject() + .endObject().endObject(); + + client().prepareIndex(INDEX, TYPE, "1") + .setSource(doc1).execute() + .actionGet(); + client().prepareIndex(INDEX, TYPE, "2") + .setSource(doc2).execute() + .actionGet(); + + refresh(); + getBackpackSuggestionAndCompare("all_colors", "backpack_red", "backpack_green"); + getBackpackSuggestionAndCompare("red", "backpack_red"); + getBackpackSuggestionAndCompare("green", "backpack_green"); + getBackpackSuggestionAndCompare("not_existing_color"); + + } + + private void getBackpackSuggestionAndCompare(String contextValue, String... expectedText) { + Set expected = Sets.newHashSet(expectedText); + CompletionSuggestionBuilder context = new CompletionSuggestionBuilder("suggestion").field("suggest_field").text("back").size(10).addContextField("color", contextValue); + SuggestRequestBuilder suggestionRequest = client().prepareSuggest(INDEX).addSuggestion(context); + SuggestResponse suggestResponse = suggestionRequest.execute().actionGet(); + Suggest suggest = suggestResponse.getSuggest(); + assertEquals(suggest.size(), 1); + for (Suggestion> s : suggest) { + CompletionSuggestion suggestion = (CompletionSuggestion) s; + for (CompletionSuggestion.Entry entry : suggestion) { + List options = entry.getOptions(); + assertEquals(options.size(), expectedText.length); + for (CompletionSuggestion.Entry.Option option : options) { + assertTrue(expected.contains(option.getText().string())); + expected.remove(option.getText().string()); + } + } + } + } + + @Test public void testBasic() throws Exception { assertAcked(prepareCreate(INDEX).addMapping(TYPE, createMapping(TYPE, false, ContextBuilder.reference("st", "_type"), ContextBuilder.reference("nd", "_type"))));