Skip to content

Explore Search Results #3868

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

Closed
wants to merge 6 commits into from
Closed
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
22 changes: 18 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-SEARCH-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down Expand Up @@ -38,7 +38,8 @@
<jsqlparser>5.0</jsqlparser>
<mysql-connector-java>9.1.0</mysql-connector-java>
<postgresql>42.7.4</postgresql>
<springdata.commons>4.0.0-SNAPSHOT</springdata.commons>
<oracle>23.7.0.25.01</oracle>
<springdata.commons>4.0.0-SEARCH-RESULT-SNAPSHOT</springdata.commons>
<vavr>0.10.3</vavr>

<hibernate.groupId>org.hibernate</hibernate.groupId>
Expand All @@ -60,13 +61,13 @@
<dependency>
<groupId>com.github.mp911de.microbenchmark-runner</groupId>
<artifactId>microbenchmark-runner-junit5</artifactId>
<version>0.4.0.RELEASE</version>
<version>0.5.0.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jitpack.io</id>
<id>jitpack</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Expand Down Expand Up @@ -120,6 +121,19 @@
</includes>
</configuration>
</execution>
<execution>
<id>oracle-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/Oracle*IntegrationTests.java
</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-SEARCH-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-SEARCH-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-SEARCH-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
40 changes: 38 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-SEARCH-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -16,7 +16,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-SEARCH-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down Expand Up @@ -88,6 +88,12 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
Expand Down Expand Up @@ -161,6 +167,28 @@
<scope>test</scope>
</dependency>

<!-- Oracle testing support -->

<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc17</artifactId>
<version>${oracle}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ucp17</artifactId>
<version>${oracle}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>oracle-free</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
Expand All @@ -183,6 +211,13 @@
</exclusions>
</dependency>

<dependency>
<groupId>${hibernate.groupId}.orm</groupId>
<artifactId>hibernate-vector</artifactId>
<version>${hibernate}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>${hibernate.groupId}.orm</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
Expand Down Expand Up @@ -318,6 +353,7 @@
<exclude>**/EclipseLink*</exclude>
<exclude>**/MySql*</exclude>
<exclude>**/Postgres*</exclude>
<exclude>**/Oracle*</exclude>
</excludes>
<argLine>
-Xmx4G
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.benchmark.model.Person;
import org.springframework.data.jpa.benchmark.model.PersonDto;
import org.springframework.data.jpa.benchmark.model.Profile;
import org.springframework.data.jpa.benchmark.repository.PersonRepository;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
Expand Down Expand Up @@ -195,6 +196,12 @@ public List<Person> stringBasedQueryDynamicSort(BenchmarkParameters parameters)
Sort.by(COLUMN_PERSON_FIRSTNAME));
}

@Benchmark
public List<PersonDto> stringBasedQueryDynamicSortAndProjection(BenchmarkParameters parameters) {
return parameters.repositoryProxy.findAllWithAnnotatedQueryByFirstname(PERSON_FIRSTNAME,
Sort.by(COLUMN_PERSON_FIRSTNAME), PersonDto.class);
}

@Benchmark
public List<Person> stringBasedNativeQuery(BenchmarkParameters parameters) {
return parameters.repositoryProxy.findAllWithNativeQueryByFirstname(PERSON_FIRSTNAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.jpa.benchmark.model;

/**
* @author Mark Paluch
*/
public record PersonDto(String firstname, String lastname) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ public interface PersonRepository extends ListCrudRepository<Person, Integer> {
@Query("SELECT p FROM org.springframework.data.jpa.benchmark.model.Person p WHERE p.firstname = ?1")
List<Person> findAllWithAnnotatedQueryByFirstname(String firstname, Sort sort);

@Query("SELECT p FROM org.springframework.data.jpa.benchmark.model.Person p WHERE p.firstname = ?1")
<T> List<T> findAllWithAnnotatedQueryByFirstname(String firstname, Sort sort, Class<T> projection);

@Query(value = "SELECT * FROM person WHERE firstname = ?1", nativeQuery = true)
List<Person> findAllWithNativeQueryByFirstname(String firstname);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ private AotQuery createQuery(PartTree partTree, ReturnedType returnedType, JpaPa

ParameterMetadataProvider metadataProvider = new ParameterMetadataProvider(parameters, EscapeCharacter.DEFAULT,
templates);
JpaQueryCreator queryCreator = new JpaQueryCreator(partTree, returnedType, metadataProvider, templates, metamodel);
JpaQueryCreator queryCreator = new JpaQueryCreator(partTree, false, returnedType, metadataProvider, templates,
metamodel);

return StringAotQuery.jpqlQuery(queryCreator.createQuery(), metadataProvider.getBindings(),
partTree.getResultLimit(), partTree.isDelete(), partTree.isExistsProjection());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public AbstractJpaQuery(JpaQueryMethod method, EntityManager em) {
return new StreamExecution();
} else if (method.isProcedureQuery()) {
return new ProcedureExecution(method.isCollectionQuery());
} else if (method.isCollectionQuery()) {
} else if (method.isCollectionQuery() || method.isSearchQuery()) {
return new CollectionExecution();
} else if (method.isSliceQuery()) {
return new SlicedExecution();
Expand Down Expand Up @@ -140,17 +140,18 @@ protected JpaMetamodel getMetamodel() {

@Override
public @Nullable Object execute(Object[] parameters) {
return doExecute(getExecution(), parameters);

JpaParametersParameterAccessor accessor = obtainParameterAccessor(parameters);
return doExecute(getExecution(accessor), accessor);
}

/**
* @param execution
* @param values
* @return
*/
private @Nullable Object doExecute(JpaQueryExecution execution, Object[] values) {
private @Nullable Object doExecute(JpaQueryExecution execution, JpaParametersParameterAccessor accessor) {

JpaParametersParameterAccessor accessor = obtainParameterAccessor(values);
Object result = execution.execute(this, accessor);

ResultProcessor withDynamicProjection = method.getResultProcessor().withDynamicProjection(accessor);
Expand All @@ -167,10 +168,17 @@ private JpaParametersParameterAccessor obtainParameterAccessor(Object[] values)
return new JpaParametersParameterAccessor(method.getParameters(), values);
}

protected JpaQueryExecution getExecution() {
protected JpaQueryExecution getExecution(JpaParametersParameterAccessor accessor) {

JpaQueryExecution execution = this.execution.getNullable();

if (method.isSearchQuery()) {

ReturnedType returnedType = method.getResultProcessor().withDynamicProjection(accessor).getReturnedType();
return new JpaQueryExecution.SearchResultExecution(execution == null ? new SingleEntityExecution() : execution,
returnedType, accessor.getScoringFunction(), accessor.normalizeSimilarity());
}

if (execution != null) {
return execution;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class JpaCountQueryCreator extends JpaQueryCreator {
public JpaCountQueryCreator(PartTree tree, ReturnedType returnedType, ParameterMetadataProvider provider,
JpqlQueryTemplates templates, EntityManager em) {

super(tree, returnedType, provider, templates, em);
super(tree, returnedType, provider, templates, em.getMetamodel());

this.distinct = tree.isDistinct();
this.returnedType = returnedType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.List;
import java.util.Map;

import java.util.concurrent.atomic.AtomicInteger;

import org.jspecify.annotations.Nullable;

import org.springframework.data.domain.KeysetScrollPosition;
Expand All @@ -49,7 +51,7 @@ public JpaKeysetScrollQueryCreator(PartTree tree, ReturnedType type, ParameterMe
JpqlQueryTemplates templates, JpaEntityInformation<?, ?> entityInformation, KeysetScrollPosition scrollPosition,
EntityManager em) {

super(tree, type, provider, templates, em);
super(tree, type, provider, templates, em.getMetamodel());

this.entityInformation = entityInformation;
this.scrollPosition = scrollPosition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@
*/
package org.springframework.data.jpa.repository.query;

import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.jspecify.annotations.Nullable;

import org.springframework.data.domain.Range;
import org.springframework.data.domain.Score;
import org.springframework.data.domain.ScoringFunction;
import org.springframework.data.domain.Similarity;
import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
import org.springframework.data.repository.query.Parameter;
import org.springframework.data.repository.query.Parameters;
Expand Down Expand Up @@ -68,4 +76,54 @@ protected Object potentiallyUnwrap(Object parameterValue) {
return parameterValue;
}

/**
* Returns the {@link ScoringFunction}.
*
* @return
*/
public ScoringFunction getScoringFunction() {
return doWithScore(Score::getFunction, Score.class::isInstance, ScoringFunction::unspecified);
}

/**
* Returns whether to normalize similarities (i.e. translate the database-specific score into {@link Similarity}).
*
* @return
*/
public boolean normalizeSimilarity() {
return doWithScore(it -> true, Similarity.class::isInstance, () -> false);
}

/**
* Returns the {@link ScoringFunction}.
*
* @return
*/
public <T> T doWithScore(Function<Score, T> function, Predicate<Score> scoreFilter, Supplier<T> defaultValue) {

Score score = getScore();
if (score != null && scoreFilter.test(score)) {
return function.apply(score);
}

JpaParameters parameters = getParameters();
if (parameters.hasScoreRangeParameter()) {

Range<Score> range = getScoreRange();

if (range != null && range.getLowerBound().isBounded()
&& scoreFilter.test(range.getLowerBound().getValue().get())) {
return function.apply(range.getUpperBound().getValue().get());
}

if (range != null && range.getUpperBound().isBounded()
&& scoreFilter.test(range.getUpperBound().getValue().get())) {
return function.apply(range.getUpperBound().getValue().get());
}

}

return defaultValue.get();
}

}
Loading