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

Consider supporting in, containing, not in, not containing query method keywords #15

Merged
merged 1 commit into from
Dec 24, 2024
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
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
Loading