Skip to content

Fix criteria filter in native query. #2846

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

Merged
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 @@ -1169,8 +1169,7 @@ public MsearchTemplateRequest searchMsearchTemplateRequest(
}

return bb;
})
);
}));
});
return mtrb;
});
Expand Down Expand Up @@ -1721,8 +1720,13 @@ private void addFilter(Query query, SearchRequest.Builder builder) {
} else // noinspection StatementWithEmptyBody
if (query instanceof StringQuery) {
// no filter for StringQuery
} else if (query instanceof NativeQuery) {
builder.postFilter(((NativeQuery) query).getFilter());
} else if (query instanceof NativeQuery nativeQuery) {

if (nativeQuery.getFilter() != null) {
builder.postFilter(nativeQuery.getFilter());
} else if (nativeQuery.getSpringDataQuery() != null) {
addFilter(nativeQuery.getSpringDataQuery(), builder);
}
} else {
throw new IllegalArgumentException("unhandled Query implementation " + query.getClass().getName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.data.elasticsearch.annotations.FieldType;
import org.springframework.data.elasticsearch.client.elc.NativeQuery;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest;
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
Expand All @@ -50,7 +51,7 @@ public void before() {
@Test
@Order(java.lang.Integer.MAX_VALUE)
void cleanup() {
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + "*")).delete();
operations.indexOps(IndexCoordinates.of(indexNameProvider.getPrefix() + '*')).delete();
}

@Test // #2391
Expand All @@ -75,6 +76,28 @@ void shouldBeAbleToUseCriteriaQueryInANativeQuery() {
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity.getId());
}

@Test // #2840
@DisplayName("should be able to use CriteriaQuery with filter arguments in a NativeQuery")
void shouldBeAbleToUseCriteriaQueryWithFilterArgumentsInANativeQuery() {
var entity1 = new SampleEntity();
entity1.setId("60");
var location1 = new GeoPoint(60.0, 60.0);
entity1.setLocation(location1);
var entity2 = new SampleEntity();
entity2.setId("70");
var location70 = new GeoPoint(70.0, 70.0);
entity2.setLocation(location70);
operations.save(entity1, entity2);

var criteriaQuery = new CriteriaQuery(Criteria.where("location").within(location1, "10km"));
var nativeQuery = NativeQuery.builder().withQuery(criteriaQuery).build();

var searchHits = operations.search(nativeQuery, SampleEntity.class);

assertThat(searchHits.getTotalHits()).isEqualTo(1);
assertThat(searchHits.getSearchHit(0).getId()).isEqualTo(entity1.getId());
}

@Test // #2391
@DisplayName("should be able to use StringQuery in a NativeQuery")
void shouldBeAbleToUseStringQueryInANativeQuery() {
Expand Down Expand Up @@ -112,6 +135,14 @@ void shouldBeAbleToUseStringQueryInANativeQuery() {
@Document(indexName = "#{@indexNameProvider.indexName()}")
static class SampleEntity {

@Nullable
@Id private String id;

@Nullable
@Field(type = FieldType.Text) private String text;

@Nullable private GeoPoint location;

@Nullable
public String getId() {
return id;
Expand All @@ -121,6 +152,7 @@ public void setId(@Nullable String id) {
this.id = id;
}

@Nullable
public String getText() {
return text;
}
Expand All @@ -130,8 +162,12 @@ public void setText(String text) {
}

@Nullable
@Id private String id;
public GeoPoint getLocation() {
return location;
}

@Field(type = FieldType.Text) private String text;
public void setLocation(GeoPoint location) {
this.location = location;
}
}
}