This repository has been archived by the owner on Jun 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 727
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added SuggestResult class and integration test for suggest action.
- Loading branch information
kramer
committed
Apr 17, 2015
1 parent
b3d564b
commit 0a9c7a6
Showing
4 changed files
with
126 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
jest-common/src/main/java/io/searchbox/core/SuggestResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
jest/src/test/java/io/searchbox/core/SuggestIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |