Skip to content

Commit dfef043

Browse files
committed
HSEARCH-3319 Add base scope/scope provider to the engine
1 parent bfec61d commit dfef043

File tree

6 files changed

+210
-5
lines changed

6 files changed

+210
-5
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.engine.mapper.scope;
8+
9+
import java.util.function.Function;
10+
11+
import org.hibernate.search.engine.backend.scope.IndexScopeExtension;
12+
import org.hibernate.search.engine.common.EntityReference;
13+
import org.hibernate.search.engine.search.aggregation.AggregationKey;
14+
import org.hibernate.search.engine.search.aggregation.SearchAggregation;
15+
import org.hibernate.search.engine.search.aggregation.dsl.SearchAggregationFactory;
16+
import org.hibernate.search.engine.search.highlighter.dsl.SearchHighlighterFactory;
17+
import org.hibernate.search.engine.search.predicate.dsl.SearchPredicateFactory;
18+
import org.hibernate.search.engine.search.projection.dsl.SearchProjectionFactory;
19+
import org.hibernate.search.engine.search.query.dsl.SearchQueryOptionsStep;
20+
import org.hibernate.search.engine.search.query.dsl.SearchQuerySelectStep;
21+
import org.hibernate.search.engine.search.query.dsl.SearchQueryWhereStep;
22+
import org.hibernate.search.engine.search.sort.dsl.SearchSortFactory;
23+
import org.hibernate.search.util.common.SearchException;
24+
25+
/**
26+
* Represents a set of types and the corresponding indexes.
27+
* <p>
28+
* The scope can be used for search, to build search-related objects (predicate, sort, projection, aggregation, ...),
29+
* or to define the targeted entities/indexes
30+
* when passing it to the search session.
31+
*
32+
* @param <E> A supertype of all types in this scope.
33+
* @param <ER> The type of entity reference used by the scope.
34+
*/
35+
public interface SearchScope<E, ER extends EntityReference> {
36+
37+
/**
38+
* Initiate the building of a search predicate.
39+
* <p>
40+
* The predicate will only be valid for search queries
41+
* created using this scope or another scope instance targeting the same indexes.
42+
* <p>
43+
* Note this method is only necessary if you do not want to use lambda expressions,
44+
* since you can {@link SearchQueryWhereStep#where(Function) define predicates with lambdas}
45+
* within the search query DSL,
46+
* removing the need to create separate objects to represent the predicates.
47+
*
48+
* @return A predicate factory.
49+
* @see SearchPredicateFactory
50+
*/
51+
SearchPredicateFactory predicate();
52+
53+
/**
54+
* Initiate the building of a search sort.
55+
* <p>
56+
* The sort will only be valid for search queries
57+
* created using this scope or another scope instance targeting the same indexes.
58+
* or a wider scope.
59+
* <p>
60+
* Note this method is only necessary if you do not want to use lambda expressions,
61+
* since you can {@link SearchQueryOptionsStep#sort(Function) define sorts with lambdas}
62+
* within the search query DSL,
63+
* removing the need to create separate objects to represent the sorts.
64+
*
65+
* @return A sort factory.
66+
* @see SearchSortFactory
67+
*/
68+
SearchSortFactory sort();
69+
70+
/**
71+
* Initiate the building of a search projection that will be valid for the indexes in this scope.
72+
* <p>
73+
* The projection will only be valid for search queries
74+
* created using this scope or another scope instance targeting the same indexes.
75+
* <p>
76+
* Note this method is only necessary if you do not want to use lambda expressions,
77+
* since you can {@link SearchQuerySelectStep#select(Function)} define projections with lambdas}
78+
* within the search query DSL,
79+
* removing the need to create separate objects to represent the projections.
80+
*
81+
* @return A projection factory.
82+
* @see SearchProjectionFactory
83+
*/
84+
SearchProjectionFactory<ER, E> projection();
85+
86+
/**
87+
* Initiate the building of a search aggregation that will be valid for the indexes in this scope.
88+
* <p>
89+
* The aggregation will only be usable in search queries
90+
* created using this scope or another scope instance targeting the same indexes.
91+
* <p>
92+
* Note this method is only necessary if you do not want to use lambda expressions,
93+
* since you can {@link SearchQueryOptionsStep#aggregation(AggregationKey, SearchAggregation)} define aggregations with lambdas}
94+
* within the search query DSL,
95+
* removing the need to create separate objects to represent the aggregation.
96+
*
97+
* @return An aggregation factory.
98+
* @see SearchAggregationFactory
99+
*/
100+
SearchAggregationFactory aggregation();
101+
102+
/**
103+
* Initiate the building of a highlighter that will be valid for the indexes in this scope.
104+
* <p>
105+
* The highlighter will only be valid for search queries
106+
* created using this scope or another scope instance targeting the same indexes.
107+
* <p>
108+
* Note this method is only necessary if you do not want to use lambda expressions,
109+
* since you can {@link SearchQueryOptionsStep#highlighter(Function) define highlighters with lambdas}
110+
* within the search query DSL,
111+
* removing the need to create separate objects to represent the projections.
112+
*
113+
* @return A highlighter factory.
114+
*/
115+
SearchHighlighterFactory highlighter();
116+
117+
/**
118+
* Extend the current search scope with the given extension,
119+
* resulting in an extended search scope offering backend-specific utilities.
120+
*
121+
* @param extension The extension to apply.
122+
* @param <T> The type of search scope provided by the extension.
123+
* @return The extended search scope.
124+
* @throws SearchException If the extension cannot be applied (wrong underlying technology, ...).
125+
*/
126+
<T> T extension(IndexScopeExtension<T> extension);
127+
128+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Hibernate Search, full-text search for your domain model
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.search.engine.mapper.scope;
8+
9+
import java.util.Collection;
10+
import java.util.Collections;
11+
12+
import org.hibernate.search.engine.common.EntityReference;
13+
14+
/**
15+
* A provider of {@link SearchScope} instances.
16+
*/
17+
public interface SearchScopeProvider<ER extends EntityReference> {
18+
19+
/**
20+
* Creates a {@link SearchScope} limited to
21+
* indexed entity types among the given class and its subtypes.
22+
*
23+
* @param clazz A class that must be an indexed entity type or a supertype of such type.
24+
* @param <T> A supertype of all indexed entity types to include in the scope.
25+
* @return The created scope.
26+
* @see SearchScope
27+
*/
28+
default <T> SearchScope<T, ER> scope(Class<T> clazz) {
29+
return scope( Collections.singleton( clazz ) );
30+
}
31+
32+
/**
33+
* Creates a {@link SearchScope} limited to
34+
* indexed entity types among the given classes and their subtypes.
35+
*
36+
* @param classes A collection of classes.
37+
* Each must be an indexed entity type or a supertype of such type.
38+
* @param <T> A supertype of all indexed entity types to include in the scope.
39+
* @return The created scope.
40+
* @see SearchScope
41+
*/
42+
<T> SearchScope<T, ER> scope(Collection<? extends Class<? extends T>> classes);
43+
44+
/**
45+
* Creates a {@link SearchScope} limited to
46+
* indexed entity types among the entity with the given name and its subtypes.
47+
*
48+
* @param expectedSuperType A supertype of all entity types to include in the scope.
49+
* @param entityName An entity name.
50+
* The referenced entity type must be an indexed entity type or a supertype of such type.
51+
* @param <T> A supertype of all indexed entity types to include in the scope.
52+
* @return The created scope.
53+
* @see SearchScope
54+
*/
55+
default <T> SearchScope<T, ER> scope(Class<T> expectedSuperType, String entityName) {
56+
return scope( expectedSuperType, Collections.singleton( entityName ) );
57+
}
58+
59+
/**
60+
* Creates a {@link SearchScope} limited to
61+
* indexed entity types among the entities with the given names and their subtypes.
62+
*
63+
* @param expectedSuperType A supertype of all indexed entity types to include in the scope.
64+
* @param entityNames A collection of entity names.
65+
* Each entity type referenced in the collection must be an indexed entity type or a supertype of such type.
66+
* @param <T> A supertype of all indexed entity types to include in the scope.
67+
* @return The created scope.
68+
* @see SearchScope
69+
*/
70+
<T> SearchScope<T, ER> scope(Class<T> expectedSuperType, Collection<String> entityNames);
71+
72+
}

mapper/orm/src/main/java/org/hibernate/search/mapper/orm/scope/SearchScope.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
*
3737
* @param <E> A supertype of all types in this scope.
3838
*/
39-
public interface SearchScope<E> {
39+
@SuppressWarnings("deprecation")
40+
public interface SearchScope<E> extends org.hibernate.search.engine.mapper.scope.SearchScope<E, org.hibernate.search.mapper.orm.common.EntityReference> {
4041

4142
/**
4243
* Initiate the building of a search predicate.

mapper/orm/src/main/java/org/hibernate/search/mapper/orm/scope/SearchScopeProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.Collection;
88
import java.util.Collections;
99

10+
import org.hibernate.search.engine.common.EntityReference;
11+
1012
import jakarta.persistence.Entity;
1113

1214
/**
@@ -15,7 +17,8 @@
1517
* @see org.hibernate.search.mapper.orm.mapping.SearchMapping
1618
* @see org.hibernate.search.mapper.orm.session.SearchSession
1719
*/
18-
public interface SearchScopeProvider {
20+
@SuppressWarnings( "deprecation" )
21+
public interface SearchScopeProvider extends org.hibernate.search.engine.mapper.scope.SearchScopeProvider<org.hibernate.search.mapper.orm.common.EntityReference>{
1922

2023
/**
2124
* Creates a {@link SearchScope} limited to

mapper/pojo-standalone/src/main/java/org/hibernate/search/mapper/pojo/standalone/scope/SearchScope.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* @param <E> A supertype of all types in this scope.
4141
*/
4242
@Incubating
43-
public interface SearchScope<E> {
43+
public interface SearchScope<E> extends org.hibernate.search.engine.mapper.scope.SearchScope<E, EntityReference> {
4444

4545
/**
4646
* Initiate the building of a search predicate.
@@ -88,7 +88,7 @@ public interface SearchScope<E> {
8888
* @return A projection factory.
8989
* @see SearchProjectionFactory
9090
*/
91-
SearchProjectionFactory<EntityReference, ?> projection();
91+
SearchProjectionFactory<EntityReference, E> projection();
9292

9393
/**
9494
* Initiate the building of a search aggregation that will be valid for the indexes in this scope.

mapper/pojo-standalone/src/main/java/org/hibernate/search/mapper/pojo/standalone/scope/SearchScopeProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Collection;
88
import java.util.Collections;
99

10+
import org.hibernate.search.engine.common.EntityReference;
1011
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.SearchEntity;
1112
import org.hibernate.search.util.common.annotation.Incubating;
1213

@@ -17,7 +18,7 @@
1718
* @see org.hibernate.search.mapper.pojo.standalone.session.SearchSession
1819
*/
1920
@Incubating
20-
public interface SearchScopeProvider {
21+
public interface SearchScopeProvider extends org.hibernate.search.engine.mapper.scope.SearchScopeProvider<EntityReference> {
2122

2223
/**
2324
* Creates a {@link SearchScope} limited to

0 commit comments

Comments
 (0)