Skip to content

Commit f6122cb

Browse files
committed
Add support for FTS
Fixes #2085 Signed-off-by: Emilien Bevierre <emilien.bevierre@couchbase.com>
1 parent 0894112 commit f6122cb

11 files changed

Lines changed: 295 additions & 18 deletions

src/main/java/org/springframework/data/couchbase/core/ExecutableFindBySearchOperation.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.stream.Stream;
2222

2323
import org.springframework.dao.IncorrectResultSizeDataAccessException;
24+
import org.springframework.data.core.TypedPropertyPath;
2425
import org.springframework.data.couchbase.core.support.InCollection;
2526
import org.springframework.data.couchbase.core.support.InScope;
2627
import org.springframework.data.couchbase.core.support.WithSearchConsistency;
@@ -167,6 +168,9 @@ interface FindBySearchWithSkip<T> extends FindBySearchWithLimit<T> {
167168

168169
interface FindBySearchWithSort<T> extends FindBySearchWithSkip<T> {
169170
FindBySearchWithSkip<T> withSort(SearchSort... sort);
171+
172+
<P> FindBySearchWithSkip<T> withSort(TypedPropertyPath<P, ?> property,
173+
TypedPropertyPath<P, ?>... additionalProperties);
170174
}
171175

172176
interface FindBySearchWithHighlight<T> extends FindBySearchWithSort<T> {
@@ -175,6 +179,14 @@ interface FindBySearchWithHighlight<T> extends FindBySearchWithSort<T> {
175179
default FindBySearchWithSort<T> withHighlight(String... fields) {
176180
return withHighlight(HighlightStyle.SERVER_DEFAULT, fields);
177181
}
182+
183+
<P> FindBySearchWithSort<T> withHighlight(HighlightStyle style, TypedPropertyPath<P, ?> field,
184+
TypedPropertyPath<P, ?>... additionalFields);
185+
186+
default <P> FindBySearchWithSort<T> withHighlight(TypedPropertyPath<P, ?> field,
187+
TypedPropertyPath<P, ?>... additionalFields) {
188+
return withHighlight(HighlightStyle.SERVER_DEFAULT, field, additionalFields);
189+
}
178190
}
179191

180192
interface FindBySearchWithFacets<T> extends FindBySearchWithHighlight<T> {
@@ -183,6 +195,9 @@ interface FindBySearchWithFacets<T> extends FindBySearchWithHighlight<T> {
183195

184196
interface FindBySearchWithFields<T> extends FindBySearchWithFacets<T> {
185197
FindBySearchWithFacets<T> withFields(String... fields);
198+
199+
<P> FindBySearchWithFacets<T> withFields(TypedPropertyPath<P, ?> field,
200+
TypedPropertyPath<P, ?>... additionalFields);
186201
}
187202

188203
interface FindBySearchWithProjection<T> extends FindBySearchWithFields<T> {

src/main/java/org/springframework/data/couchbase/core/ExecutableFindBySearchOperationSupport.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020
import java.util.stream.Stream;
2121

22+
import org.springframework.data.core.TypedPropertyPath;
2223
import org.springframework.data.couchbase.core.ReactiveFindBySearchOperationSupport.ReactiveFindBySearchSupport;
2324
import org.springframework.data.couchbase.core.query.OptionsBuilder;
2425
import org.springframework.util.Assert;
@@ -186,8 +187,8 @@ public FindBySearchWithOptions<T> inCollection(final String collection) {
186187
@Override
187188
public FindBySearchWithQuery<T> withOptions(final SearchOptions options) {
188189
return new ExecutableFindBySearchSupport<>(template, domainType, returnType, indexName, searchRequest,
189-
scanConsistency, scope, collection, options != null ? options : this.options, sort,
190-
highlightStyle, highlightFields, facets, fields, limitSkip);
190+
scanConsistency, scope, collection, options != null ? options : this.options, sort, highlightStyle,
191+
highlightFields, facets, fields, limitSkip);
191192
}
192193

193194
@Override
@@ -215,11 +216,23 @@ public FindBySearchWithSkip<T> withSort(SearchSort... sort) {
215216
fields, limitSkip);
216217
}
217218

219+
@Override
220+
public <P> FindBySearchWithSkip<T> withSort(TypedPropertyPath<P, ?> property,
221+
TypedPropertyPath<P, ?>... additionalProperties) {
222+
return withSort(SearchPropertyPathSupport.toSearchSorts(template.getConverter(), property, additionalProperties));
223+
}
224+
218225
@Override
219226
public FindBySearchWithSort<T> withHighlight(HighlightStyle style, String... fields) {
220227
return new ExecutableFindBySearchSupport<>(template, domainType, returnType, indexName, searchRequest,
221-
scanConsistency, scope, collection, options, sort, style, fields, facets,
222-
this.fields, limitSkip);
228+
scanConsistency, scope, collection, options, sort, style, fields, facets, this.fields, limitSkip);
229+
}
230+
231+
@Override
232+
public <P> FindBySearchWithSort<T> withHighlight(HighlightStyle style, TypedPropertyPath<P, ?> field,
233+
TypedPropertyPath<P, ?>... additionalFields) {
234+
return withHighlight(style,
235+
SearchPropertyPathSupport.getMappedFieldPaths(template.getConverter(), field, additionalFields));
223236
}
224237

225238
@Override
@@ -235,5 +248,11 @@ public FindBySearchWithFacets<T> withFields(String... fields) {
235248
scanConsistency, scope, collection, options, sort, highlightStyle, highlightFields, facets,
236249
fields, limitSkip);
237250
}
251+
252+
@Override
253+
public <P> FindBySearchWithFacets<T> withFields(TypedPropertyPath<P, ?> field,
254+
TypedPropertyPath<P, ?>... additionalFields) {
255+
return withFields(SearchPropertyPathSupport.getMappedFieldPaths(template.getConverter(), field, additionalFields));
256+
}
238257
}
239258
}

src/main/java/org/springframework/data/couchbase/core/ReactiveFindBySearchOperation.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222

2323
import org.springframework.dao.IncorrectResultSizeDataAccessException;
24+
import org.springframework.data.core.TypedPropertyPath;
2425
import org.springframework.data.couchbase.core.support.InCollection;
2526
import org.springframework.data.couchbase.core.support.InScope;
2627
import org.springframework.data.couchbase.core.support.WithSearchConsistency;
@@ -192,6 +193,9 @@ interface FindBySearchWithSort<T> extends FindBySearchWithSkip<T> {
192193
* @param sort the sort specifications.
193194
*/
194195
FindBySearchWithSkip<T> withSort(SearchSort... sort);
196+
197+
<P> FindBySearchWithSkip<T> withSort(TypedPropertyPath<P, ?> property,
198+
TypedPropertyPath<P, ?>... additionalProperties);
195199
}
196200

197201
/**
@@ -214,6 +218,14 @@ interface FindBySearchWithHighlight<T> extends FindBySearchWithSort<T> {
214218
default FindBySearchWithSort<T> withHighlight(String... fields) {
215219
return withHighlight(HighlightStyle.SERVER_DEFAULT, fields);
216220
}
221+
222+
<P> FindBySearchWithSort<T> withHighlight(HighlightStyle style, TypedPropertyPath<P, ?> field,
223+
TypedPropertyPath<P, ?>... additionalFields);
224+
225+
default <P> FindBySearchWithSort<T> withHighlight(TypedPropertyPath<P, ?> field,
226+
TypedPropertyPath<P, ?>... additionalFields) {
227+
return withHighlight(HighlightStyle.SERVER_DEFAULT, field, additionalFields);
228+
}
217229
}
218230

219231
/**
@@ -238,6 +250,9 @@ interface FindBySearchWithFields<T> extends FindBySearchWithFacets<T> {
238250
* @param fields the field names.
239251
*/
240252
FindBySearchWithFacets<T> withFields(String... fields);
253+
254+
<P> FindBySearchWithFacets<T> withFields(TypedPropertyPath<P, ?> field,
255+
TypedPropertyPath<P, ?>... additionalFields);
241256
}
242257

243258
/**

src/main/java/org/springframework/data/couchbase/core/ReactiveFindBySearchOperationSupport.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
26+
import org.springframework.data.core.TypedPropertyPath;
2627
import org.springframework.data.couchbase.core.query.OptionsBuilder;
2728
import org.springframework.util.Assert;
2829

@@ -106,16 +107,16 @@ static class ReactiveFindBySearchSupport<T> implements ReactiveFindBySearch<T> {
106107
public TerminatingFindBySearch<T> matching(SearchRequest searchRequest) {
107108
Assert.notNull(searchRequest, "SearchRequest must not be null!");
108109
return new ReactiveFindBySearchSupport<>(template, domainType, returnType, indexName, searchRequest,
109-
scanConsistency, scope, collection, options, sort, highlightStyle, highlightFields, facets,
110-
fields, limitSkip, support);
110+
scanConsistency, scope, collection, options, sort, highlightStyle, highlightFields, facets, fields,
111+
limitSkip, support);
111112
}
112113

113114
@Override
114115
public FindBySearchWithProjection<T> withIndex(String indexName) {
115116
Assert.notNull(indexName, "Index name must not be null!");
116117
return new ReactiveFindBySearchSupport<>(template, domainType, returnType, indexName, searchRequest,
117-
scanConsistency, scope, collection, options, sort, highlightStyle, highlightFields, facets,
118-
fields, limitSkip, support);
118+
scanConsistency, scope, collection, options, sort, highlightStyle, highlightFields, facets, fields,
119+
limitSkip, support);
119120
}
120121

121122
@Override
@@ -129,8 +130,8 @@ public <R> FindBySearchWithFields<R> as(final Class<R> returnType) {
129130
@Override
130131
public FindBySearchInScope<T> withConsistency(SearchScanConsistency scanConsistency) {
131132
return new ReactiveFindBySearchSupport<>(template, domainType, returnType, indexName, searchRequest,
132-
scanConsistency, scope, collection, options, sort, highlightStyle, highlightFields, facets,
133-
fields, limitSkip, support);
133+
scanConsistency, scope, collection, options, sort, highlightStyle, highlightFields, facets, fields,
134+
limitSkip, support);
134135
}
135136

136137
@Override
@@ -150,8 +151,8 @@ public FindBySearchWithOptions<T> inCollection(final String collection) {
150151
@Override
151152
public FindBySearchWithQuery<T> withOptions(final SearchOptions options) {
152153
return new ReactiveFindBySearchSupport<>(template, domainType, returnType, indexName, searchRequest,
153-
scanConsistency, scope, collection, options != null ? options : this.options, sort,
154-
highlightStyle, highlightFields, facets, fields, limitSkip, support);
154+
scanConsistency, scope, collection, options != null ? options : this.options, sort, highlightStyle,
155+
highlightFields, facets, fields, limitSkip, support);
155156
}
156157

157158
@Override
@@ -179,11 +180,24 @@ public FindBySearchWithSkip<T> withSort(SearchSort... sort) {
179180
fields, limitSkip, support);
180181
}
181182

183+
@Override
184+
public <P> FindBySearchWithSkip<T> withSort(TypedPropertyPath<P, ?> property,
185+
TypedPropertyPath<P, ?>... additionalProperties) {
186+
return withSort(SearchPropertyPathSupport.toSearchSorts(template.getConverter(), property, additionalProperties));
187+
}
188+
182189
@Override
183190
public FindBySearchWithSort<T> withHighlight(HighlightStyle style, String... fields) {
184191
return new ReactiveFindBySearchSupport<>(template, domainType, returnType, indexName, searchRequest,
185-
scanConsistency, scope, collection, options, sort, style, fields, facets,
186-
this.fields, limitSkip, support);
192+
scanConsistency, scope, collection, options, sort, style, fields, facets, this.fields, limitSkip,
193+
support);
194+
}
195+
196+
@Override
197+
public <P> FindBySearchWithSort<T> withHighlight(HighlightStyle style, TypedPropertyPath<P, ?> field,
198+
TypedPropertyPath<P, ?>... additionalFields) {
199+
return withHighlight(style,
200+
SearchPropertyPathSupport.getMappedFieldPaths(template.getConverter(), field, additionalFields));
187201
}
188202

189203
@Override
@@ -200,6 +214,12 @@ public FindBySearchWithFacets<T> withFields(String... fields) {
200214
fields, limitSkip, support);
201215
}
202216

217+
@Override
218+
public <P> FindBySearchWithFacets<T> withFields(TypedPropertyPath<P, ?> field,
219+
TypedPropertyPath<P, ?>... additionalFields) {
220+
return withFields(SearchPropertyPathSupport.getMappedFieldPaths(template.getConverter(), field, additionalFields));
221+
}
222+
203223
@Override
204224
public Mono<T> one() {
205225
return all().singleOrEmpty();
@@ -223,7 +243,7 @@ public Flux<T> all() {
223243
return TransactionalSupport.verifyNotInTransaction("findBySearch")
224244
.thenMany(executeSearch()
225245
.flatMapMany(ReactiveSearchResult::rows))
226-
.concatMap(row -> hydrateRow(row))
246+
.concatMap(this::hydrateRow)
227247
.onErrorMap(throwable -> {
228248
if (throwable instanceof RuntimeException) {
229249
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
@@ -331,7 +351,6 @@ private Mono<T> hydrateRow(SearchRow row) {
331351
return Mono.empty();
332352
});
333353
}
334-
335354
private Mono<ReactiveSearchResult> executeSearch() {
336355
SearchOptions opts = buildSearchOptions();
337356
if (scope != null) {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2026-present the original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.couchbase.core;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
import org.springframework.data.core.TypedPropertyPath;
22+
import org.springframework.data.couchbase.core.convert.CouchbaseConverter;
23+
import org.springframework.data.couchbase.core.mapping.CouchbasePersistentProperty;
24+
import org.springframework.data.mapping.PersistentPropertyPath;
25+
26+
import com.couchbase.client.java.search.sort.SearchSort;
27+
28+
final class SearchPropertyPathSupport {
29+
30+
private SearchPropertyPathSupport() {
31+
}
32+
33+
static <P> String getMappedFieldPath(CouchbaseConverter converter, TypedPropertyPath<P, ?> property) {
34+
PersistentPropertyPath<CouchbasePersistentProperty> path = converter.getMappingContext()
35+
.getPersistentPropertyPath(property);
36+
return path.toDotPath(CouchbasePersistentProperty::getFieldName);
37+
}
38+
39+
@SafeVarargs
40+
static <P> String[] getMappedFieldPaths(CouchbaseConverter converter, TypedPropertyPath<P, ?> property,
41+
TypedPropertyPath<P, ?>... additionalProperties) {
42+
43+
List<String> fields = new ArrayList<>(additionalProperties.length + 1);
44+
fields.add(getMappedFieldPath(converter, property));
45+
46+
for (TypedPropertyPath<P, ?> additionalProperty : additionalProperties) {
47+
fields.add(getMappedFieldPath(converter, additionalProperty));
48+
}
49+
50+
return fields.toArray(String[]::new);
51+
}
52+
53+
@SafeVarargs
54+
static <P> SearchSort[] toSearchSorts(CouchbaseConverter converter, TypedPropertyPath<P, ?> property,
55+
TypedPropertyPath<P, ?>... additionalProperties) {
56+
57+
String[] fields = getMappedFieldPaths(converter, property, additionalProperties);
58+
SearchSort[] result = new SearchSort[fields.length];
59+
60+
for (int i = 0; i < fields.length; i++) {
61+
result[i] = SearchSort.byField(fields[i]);
62+
}
63+
64+
return result;
65+
}
66+
}

src/main/java/org/springframework/data/couchbase/repository/query/ReactiveSearchBasedCouchbaseQuery.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ private ReactiveFindBySearchOperation.FindBySearchWithConsistency<?> applyPagina
128128
}
129129
return searchOp;
130130
}
131-
132131
private static String resolveIndexName(ReactiveCouchbaseQueryMethod method) {
133132
SearchIndex methodAnnotation = method.getAnnotation(SearchIndex.class);
134133
if (methodAnnotation != null) {

src/main/java/org/springframework/data/couchbase/repository/query/SearchBasedCouchbaseQuery.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ private ExecutableFindBySearchOperation.FindBySearchWithConsistency<?> applyLimi
170170
: searchOp;
171171
return limit != null ? withSkipApplied.withLimit(limit) : withSkipApplied;
172172
}
173-
174173
private static String resolveIndexName(CouchbaseQueryMethod method) {
175174
SearchIndex methodAnnotation = method.getAnnotation(SearchIndex.class);
176175
if (methodAnnotation != null) {

src/test/java/org/springframework/data/couchbase/core/ExecutableFindBySearchOperationTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Map;
2323

2424
import org.junit.jupiter.api.Test;
25+
import org.springframework.data.core.TypedPropertyPath;
2526

2627
import com.couchbase.client.java.search.HighlightStyle;
2728
import com.couchbase.client.java.search.SearchOptions;
@@ -104,4 +105,20 @@ void highlightDefaultMethodUsesServerDefault() {
104105
// This is a compile-time check -- the default highlight method delegates to withHighlight(SERVER_DEFAULT, fields)
105106
assertNotNull(HighlightStyle.SERVER_DEFAULT);
106107
}
108+
109+
@Test
110+
void typedPropertyReferenceOverloadsAreDeclaredOnInterfaces() throws Exception {
111+
assertNotNull(ExecutableFindBySearchOperation.FindBySearchWithSort.class
112+
.getMethod("withSort", TypedPropertyPath.class, TypedPropertyPath[].class));
113+
assertNotNull(ExecutableFindBySearchOperation.FindBySearchWithHighlight.class
114+
.getMethod("withHighlight", HighlightStyle.class, TypedPropertyPath.class, TypedPropertyPath[].class));
115+
assertNotNull(ExecutableFindBySearchOperation.FindBySearchWithFields.class
116+
.getMethod("withFields", TypedPropertyPath.class, TypedPropertyPath[].class));
117+
assertNotNull(ReactiveFindBySearchOperation.FindBySearchWithSort.class
118+
.getMethod("withSort", TypedPropertyPath.class, TypedPropertyPath[].class));
119+
assertNotNull(ReactiveFindBySearchOperation.FindBySearchWithHighlight.class
120+
.getMethod("withHighlight", HighlightStyle.class, TypedPropertyPath.class, TypedPropertyPath[].class));
121+
assertNotNull(ReactiveFindBySearchOperation.FindBySearchWithFields.class
122+
.getMethod("withFields", TypedPropertyPath.class, TypedPropertyPath[].class));
123+
}
107124
}

0 commit comments

Comments
 (0)