@@ -26,6 +26,7 @@ import org.hibernate.search.FullTextQuery
26
26
import org.hibernate.search.FullTextSession
27
27
import org.hibernate.search.MassIndexer
28
28
import org.hibernate.search.Search
29
+ import org.hibernate.search.exception.EmptyQueryException ;
29
30
import org.hibernate.search.query.dsl.FieldCustomization
30
31
import org.hibernate.search.query.dsl.QueryBuilder
31
32
import org.slf4j.Logger ;
@@ -65,6 +66,38 @@ class HibernateSearchQueryBuilder {
65
66
children << component
66
67
}
67
68
69
+ /**
70
+ * @return true if composite contains at least one valid (not empty) query
71
+ */
72
+ protected boolean forEachQuery ( Closure action ) {
73
+
74
+ boolean notEmpty = false ;
75
+ if (children) {
76
+
77
+ for (child in children) {
78
+ try {
79
+
80
+ Query subQuery = child. createQuery();
81
+
82
+ action. delegate = subQuery
83
+ action. resolveStrategy = Closure . DELEGATE_FIRST
84
+ action. call(subQuery)
85
+ notEmpty = true ;
86
+
87
+ } catch (EmptyQueryException e) {
88
+ if (HibernateSearchGrailsPlugin . pluginConfig. throwExceptionOnEmptyQuery) {
89
+ throw e
90
+ } else {
91
+ log. warn ' empty Hibernate search query ignored! ' + child, e
92
+ }
93
+ }
94
+ }
95
+
96
+ }
97
+
98
+ return notEmpty
99
+ }
100
+
68
101
def toString ( indent ) {
69
102
[( " -" * indent ) + this . class. simpleName, children. collect { it. toString( indent + 1 ) }]. flatten(). findAll {it}. join( " \n " )
70
103
}
@@ -100,54 +133,47 @@ class HibernateSearchQueryBuilder {
100
133
101
134
private static class MustNotComponent extends Composite {
102
135
Query createQuery ( ) {
103
- if ( children ) {
104
136
105
- def query = queryBuilder. bool()
137
+ def query = queryBuilder. bool()
138
+ boolean notEmpty = forEachQuery { subQuery ->
139
+ query = query. must( subQuery ). not()
140
+ }
106
141
107
- children* . createQuery(). each {
108
- query = query. must( it ). not()
109
- }
110
-
111
- query. createQuery()
112
-
113
- } else {
114
- queryBuilder. all(). createQuery()
115
- }
142
+ if (notEmpty) {
143
+ return query. createQuery()
144
+ } else {
145
+ return queryBuilder. all(). createQuery()
146
+ }
147
+
116
148
}
117
149
}
118
150
private static class MustComponent extends Composite {
119
151
Query createQuery ( ) {
120
- if ( children ) {
121
-
122
- def query = queryBuilder. bool()
123
-
124
- children* . createQuery(). each {
125
- query = query. must( it )
126
- }
127
-
128
- query. createQuery()
129
-
130
- } else {
131
- queryBuilder. all(). createQuery()
132
- }
152
+ def query = queryBuilder. bool()
153
+ boolean notEmpty = forEachQuery { subQuery ->
154
+ query = query. must( subQuery )
155
+ }
156
+
157
+ if (notEmpty) {
158
+ return query. createQuery()
159
+ } else {
160
+ return queryBuilder. all(). createQuery()
161
+ }
133
162
}
134
163
}
135
164
136
165
private static class ShouldComponent extends Composite {
137
166
Query createQuery ( ) {
138
- if ( children ) {
139
-
140
- def query = queryBuilder. bool()
141
-
142
- children* . createQuery(). each {
143
- query = query. should( it )
144
- }
145
-
146
- query. createQuery()
147
-
148
- } else {
149
- queryBuilder. all(). createQuery()
150
- }
167
+ def query = queryBuilder. bool()
168
+ boolean notEmpty = forEachQuery { subQuery ->
169
+ query = query. should( subQuery )
170
+ }
171
+
172
+ if (notEmpty) {
173
+ return query. createQuery()
174
+ } else {
175
+ return queryBuilder. all(). createQuery()
176
+ }
151
177
}
152
178
}
153
179
@@ -219,6 +245,8 @@ class HibernateSearchQueryBuilder {
219
245
220
246
private static final List MASS_INDEXER_METHODS = MassIndexer . methods. findAll { it. returnType == MassIndexer }* . name
221
247
248
+ private final HibernateSearchConfig pluginConfig;
249
+
222
250
private final FullTextSession fullTextSession
223
251
private final clazz
224
252
private final instance
@@ -241,15 +269,16 @@ class HibernateSearchQueryBuilder {
241
269
242
270
Filter filter
243
271
244
- HibernateSearchQueryBuilder ( clazz , instance , Session session ) {
272
+ HibernateSearchQueryBuilder ( clazz , instance , Session session , HibernateSearchConfig pluginConfig ) {
245
273
this . clazz = clazz
246
274
this . fullTextSession = Search . getFullTextSession( session )
247
275
this . instance = instance
248
276
this . staticContext = instance == null
277
+ this . pluginConfig = pluginConfig;
249
278
}
250
279
251
- HibernateSearchQueryBuilder ( clazz , Session session ) {
252
- this ( clazz, null , session )
280
+ HibernateSearchQueryBuilder ( clazz , Session session , HibernateSearchConfig pluginConfig ) {
281
+ this ( clazz, null , session, pluginConfig )
253
282
}
254
283
255
284
private FullTextQuery createFullTextQuery ( ) {
0 commit comments