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

Commit

Permalink
search inner hits & suggest query
Browse files Browse the repository at this point in the history
  • Loading branch information
musabgelisgen committed Aug 6, 2018
1 parent 2cb38cc commit 849a645
Showing 1 changed file with 103 additions and 3 deletions.
106 changes: 103 additions & 3 deletions jest/src/test/java/io/searchbox/core/SearchIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.searchbox.client.JestResult;
import io.searchbox.common.AbstractIntegrationTest;
import io.searchbox.params.Parameters;
Expand Down Expand Up @@ -55,7 +56,7 @@ public void searchWithValidQuery() throws IOException {

@Test
public void searchWithMultipleHits() throws Exception {
assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"user\":{\"type\":\"keyword\"}}}"));
assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"user\":{\"type\":\"keyword\"}}}", XContentType.JSON));
assertTrue(index(INDEX, TYPE, "swmh1", "{\"user\":\"kimchy1\"}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(INDEX, TYPE, "swmh2", "{\"user\":\"kimchy2\"}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(INDEX, TYPE, "swmh3", "{\"user\":\"kimchy3\"}").getResult().equals(DocWriteResponse.Result.CREATED));
Expand All @@ -79,7 +80,7 @@ public void searchWithMultipleHits() throws Exception {

@Test
public void searchWithSourceFilterByQuery() throws Exception {
assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"includeFieldName\":{\"type\":\"keyword\"}}}"));
assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"includeFieldName\":{\"type\":\"keyword\"}}}", XContentType.JSON));
assertTrue(index(INDEX, TYPE, "Jeehong1", "{\"includeFieldName\":\"SeoHoo\",\"excludeFieldName\":\"SeongJeon\"}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(INDEX, TYPE, "Jeehong2", "{\"includeFieldName\":\"Seola\",\"excludeFieldName\":\"SeongJeon\"}").getResult().equals(DocWriteResponse.Result.CREATED));
refresh();
Expand All @@ -97,7 +98,7 @@ public void searchWithSourceFilterByQuery() throws Exception {

@Test
public void searchWithSourceFilterByParam() throws Exception {
assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"includeFieldName\":{\"type\":\"keyword\"}}}"));
assertAcked(prepareCreate(INDEX).addMapping(TYPE, "{\"properties\":{\"includeFieldName\":{\"type\":\"keyword\"}}}", XContentType.JSON));
assertTrue(index(INDEX, TYPE, "Happyprg1", "{\"includeFieldName\":\"SeoHoo\",\"excludeFieldName\":\"SeongJeon\"}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(INDEX, TYPE, "Happyprg2", "{\"includeFieldName\":\"Seola\",\"excludeFieldName\":\"SeongJeon\"}").getResult().equals(DocWriteResponse.Result.CREATED));
refresh();
Expand Down Expand Up @@ -260,4 +261,103 @@ public void searchIndexWithTypeWithNullJestId() throws Exception {
List<TestArticleModel> articleResult = result.getSourceAsObjectList(TestArticleModel.class);
assertNotNull(articleResult.get(0).getId());
}

@Test
public void suggestQuery() throws IOException {
String index = "twitter";
String type = "tweet";

String mapping = "{\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }";

assertAcked(prepareCreate(index).addMapping(type, mapping, XContentType.JSON));
assertTrue(index(index, type, "1", "{\"message\":\"istanbul\"}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(index, type, "2", "{\"message\":\"amsterdam\"}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(index, type, "3", "{\"message\":\"rotterdam\"}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(index, type, "4", "{\"message\":\"vienna\"}").getResult().equals(DocWriteResponse.Result.CREATED));
assertTrue(index(index, type, "5", "{\"message\":\"london\"}").getResult().equals(DocWriteResponse.Result.CREATED));

refresh();
ensureSearchable(INDEX);

String query = "{\n" +
" \"query\" : {\n" +
" \"match\": {\n" +
" \"message\": \"amsterdam\"\n" +
" }\n" +
" },\n" +
" \"suggest\" : {\n" +
" \"my-suggestion\" : {\n" +
" \"text\" : \"amsterdma\",\n" +
" \"term\" : {\n" +
" \"field\" : \"message\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}";

SearchResult result = client.execute(new Search.Builder(query).addIndex(index).build());
assertTrue(result.getErrorMessage(), result.isSucceeded());
assertEquals("amsterdma", result.getJsonObject().getAsJsonObject("suggest").getAsJsonArray("my-suggestion").get(0).getAsJsonObject().get("text").getAsString());
}

@Test
public void searchInnerHits() throws Exception {

String field = "comments";

String mapping = "{\n" +
" \"properties\": {\n" +
" \"" + field + "\": {\n" +
" \"type\": \"nested\"\n" +
" }\n" +
" }\n" +
" }";
assertAcked(prepareCreate(INDEX).addMapping(TYPE, mapping, XContentType.JSON));

String source = "{\n" +
" \"title\": \"Test title\",\n" +
" \"" + field + "\": [\n" +
" {\n" +
" \"author\": \"ferhats\",\n" +
" \"number\": 1\n" +
" },\n" +
" {\n" +
" \"author\": \"musabg\",\n" +
" \"number\": 2\n" +
" }\n" +
" ]\n" +
"}";


assertTrue(index(INDEX, TYPE, null, source).getResult().equals(DocWriteResponse.Result.CREATED));
refresh();
ensureSearchable(INDEX);

String query = "{\n" +
" \"query\": {\n" +
" \"nested\": {\n" +
" \"path\": \"" + field + "\",\n" +
" \"query\": {\n" +
" \"match\": {\"" + field + ".number\" : 2}\n" +
" },\n" +
" \"inner_hits\": {} \n" +
" }\n" +
" }\n" +
"}";


SearchResult result = client.execute(new Search.Builder(query).build());
assertTrue(result.getErrorMessage(), result.isSucceeded());

JsonObject innerHits = result.getJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray().get(0).getAsJsonObject().get("inner_hits").getAsJsonObject();
JsonObject innerHitsResult = innerHits.get(field).getAsJsonObject().get("hits").getAsJsonObject().get("hits").getAsJsonArray().get(0).getAsJsonObject();

assertEquals("musabg", innerHitsResult.get("_source").getAsJsonObject().get("author").getAsString());
}
}

0 comments on commit 849a645

Please sign in to comment.