Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
added SuggestResult class and integration test for suggest action.
Browse files Browse the repository at this point in the history
  • Loading branch information
kramer committed Apr 17, 2015
1 parent b3d564b commit 0a9c7a6
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 11 deletions.
10 changes: 7 additions & 3 deletions jest-common/src/main/java/io/searchbox/core/Suggest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import com.google.gson.Gson;

public class Suggest extends AbstractAction<JestResult> {
public class Suggest extends AbstractAction<SuggestResult> {

private final String query;

Expand All @@ -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
Expand Down Expand Up @@ -51,6 +51,10 @@ public static class Builder extends AbstractMultiTypeActionBuilder<Suggest, Buil

private final String query;

/**
* Suggest requests executed against the _suggest endpoint should omit the
* surrounding suggest element which is only used if the suggest request is part of a search.
*/
public Builder(final String query) {
this.query = query;
}
Expand Down
47 changes: 47 additions & 0 deletions jest-common/src/main/java/io/searchbox/core/SuggestResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.searchbox.core;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.searchbox.client.JestResult;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author cihat keser
*/
public class SuggestResult extends JestResult {

public SuggestResult(Gson gson) {
super(gson);
}

public List<Suggestion> getSuggestions(String suggestionName) {
List<Suggestion> suggestions = new ArrayList<Suggestion>();

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<Map<String, Object>> options;

public Suggestion(String text, Integer offset, Integer length, List<Map<String, Object>> options) {
this.text = text;
this.offset = offset;
this.length = length;
this.options = options;
}
}

}
16 changes: 8 additions & 8 deletions jest-common/src/test/java/io/searchbox/core/SuggestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@ public class SuggestTest {

@Test
public void getURIWithoutIndexAndType() {
Action<JestResult> suggest = new Suggest.Builder("").build();
Action suggest = new Suggest.Builder("").build();
assertEquals("_all/_suggest", suggest.getURI());
}

@Test
public void getURIWithOnlyOneIndex() {
Action<JestResult> 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<JestResult> 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<JestResult> 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<JestResult> 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<JestResult> 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<JestResult> suggest = new Suggest.Builder("")
Action suggest = new Suggest.Builder("")
.addIndex("twitter")
.addIndex("suggestbox")
.addType("tweet")
Expand All @@ -59,7 +59,7 @@ public void getURIWithMultipleIndexAndTypes() {
@Test
public void getDataSimple() {
String query = "{\"elementSuggestion\":{\"text\":\"courgette\",\"completion\":{\"field\":\"element.completion\",\"size\":10,\"fuzzy\":{}}}}";
Action<JestResult> suggest = new Suggest.Builder(query).build();
Action suggest = new Suggest.Builder(query).build();

assertNotNull("data", suggest.getData(null));
assertEquals(query, suggest.getData(null).toString());
Expand Down
64 changes: 64 additions & 0 deletions jest/src/test/java/io/searchbox/core/SuggestIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -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<SuggestResult.Suggestion> 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());
}

}

0 comments on commit 0a9c7a6

Please sign in to comment.