Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure nested field support #374

Merged
merged 2 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/test/java/com/meilisearch/integration/SearchNestedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

@Tag("integration")
public class SearchNestedTest extends AbstractIT {

private TestData<Movie> testData;

final class Results {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class already exist in other places right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in the SearchTest file.

Movie[] hits;
int offset;
int limit;
int nbHits;
boolean exhaustiveNbHits;
int processingTimeMs;
String query;
}

@BeforeEach
public void initialize() {
cleanup();
this.setUp();
if (testData == null) testData = this.getTestData(NESTED_MOVIES, Movie.class);
}

/** 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<Movie> 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<Movie> 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<Movie> 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<Movie> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ public abstract class AbstractIT {
private final Map<String, TestData<?>> 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();
}
Expand Down