From 0a9c7a675a8e87423b22428608c675453b7f792a Mon Sep 17 00:00:00 2001 From: kramer Date: Fri, 17 Apr 2015 17:09:14 +0200 Subject: [PATCH] added SuggestResult class and integration test for suggest action. --- .../main/java/io/searchbox/core/Suggest.java | 10 ++- .../java/io/searchbox/core/SuggestResult.java | 47 ++++++++++++++ .../java/io/searchbox/core/SuggestTest.java | 16 ++--- .../core/SuggestIntegrationTest.java | 64 +++++++++++++++++++ 4 files changed, 126 insertions(+), 11 deletions(-) create mode 100644 jest-common/src/main/java/io/searchbox/core/SuggestResult.java create mode 100644 jest/src/test/java/io/searchbox/core/SuggestIntegrationTest.java diff --git a/jest-common/src/main/java/io/searchbox/core/Suggest.java b/jest-common/src/main/java/io/searchbox/core/Suggest.java index 9830b4c38..7df656f49 100644 --- a/jest-common/src/main/java/io/searchbox/core/Suggest.java +++ b/jest-common/src/main/java/io/searchbox/core/Suggest.java @@ -6,7 +6,7 @@ import com.google.gson.Gson; -public class Suggest extends AbstractAction { +public class Suggest extends AbstractAction { private final String query; @@ -18,8 +18,8 @@ private Suggest(final Builder builder) { } @Override - public JestResult createNewElasticSearchResult(final String json, final int statusCode, final String reasonPhrase, final Gson gson) { - return this.createNewElasticSearchResult(new JestResult(gson), json, statusCode, reasonPhrase, gson); + public SuggestResult createNewElasticSearchResult(final String json, final int statusCode, final String reasonPhrase, final Gson gson) { + return this.createNewElasticSearchResult(new SuggestResult(gson), json, statusCode, reasonPhrase, gson); } @Override @@ -51,6 +51,10 @@ public static class Builder extends AbstractMultiTypeActionBuilder getSuggestions(String suggestionName) { + List suggestions = new ArrayList(); + + if (jsonObject != null && jsonObject.has(suggestionName)) { + for (JsonElement suggestionElement : jsonObject.getAsJsonArray(suggestionName)) { + suggestions.add(gson.fromJson(suggestionElement, Suggestion.class)); + } + } + + return suggestions; + } + + public class Suggestion { + public final String text; + public final Integer offset; + public final Integer length; + public final List> options; + + public Suggestion(String text, Integer offset, Integer length, List> options) { + this.text = text; + this.offset = offset; + this.length = length; + this.options = options; + } + } + +} diff --git a/jest-common/src/test/java/io/searchbox/core/SuggestTest.java b/jest-common/src/test/java/io/searchbox/core/SuggestTest.java index 2c13cb297..1df97fb85 100644 --- a/jest-common/src/test/java/io/searchbox/core/SuggestTest.java +++ b/jest-common/src/test/java/io/searchbox/core/SuggestTest.java @@ -11,43 +11,43 @@ public class SuggestTest { @Test public void getURIWithoutIndexAndType() { - Action suggest = new Suggest.Builder("").build(); + Action suggest = new Suggest.Builder("").build(); assertEquals("_all/_suggest", suggest.getURI()); } @Test public void getURIWithOnlyOneIndex() { - Action suggest = new Suggest.Builder("").addIndex("twitter").build(); + Action suggest = new Suggest.Builder("").addIndex("twitter").build(); assertEquals("twitter/_suggest", suggest.getURI()); } @Test public void getURIWithOneIndexAndOneType() { - Action suggest = new Suggest.Builder("").addIndex("twitter").addType("tweet").build(); + Action suggest = new Suggest.Builder("").addIndex("twitter").addType("tweet").build(); assertEquals("twitter/tweet/_suggest", suggest.getURI()); } @Test public void getURIWithOnlyOneType() { - Action suggest = new Suggest.Builder("").addType("tweet").build(); + Action suggest = new Suggest.Builder("").addType("tweet").build(); assertEquals("_all/tweet/_suggest", suggest.getURI()); } @Test public void getURIWithOnlyMultipleIndex() { - Action suggest = new Suggest.Builder("").addIndex("twitter").addIndex("suggestbox").build(); + Action suggest = new Suggest.Builder("").addIndex("twitter").addIndex("suggestbox").build(); assertEquals("twitter%2Csuggestbox/_suggest", suggest.getURI()); } @Test public void getURIWithOnlyMultipleType() { - Action suggest = new Suggest.Builder("").addType("tweet").addType("jest").build(); + Action suggest = new Suggest.Builder("").addType("tweet").addType("jest").build(); assertEquals("_all/tweet%2Cjest/_suggest", suggest.getURI()); } @Test public void getURIWithMultipleIndexAndTypes() { - Action suggest = new Suggest.Builder("") + Action suggest = new Suggest.Builder("") .addIndex("twitter") .addIndex("suggestbox") .addType("tweet") @@ -59,7 +59,7 @@ public void getURIWithMultipleIndexAndTypes() { @Test public void getDataSimple() { String query = "{\"elementSuggestion\":{\"text\":\"courgette\",\"completion\":{\"field\":\"element.completion\",\"size\":10,\"fuzzy\":{}}}}"; - Action suggest = new Suggest.Builder(query).build(); + Action suggest = new Suggest.Builder(query).build(); assertNotNull("data", suggest.getData(null)); assertEquals(query, suggest.getData(null).toString()); diff --git a/jest/src/test/java/io/searchbox/core/SuggestIntegrationTest.java b/jest/src/test/java/io/searchbox/core/SuggestIntegrationTest.java new file mode 100644 index 000000000..5d70dd6b0 --- /dev/null +++ b/jest/src/test/java/io/searchbox/core/SuggestIntegrationTest.java @@ -0,0 +1,64 @@ +package io.searchbox.core; + +import io.searchbox.common.AbstractIntegrationTest; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.test.ElasticsearchIntegrationTest; +import org.junit.Test; + +import java.io.IOException; +import java.util.List; + +/** + * @author cihat keser + */ +@ElasticsearchIntegrationTest.ClusterScope(scope = ElasticsearchIntegrationTest.Scope.SUITE, numDataNodes = 1) +public class SuggestIntegrationTest extends AbstractIntegrationTest { + + private static final String INDEX = "socializr"; + private static final String TYPE = "meetings"; + + @Test + public void testWithSingleTermSuggester() throws IOException { + String suggestionName = "my-suggestion"; + + assertTrue(client().index(new IndexRequest(INDEX, TYPE).source("{\"body\":\"istanbul\"}").refresh(true)).actionGet().isCreated()); + assertTrue(client().index(new IndexRequest(INDEX, TYPE).source("{\"body\":\"amsterdam\"}").refresh(true)).actionGet().isCreated()); + assertTrue(client().index(new IndexRequest(INDEX, TYPE).source("{\"body\":\"rotterdam\"}").refresh(true)).actionGet().isCreated()); + assertTrue(client().index(new IndexRequest(INDEX, TYPE).source("{\"body\":\"vienna\"}").refresh(true)).actionGet().isCreated()); + assertTrue(client().index(new IndexRequest(INDEX, TYPE).source("{\"body\":\"london\"}").refresh(true)).actionGet().isCreated()); + + Suggest suggest = new Suggest.Builder("{\n" + + " \"" + suggestionName + "\" : {\n" + + " \"text\" : \"the amsterdma meetpu\",\n" + + " \"term\" : {\n" + + " \"field\" : \"body\"\n" + + " }\n" + + " }\n" + + "}").build(); + + SuggestResult result = client.execute(suggest); + assertTrue(result.getErrorMessage(), result.isSucceeded()); + + List suggestions = result.getSuggestions(suggestionName); + assertEquals(3, suggestions.size()); + + SuggestResult.Suggestion suggestion1 = suggestions.get(0); + assertEquals("the", suggestion1.text); + assertEquals(Integer.valueOf(0), suggestion1.offset); + assertEquals(Integer.valueOf(3), suggestion1.length); + assertEquals(0, suggestion1.options.size()); + + SuggestResult.Suggestion suggestion2 = suggestions.get(1); + assertEquals("amsterdma", suggestion2.text); + assertEquals(Integer.valueOf(4), suggestion2.offset); + assertEquals(Integer.valueOf(9), suggestion2.length); + assertEquals(1, suggestion2.options.size()); + + SuggestResult.Suggestion suggestion3 = suggestions.get(2); + assertEquals("meetpu", suggestion3.text); + assertEquals(Integer.valueOf(14), suggestion3.offset); + assertEquals(Integer.valueOf(6), suggestion3.length); + assertEquals(0, suggestion3.options.size()); + } + +}