Skip to content

Commit

Permalink
Consider supporting in, containing, not in, not containing query meth…
Browse files Browse the repository at this point in the history
…od keywords

Closes gh-14
  • Loading branch information
DAN1X27 committed Dec 24, 2024
1 parent 2ed4b7b commit 336a679
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,6 +102,12 @@ private Query<?> where(Part part, Query<?> criteria, Iterator<Object> 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:
Expand All @@ -114,6 +121,12 @@ private Query<?> where(Part part, Query<?> criteria, Iterator<Object> parameters
}
}

private Query<?> createInQuery(Query<?> criteria, String indexName, Iterator<Object> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,54 @@ public void deleteAll() {
assertEquals(0, this.repository.count());
}

@Test
public void findByIdIn() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
List<TestItem> foundItems = this.repository.findByIdIn(expectedItems.stream()
.map(TestItem::getId)
.collect(Collectors.toList()));
assertEquals(expectedItems.size(), foundItems.size());
}

@Test
public void findByIdContaining() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
List<TestItem> foundItems = this.repository.findByIdContaining(expectedItems.stream()
.map(TestItem::getId)
.collect(Collectors.toList()));
assertEquals(expectedItems.size(), foundItems.size());
}

@Test
public void findByIdNotIn() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
List<TestItem> foundItems = this.repository.findByIdNotIn(expectedItems.stream()
.map(TestItem::getId)
.collect(Collectors.toList()));
assertEquals(0, foundItems.size());
}

@Test
public void findByIdNotContaining() {
List<TestItem> expectedItems = new ArrayList<>();
for (long i = 0; i < 100; i++) {
expectedItems.add(this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i)));
}
List<TestItem> foundItems = this.repository.findByIdNotContaining(expectedItems.stream()
.map(TestItem::getId)
.collect(Collectors.toList()));
assertEquals(0, foundItems.size());
}

@Configuration
@EnableReindexerRepositories(basePackageClasses = TestItemReindexerRepository.class, considerNestedRepositories = true)
@EnableTransactionManagement
Expand Down Expand Up @@ -642,6 +690,13 @@ Optional<TestItem> findOneSqlByNameAndValueManyParams(String name1, String name2
@Query("SELECT * FROM items")
Stream<TestItem> findAllStreamSql();

List<TestItem> findByIdIn(List<Long> ids);

List<TestItem> findByIdContaining(List<Long> ids);

List<TestItem> findByIdNotIn(List<Long> ids);

List<TestItem> findByIdNotContaining(List<Long> ids);
}

@Namespace(name = NAMESPACE_NAME)
Expand Down

0 comments on commit 336a679

Please sign in to comment.