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 like query method keyword #44

Merged
merged 1 commit into from
Jan 14, 2025
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 @@ -36,6 +36,7 @@
import org.springframework.data.repository.query.ReturnedType;
import org.springframework.data.repository.query.parser.AbstractQueryCreator;
import org.springframework.data.repository.query.parser.Part;
import org.springframework.data.repository.query.parser.Part.Type;
import org.springframework.data.repository.query.parser.PartTree;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -99,15 +100,30 @@ protected Query<?> and(Part part, Query<?> base, Iterator<Object> parameters) {
case GREATER_THAN_EQUAL -> where(base, indexName, Condition.GE, parameters);
case LESS_THAN -> where(base, indexName, Condition.LT, parameters);
case LESS_THAN_EQUAL -> where(base, indexName, Condition.LE, parameters);
case IN, CONTAINING -> where(base, indexName, Condition.SET, parameters);
case NOT_IN, NOT_CONTAINING -> where(base.not(), indexName, Condition.SET, parameters);
case IS_NOT_NULL -> base.not().isNull(indexName);
case IN -> where(base, indexName, Condition.SET, parameters);
case NOT_IN -> where(base.not(), indexName, Condition.SET, parameters);
case IS_NOT_NULL -> base.isNotNull(indexName);
case IS_NULL -> base.isNull(indexName);
case SIMPLE_PROPERTY -> where(base, indexName, Condition.EQ, parameters);
case NEGATING_SIMPLE_PROPERTY -> where(base.not(), indexName, Condition.EQ, parameters);
case BETWEEN -> base.where(indexName, Condition.RANGE, getParameterValues(indexName, parameters.next(), parameters.next()));
case TRUE -> base.where(indexName, Condition.EQ, true);
case FALSE -> base.where(indexName, Condition.EQ, false);
case LIKE, NOT_LIKE, STARTING_WITH, ENDING_WITH, CONTAINING, NOT_CONTAINING -> {
if (part.getProperty().getLeafProperty().isCollection()) {
yield where(part.getType() == Type.NOT_CONTAINING ? base.not() : base, indexName, Condition.SET, parameters);
}
Object value = parameters.next();
Assert.isInstanceOf(String.class, value, "Value of '" + part.getType() + "' expression must be String");
String expression = switch (part.getType()) {
case STARTING_WITH -> value + "%";
case ENDING_WITH -> "%" + value;
case CONTAINING, NOT_CONTAINING -> "%" + value + "%";
default -> (String) value;
};
yield part.getType() == Type.NOT_LIKE || part.getType() == Type.NOT_CONTAINING
? base.not().like(indexName, expression) : base.like(indexName, expression);
}
default -> throw new IllegalArgumentException("Unsupported keyword!");
};
}
Expand All @@ -117,9 +133,7 @@ private Query<?> where(Query<?> base, String indexName, Condition condition, Ite
if (value instanceof Collection<?> values) {
return base.where(indexName, condition, values);
}
else {
return base.where(indexName, condition, value);
}
return base.where(indexName, condition, value);
}

private Object[] getParameterValues(String indexName, Object... values) {
Expand Down Expand Up @@ -167,16 +181,17 @@ protected Query<?> complete(Query<?> criteria, Sort sort) {
if (criteria == null) {
criteria = this.namespace.query();
}
if (this.tree.isDistinct()) {
if (this.returnedType.needsCustomConstruction()) {
for (String field : this.returnedType.getInputProperties()) {
if (this.returnedType.needsCustomConstruction()) {
String[] fields = this.returnedType.getInputProperties().toArray(String[]::new);
if (this.tree.isDistinct()) {
for (String field : fields) {
criteria.aggregateDistinct(field);
}
criteria.aggregateFacet(this.returnedType.getInputProperties().toArray(String[]::new));
criteria.aggregateFacet(fields);
}
else {
criteria.select(fields);
}
}
if (this.returnedType.needsCustomConstruction()) {
criteria.select(this.returnedType.getInputProperties().toArray(String[]::new));
}
else if (this.tree.isExistsProjection()) {
criteria.select(this.entityInformation.getIdFieldName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -785,35 +785,29 @@ public void findByIdInArray() {

@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());
this.repository.save(new TestItem(1L, "TestName1", "TestValue1", List.of("City1", "City2")));
this.repository.save(new TestItem(2L, "TestName2", "TestValue2", List.of("City1", "City3")));
this.repository.save(new TestItem(3L, "TestName3", "TestValue3", List.of("City2", "City3")));
List<TestItem> foundItems = this.repository.findAllByCitiesContaining("City1");
assertThat(foundItems.stream().map(TestItem::getId).toList()).containsOnly(1L, 2L);
}

@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());
public void findByIdNotContaining() {
this.repository.save(new TestItem(1L, "TestName1", "TestValue1", List.of("City1", "City2")));
this.repository.save(new TestItem(2L, "TestName2", "TestValue2", List.of("City1", "City3")));
this.repository.save(new TestItem(3L, "TestName3", "TestValue3", List.of("City2", "City3")));
List<TestItem> foundItems = this.repository.findAllByCitiesNotContaining("City1");
assertThat(foundItems.stream().map(TestItem::getId).toList()).containsOnly(3L);
}

@Test
public void findByIdNotContaining() {
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.findByIdNotContaining(expectedItems.stream()
List<TestItem> foundItems = this.repository.findByIdNotIn(expectedItems.stream()
.map(TestItem::getId)
.collect(Collectors.toList()));
assertEquals(0, foundItems.size());
Expand Down Expand Up @@ -1345,6 +1339,60 @@ public void findAllSqlLimit() {
assertEquals(90, expectedItems.size());
}

@Test
public void findAllByNameLike() {
this.repository.save(new TestItem(1L, "LIMITED", "TestValue1"));
this.repository.save(new TestItem(2L, "UNLIMITED", "TestValue2"));
this.repository.save(new TestItem(3L, "TestName", "TestValue3"));
List<TestItem> foundItems = this.repository.findAllByNameLike("%LIMITED");
assertThat(foundItems.stream().map(TestItem::getId).toList()).containsOnly(1L, 2L);
}

@Test
public void findAllByNameNotLike() {
this.repository.save(new TestItem(1L, "LIMITED", "TestValue1"));
this.repository.save(new TestItem(2L, "UNLIMITED", "TestValue2"));
this.repository.save(new TestItem(3L, "TestName", "TestValue3"));
List<TestItem> foundItems = this.repository.findAllByNameNotLike("%LIMITED");
assertThat(foundItems.stream().map(TestItem::getId).toList()).containsOnly(3L);
}

@Test
public void findAllByNameContaining() {
this.repository.save(new TestItem(1L, "LIMITED", "TestValue1"));
this.repository.save(new TestItem(2L, "UNLIMITED", "TestValue2"));
this.repository.save(new TestItem(3L, "TestName", "TestValue3"));
List<TestItem> foundItems = this.repository.findAllByNameContaining("LIMIT");
assertThat(foundItems.stream().map(TestItem::getId).toList()).containsOnly(1L, 2L);
}

@Test
public void findAllByNameNotContaining() {
this.repository.save(new TestItem(1L, "LIMITED", "TestValue1"));
this.repository.save(new TestItem(2L, "UNLIMITED", "TestValue2"));
this.repository.save(new TestItem(3L, "TestName", "TestValue3"));
List<TestItem> foundItems = this.repository.findAllByNameNotContaining("LIMIT");
assertThat(foundItems.stream().map(TestItem::getId).toList()).containsOnly(3L);
}

@Test
public void findAllByNameStartingWith() {
this.repository.save(new TestItem(1L, "LIMITED", "TestValue1"));
this.repository.save(new TestItem(2L, "UNLIMITED", "TestValue2"));
this.repository.save(new TestItem(3L, "TestName", "TestValue3"));
List<TestItem> foundItems = this.repository.findAllByNameStartingWith("Test");
assertThat(foundItems.stream().map(TestItem::getId).toList()).containsOnly(3L);
}

@Test
public void findAllByNameEndingWith() {
this.repository.save(new TestItem(1L, "LIMITED", "TestValue1"));
this.repository.save(new TestItem(2L, "UNLIMITED", "TestValue2"));
this.repository.save(new TestItem(3L, "TestName", "TestValue3"));
List<TestItem> foundItems = this.repository.findAllByNameEndingWith("ED");
assertThat(foundItems.stream().map(TestItem::getId).toList()).containsOnly(1L, 2L);
}

@Configuration
@EnableReindexerRepositories(basePackageClasses = TestItemReindexerRepository.class, considerNestedRepositories = true)
@EnableTransactionManagement
Expand Down Expand Up @@ -1467,11 +1515,11 @@ Optional<TestItem> findOneSqlByNameAndValueManyParams(String name1, String name2

List<TestItem> findByIdIn(long... ids);

List<TestItem> findByIdContaining(List<Long> ids);
List<TestItem> findAllByCitiesContaining(String city);

List<TestItem> findByIdNotIn(List<Long> ids);
List<TestItem> findAllByCitiesNotContaining(String city);

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

List<TestItem> findByTestEnumStringIn(List<TestEnum> values);

Expand Down Expand Up @@ -1564,6 +1612,18 @@ Optional<TestItem> findOneSqlByNameAndValueManyParams(String name1, String name2

@Query("SELECT * FROM items")
List<TestItem> findAllSqlLimit(Limit limit);

List<TestItem> findAllByNameLike(String pattern);

List<TestItem> findAllByNameNotLike(String pattern);

List<TestItem> findAllByNameContaining(String text);

List<TestItem> findAllByNameNotContaining(String text);

List<TestItem> findAllByNameStartingWith(String text);

List<TestItem> findAllByNameEndingWith(String text);
}

@Namespace(name = NAMESPACE_NAME)
Expand All @@ -1586,6 +1646,9 @@ public static class TestItem {
@Reindex(name = "testEnumOrdinal")
private TestEnum testEnumOrdinal;

@Reindex(name = "cities")
private List<String> cities = new ArrayList<>();

@Reindex(name = "active")
private boolean active;

Expand All @@ -1603,6 +1666,13 @@ public TestItem(Long id, boolean active) {
this.active = active;
}

public TestItem(Long id, String name, String value, List<String> cities) {
this.id = id;
this.name = name;
this.value = value;
this.cities = cities;
}

public TestItem(Long id, String name, String value, TestEnum testEnumString, TestEnum testEnumOrdinal) {
this.id = id;
this.name = name;
Expand Down Expand Up @@ -1651,6 +1721,14 @@ public void setTestEnumOrdinal(TestEnum testEnumOrdinal) {
this.testEnumOrdinal = testEnumOrdinal;
}

public List<String> getCities() {
return this.cities;
}

public void setCities(List<String> cities) {
this.cities = cities;
}

public boolean isActive() {
return this.active;
}
Expand All @@ -1666,12 +1744,13 @@ public boolean equals(Object o) {
}
TestItem testItem = (TestItem) o;
return Objects.equals(id, testItem.id) && Objects.equals(name, testItem.name) && Objects.equals(value, testItem.value)
&& testEnumString == testItem.testEnumString && testEnumOrdinal == testItem.testEnumOrdinal && active == testItem.active;
&& testEnumString == testItem.testEnumString && testEnumOrdinal == testItem.testEnumOrdinal && Objects.equals(cities, testItem.cities)
&& active == testItem.active;
}

@Override
public int hashCode() {
return Objects.hash(id, name, value, testEnumString, testEnumOrdinal, active);
return Objects.hash(id, name, value, testEnumString, testEnumOrdinal, cities, active);
}

@Override
Expand All @@ -1682,6 +1761,7 @@ public String toString() {
", value='" + this.value + '\'' +
", testEnumString=" + this.testEnumString +
", testEnumOrdinal=" + this.testEnumOrdinal +
", cities=" + this.cities +
", active=" + this.active +
'}';
}
Expand Down
Loading