diff --git a/src/main/java/org/springframework/data/reindexer/repository/query/ReindexerRepositoryQuery.java b/src/main/java/org/springframework/data/reindexer/repository/query/ReindexerRepositoryQuery.java index b081b97..bf3a5c3 100644 --- a/src/main/java/org/springframework/data/reindexer/repository/query/ReindexerRepositoryQuery.java +++ b/src/main/java/org/springframework/data/reindexer/repository/query/ReindexerRepositoryQuery.java @@ -15,6 +15,7 @@ */ package org.springframework.data.reindexer.repository.query; +import java.util.Collection; import java.util.Iterator; import ru.rt.restream.reindexer.Namespace; @@ -101,6 +102,12 @@ private Query where(Part part, Query criteria, Iterator parameters return criteria.where(indexName, Query.Condition.LT, parameters.next()); case LESS_THAN_EQUAL: return criteria.where(indexName, Query.Condition.LE, parameters.next()); + case IN: + case CONTAINING: + return createInQuery(criteria, indexName, parameters); + case NOT_IN: + case NOT_CONTAINING: + return createInQuery(criteria.not(), indexName, parameters); case IS_NOT_NULL: return criteria.isNotNull(indexName); case IS_NULL: @@ -114,6 +121,12 @@ private Query where(Part part, Query criteria, Iterator parameters } } + private Query createInQuery(Query criteria, String indexName, Iterator parameters) { + Object value = parameters.next(); + Assert.isInstanceOf(Collection.class, value, () -> "Expected Collection but got " + value); + return criteria.where(indexName, Query.Condition.SET, (Collection) value); + } + @Override public ReindexerQueryMethod getQueryMethod() { return this.queryMethod; diff --git a/src/test/java/org/springframework/data/reindexer/repository/ReindexerRepositoryTests.java b/src/test/java/org/springframework/data/reindexer/repository/ReindexerRepositoryTests.java index 6c54409..ddd132c 100644 --- a/src/test/java/org/springframework/data/reindexer/repository/ReindexerRepositoryTests.java +++ b/src/test/java/org/springframework/data/reindexer/repository/ReindexerRepositoryTests.java @@ -539,6 +539,54 @@ public void deleteAll() { assertEquals(0, this.repository.count()); } + @Test + public void findByIdIn() { + List expectedItems = new ArrayList<>(); + for (long i = 0; i < 100; i++) { + expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i))); + } + List foundItems = this.repository.findByIdIn(expectedItems.stream() + .map(TestItem::getId) + .collect(Collectors.toList())); + assertEquals(expectedItems.size(), foundItems.size()); + } + + @Test + public void findByIdContaining() { + List expectedItems = new ArrayList<>(); + for (long i = 0; i < 100; i++) { + expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i))); + } + List foundItems = this.repository.findByIdContaining(expectedItems.stream() + .map(TestItem::getId) + .collect(Collectors.toList())); + assertEquals(expectedItems.size(), foundItems.size()); + } + + @Test + public void findByIdNotIn() { + List expectedItems = new ArrayList<>(); + for (long i = 0; i < 100; i++) { + expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i))); + } + List foundItems = this.repository.findByIdNotIn(expectedItems.stream() + .map(TestItem::getId) + .collect(Collectors.toList())); + assertEquals(0, foundItems.size()); + } + + @Test + public void findByIdNotContaining() { + List expectedItems = new ArrayList<>(); + for (long i = 0; i < 100; i++) { + expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i))); + } + List foundItems = this.repository.findByIdNotContaining(expectedItems.stream() + .map(TestItem::getId) + .collect(Collectors.toList())); + assertEquals(0, foundItems.size()); + } + @Configuration @EnableReindexerRepositories(basePackageClasses = TestItemReindexerRepository.class, considerNestedRepositories = true) @EnableTransactionManagement @@ -642,6 +690,13 @@ Optional findOneSqlByNameAndValueManyParams(String name1, String name2 @Query("SELECT * FROM items") Stream findAllStreamSql(); + List findByIdIn(List ids); + + List findByIdContaining(List ids); + + List findByIdNotIn(List ids); + + List findByIdNotContaining(List ids); } @Namespace(name = NAMESPACE_NAME)