Browse Source

DATAMONGO-701 - Improve performance of starts-with and ends-with queries.

This changes the starts-with regex to the prefixed form using ^ to better make use of any index on the queried field. Also changes ending-with queries to use the $ anchor.
pull/43/merge
Andrew Duncan 13 years ago committed by Oliver Gierke
parent
commit
187c80dfcc
  1. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java
  2. 28
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java
  3. 4
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java
  4. 4
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/MongoQueryCreator.java

@ -269,10 +269,10 @@ class MongoQueryCreator extends AbstractQueryCreator<Query, Criteria> {
switch (type) { switch (type) {
case STARTING_WITH: case STARTING_WITH:
source = source + "*"; source = "^" + source;
break; break;
case ENDING_WITH: case ENDING_WITH:
source = "*" + source; source = source + "$";
break; break;
case CONTAINING: case CONTAINING:
source = "*" + source + "*"; source = "*" + source + "*";

28
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java

@ -53,11 +53,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
public abstract class AbstractPersonRepositoryIntegrationTests { public abstract class AbstractPersonRepositoryIntegrationTests {
@Autowired @Autowired protected PersonRepository repository;
protected PersonRepository repository;
@Autowired @Autowired MongoOperations operations;
MongoOperations operations;
Person dave, oliver, carter, boyd, stefan, leroi, alicia; Person dave, oliver, carter, boyd, stefan, leroi, alicia;
QPerson person; QPerson person;
@ -570,4 +568,26 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
public void executesAnnotatedCountProjection() { public void executesAnnotatedCountProjection() {
assertThat(repository.someCountQuery("Matthews"), is(2L)); assertThat(repository.someCountQuery("Matthews"), is(2L));
} }
/**
* @see DATAMONGO-701
*/
@Test
public void executesDerivedStartsWithQueryCorrectly() {
List<Person> result = repository.findByLastnameStartsWith("Matt");
assertThat(result, hasSize(2));
assertThat(result, hasItems(dave, oliver));
}
/**
* @see DATAMONGO-701
*/
@Test
public void executesDerivedEndsWithQueryCorrectly() {
List<Person> result = repository.findByLastnameEndsWith("thews");
assertThat(result, hasSize(2));
assertThat(result, hasItems(dave, oliver));
}
} }

4
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java

@ -47,6 +47,10 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
*/ */
List<Person> findByLastname(String lastname); List<Person> findByLastname(String lastname);
List<Person> findByLastnameStartsWith(String prefix);
List<Person> findByLastnameEndsWith(String postfix);
/** /**
* Returns all {@link Person}s with the given lastname ordered by their firstname. * Returns all {@link Person}s with the given lastname ordered by their firstname.
* *

4
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/MongoQueryCreatorUnitTests.java

@ -263,7 +263,7 @@ public class MongoQueryCreatorUnitTests {
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "Matt"), context); MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "Matt"), context);
Query query = creator.createQuery(); Query query = creator.createQuery();
assertThat(query, is(query(where("foo").regex("Matt.*")))); assertThat(query, is(query(where("foo").regex("^Matt"))));
} }
/** /**
@ -276,7 +276,7 @@ public class MongoQueryCreatorUnitTests {
MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "ews"), context); MongoQueryCreator creator = new MongoQueryCreator(tree, getAccessor(converter, "ews"), context);
Query query = creator.createQuery(); Query query = creator.createQuery();
assertThat(query, is(query(where("foo").regex(".*ews")))); assertThat(query, is(query(where("foo").regex("ews$"))));
} }
/** /**

Loading…
Cancel
Save