From 62a38599f81652178090d751d9c94695a60fc858 Mon Sep 17 00:00:00 2001 From: alallema Date: Mon, 25 Apr 2022 10:13:36 +0200 Subject: [PATCH 1/2] Ensure nested field support --- .../integration/SearchNestedTest.java | 125 ++++++++++++++++++ .../integration/classes/AbstractIT.java | 2 + 2 files changed, 127 insertions(+) create mode 100644 src/test/java/com/meilisearch/integration/SearchNestedTest.java diff --git a/src/test/java/com/meilisearch/integration/SearchNestedTest.java b/src/test/java/com/meilisearch/integration/SearchNestedTest.java new file mode 100644 index 00000000..4c2721cd --- /dev/null +++ b/src/test/java/com/meilisearch/integration/SearchNestedTest.java @@ -0,0 +1,125 @@ +package com.meilisearch.integration; + +import static org.junit.jupiter.api.Assertions.*; + +import com.meilisearch.integration.classes.AbstractIT; +import com.meilisearch.integration.classes.TestData; +import com.meilisearch.sdk.Index; +import com.meilisearch.sdk.SearchRequest; +import com.meilisearch.sdk.Settings; +import com.meilisearch.sdk.Task; +import com.meilisearch.sdk.json.GsonJsonHandler; +import com.meilisearch.sdk.utils.Movie; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Tag; +import org.junit.jupiter.api.Test; + +@Tag("integration") +public class SearchNestedTest extends AbstractIT { + + private TestData testData; + + final class Results { + Movie[] hits; + int offset; + int limit; + int nbHits; + boolean exhaustiveNbHits; + int processingTimeMs; + String query; + } + + @BeforeEach + public void initialize() { + this.setUp(); + if (testData == null) testData = this.getTestData(NESTED_MOVIES, Movie.class); + } + + @AfterAll + static void cleanMeiliSearch() { + cleanup(); + } + + /** Test basic search in nested fields */ + @Test + public void testBasicSearchInNestedFields() throws Exception { + String indexUid = "SearchOnNestedFields"; + Index index = client.index(indexUid); + GsonJsonHandler jsonGson = new GsonJsonHandler(); + + TestData testData = this.getTestData(NESTED_MOVIES, Movie.class); + Task task = index.addDocuments(testData.getRaw()); + index.waitForTask(task.getUid()); + Results searchResultGson = jsonGson.decode(index.rawSearch("An awesome"), Results.class); + + assertEquals(1, searchResultGson.hits.length); + assertEquals("5", searchResultGson.hits[0].getId()); + } + + /** Test search on nested documents with searchable attributes */ + @Test + public void testSearchOnNestedFieldsWithSearchableAttributes() throws Exception { + String indexUid = "SearchOnNestedFields"; + Index index = client.index(indexUid); + GsonJsonHandler jsonGson = new GsonJsonHandler(); + String[] newSearchableAttributes = {"title", "info.comment"}; + + TestData testData = this.getTestData(NESTED_MOVIES, Movie.class); + Task task = index.addDocuments(testData.getRaw()); + + index.waitForTask(task.getUid()); + + index.waitForTask( + index.updateSearchableAttributesSettings(newSearchableAttributes).getUid()); + + Results searchResultGson = jsonGson.decode(index.rawSearch("An awesome"), Results.class); + + assertEquals(1, searchResultGson.hits.length); + assertEquals("5", searchResultGson.hits[0].getId()); + } + + /** Test search on nested documents with sortable attributes */ + @Test + public void testSearchOnNestedFieldsWithSortableAttributes() throws Exception { + String indexUid = "SearchOnNestedFields"; + Index index = client.index(indexUid); + GsonJsonHandler jsonGson = new GsonJsonHandler(); + Settings newSettings = new Settings(); + newSettings.setSortableAttributes(new String[] {"info.reviewNb"}); + + TestData testData = this.getTestData(NESTED_MOVIES, Movie.class); + Task task = index.addDocuments(testData.getRaw()); + index.waitForTask(task.getUid()); + index.waitForTask(index.updateSettings(newSettings).getUid()); + SearchRequest searchRequest = + new SearchRequest("An Awesome").setSort(new String[] {"info.reviewNb:desc"}); + + Results searchResultGson = jsonGson.decode(index.rawSearch(searchRequest), Results.class); + + assertEquals(1, searchResultGson.hits.length); + assertEquals("5", searchResultGson.hits[0].getId()); + } + + /** Test search on nested documents with sortable and searchable attributes */ + @Test + public void testSearchOnNestedFieldsWithSortableAndSearchableAttributes() throws Exception { + String indexUid = "SearchOnNestedFields"; + Index index = client.index(indexUid); + GsonJsonHandler jsonGson = new GsonJsonHandler(); + Settings newSettings = new Settings(); + newSettings.setSearchableAttributes(new String[] {"title", "info.comment"}); + newSettings.setSortableAttributes(new String[] {"info.reviewNb"}); + + TestData testData = this.getTestData(NESTED_MOVIES, Movie.class); + Task task = index.addDocuments(testData.getRaw()); + index.waitForTask(task.getUid()); + index.waitForTask(index.updateSettings(newSettings).getUid()); + SearchRequest searchRequest = + new SearchRequest("An Awesome").setSort(new String[] {"info.reviewNb:desc"}); + Results searchResultGson = jsonGson.decode(index.rawSearch(searchRequest), Results.class); + + assertEquals(1, searchResultGson.hits.length); + assertEquals("5", searchResultGson.hits[0].getId()); + } +} diff --git a/src/test/java/com/meilisearch/integration/classes/AbstractIT.java b/src/test/java/com/meilisearch/integration/classes/AbstractIT.java index ea054bdb..44a0908f 100644 --- a/src/test/java/com/meilisearch/integration/classes/AbstractIT.java +++ b/src/test/java/com/meilisearch/integration/classes/AbstractIT.java @@ -22,10 +22,12 @@ public abstract class AbstractIT { private final Map> testData = new HashMap<>(); public static final String MOVIES_INDEX = "movies.json"; + public static final String NESTED_MOVIES = "nested_movies.json"; public AbstractIT() { try { loadResource(MOVIES_INDEX); + loadResource(NESTED_MOVIES); } catch (IOException e) { e.printStackTrace(); } From f15f3d93379a49b0c9c12909f38aa97b2e8a4f91 Mon Sep 17 00:00:00 2001 From: alallema Date: Wed, 4 May 2022 14:54:11 +0200 Subject: [PATCH 2/2] Clean tests --- .../java/com/meilisearch/integration/SearchNestedTest.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/test/java/com/meilisearch/integration/SearchNestedTest.java b/src/test/java/com/meilisearch/integration/SearchNestedTest.java index 4c2721cd..8e96ca99 100644 --- a/src/test/java/com/meilisearch/integration/SearchNestedTest.java +++ b/src/test/java/com/meilisearch/integration/SearchNestedTest.java @@ -10,7 +10,6 @@ import com.meilisearch.sdk.Task; import com.meilisearch.sdk.json.GsonJsonHandler; import com.meilisearch.sdk.utils.Movie; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -32,15 +31,11 @@ final class Results { @BeforeEach public void initialize() { + cleanup(); this.setUp(); if (testData == null) testData = this.getTestData(NESTED_MOVIES, Movie.class); } - @AfterAll - static void cleanMeiliSearch() { - cleanup(); - } - /** Test basic search in nested fields */ @Test public void testBasicSearchInNestedFields() throws Exception {