Skip to content

Commit

Permalink
Add support for pagination setting.
Browse files Browse the repository at this point in the history
Closes #121
  • Loading branch information
junghoon-vans authored Jun 6, 2024
1 parent e9870fa commit f4484f6
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vanslog.spring.data.meilisearch.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.data.annotation.Persistent;

/**
* Meilisearch Pagination
*
* @author Junghoon Ban
*/
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
public @interface Pagination {

/**
* The maximum number of search results Meilisearch can return
*/
int maxTotalHits() default 1000;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* Meilisearch Setting
*
* @author Junghoon Ban
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings">Settings</a>
*/
@Persistent
@Inherited
Expand All @@ -34,19 +35,50 @@
public @interface Setting {

/**
* attributes to define an index sorting
* attributes to be used for sorting
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#sortable-attributes">Sortable attributes</a>
*/
String[] sortAttributes() default {};

/**
* attributes to be used for filtering
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#filterable-attributes">Filterable attributes</a>
*/
String[] filterableAttributes() default {};

/**
* defines the ranking rules
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#ranking-rules">Ranking rules</a>
*/
String distinctAttribute() default "";

/**
* attributes to be used for searching
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#searchable-attributes">Searchable attributes</a>
*/
String[] searchableAttributes() default { "*" };

/**
* attributes to be displayed in the search results
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#displayed-attributes">Displayed attributes</a>
*/
String[] displayedAttributes() default { "*" };

/**
* defines the ranking rules
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#ranking-rules">Ranking rules</a>
*/
String[] rankingRules() default { "words", "typo", "proximity", "attribute", "sort", "exactness" };

/**
* defines the stop words
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#stop-words">Stop words</a>
*/
String[] stopWords() default {};

/**
* defines the pagination behavior
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#pagination">Pagination</a>
*/
Pagination pagination() default @Pagination;
}
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ public interface MeilisearchOperations {

/**
* Search for entities that meet the criteria using the given {@link SearchRequest}. Note that by default,
* {@literal MaxTotalHits} in {@link com.meilisearch.sdk.model.Pagination} is 1000.
* {@literal MaxTotalHits} in {@link io.vanslog.spring.data.meilisearch.annotations.Pagination} is 1000. If you want
* to change the value, you can use {@link io.vanslog.spring.data.meilisearch.annotations.Setting}.
*
* @param searchRequest the search request
* @param clazz the entity class, must be annotated with
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.meilisearch.sdk.model.Settings;
import io.vanslog.spring.data.meilisearch.annotations.Document;

import io.vanslog.spring.data.meilisearch.annotations.Pagination;
import io.vanslog.spring.data.meilisearch.annotations.Setting;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
Expand Down Expand Up @@ -114,6 +115,7 @@ private void processSettingAnnotation(Setting settingAnnotation, SettingsParamet
settingsParameter.searchableAttributes = settingAnnotation.searchableAttributes();
settingsParameter.displayedAttributes = settingAnnotation.displayedAttributes();
settingsParameter.rankingRules = settingAnnotation.rankingRules();
settingsParameter.pagination = settingAnnotation.pagination();

String[] sortAttributes = settingAnnotation.sortAttributes();
String[] filterableAttributes = settingAnnotation.filterableAttributes();
Expand Down Expand Up @@ -145,6 +147,7 @@ private static class SettingsParameter {
private String[] displayedAttributes;
private String[] rankingRules;
@Nullable private String[] stopWords;
private Pagination pagination;

Settings toSettings() {
Settings settings = new Settings();
Expand All @@ -168,6 +171,10 @@ Settings toSettings() {
settings.setStopWords(stopWords);
}

var meiliPagination = new com.meilisearch.sdk.model.Pagination();
meiliPagination.setMaxTotalHits(this.pagination.maxTotalHits());
settings.setPagination(meiliPagination);

return settings;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.vanslog.spring.data.meilisearch.entities;

import io.vanslog.spring.data.meilisearch.annotations.Document;
import io.vanslog.spring.data.meilisearch.annotations.Pagination;
import io.vanslog.spring.data.meilisearch.annotations.Setting;
import org.springframework.data.annotation.Id;

/**
* Pagination entity for tests.
* It is set to have a maximum of 10 total hits.
*/
@Document(indexUid = "total-hits-limited")
@Setting(
sortAttributes = { "name" },
pagination = @Pagination(maxTotalHits = 10)
)
public class TotalHitsLimited {
@Id
public int id;
public String name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.assertj.core.api.Assertions.*;

import io.vanslog.spring.data.meilisearch.entities.Movie;
import io.vanslog.spring.data.meilisearch.entities.TotalHitsLimited;
import io.vanslog.spring.data.meilisearch.junit.jupiter.MeilisearchTest;
import io.vanslog.spring.data.meilisearch.junit.jupiter.MeilisearchTestConfiguration;
import io.vanslog.spring.data.meilisearch.repository.config.EnableMeilisearchRepositories;
Expand Down Expand Up @@ -45,6 +46,7 @@
class MeilisearchRepositoryIntegrationTests {

@Autowired private MovieRepository movieRepository;
@Autowired private TotalHitsLimitedRepository totalHitsLimitedRepository;

@BeforeEach
void setUp() {
Expand Down Expand Up @@ -335,7 +337,6 @@ void shouldFindDocumentsWithSorting() {
assertThat(ascOrdered).hasSize(3).containsExactly(movie1, movie3, movie2);
}


@Test
void shouldFindDocumentsWithPagingAndSorting() {
// given
Expand Down Expand Up @@ -380,9 +381,29 @@ void shouldFindDocumentsWithPagingAndSorting() {
assertThat(page2.getContent().get(0)).isEqualTo(movie2);
}

@Test
void shouldNotSearchAllElementsWhenMaxTotalHitsAre10() {
// given
int elementCount = 11;

for (int i = 0; i < elementCount; i++) {
TotalHitsLimited entity = new TotalHitsLimited(); // It is limited to 10 hits.
entity.id = i;
entity.name = "name" + i;
totalHitsLimitedRepository.save(entity);
}

// when
Page<TotalHitsLimited> page = totalHitsLimitedRepository.findAll(PageRequest.of(0, elementCount));

// then
assertThat(page).hasSizeLessThan(elementCount);
}

interface MovieRepository extends MeilisearchRepository<Movie, Integer> {}

interface TotalHitsLimitedRepository extends MeilisearchRepository<TotalHitsLimited, String> {}

@Configuration
@Import(MeilisearchTestConfiguration.class)
@EnableMeilisearchRepositories(basePackages = "io.vanslog.spring.data.meilisearch.repository",
Expand Down

0 comments on commit f4484f6

Please sign in to comment.