@@ -133,48 +133,62 @@ protected function getPageOfDataFromQuery(EloquentBuilder $query, int $page = 1,
133133 protected function buildQuery (SearchOptions $ searchOpts , string $ entityType = 'page ' , string $ action = 'view ' ): EloquentBuilder
134134 {
135135 $ entity = $ this ->entityProvider ->get ($ entityType );
136- $ entitySelect = $ entity ->newQuery ();
136+ $ entityQuery = $ entity ->newQuery ();
137137
138138 // Handle normal search terms
139- if (count ($ searchOpts ->searches ) > 0 ) {
140- $ rawScoreSum = DB ::raw ('SUM(score) as score ' );
141- $ subQuery = DB ::table ('search_terms ' )->select ('entity_id ' , 'entity_type ' , $ rawScoreSum );
142- $ subQuery ->where ('entity_type ' , '= ' , $ entity ->getMorphClass ());
143- $ subQuery ->where (function (Builder $ query ) use ($ searchOpts ) {
144- foreach ($ searchOpts ->searches as $ inputTerm ) {
145- $ query ->orWhere ('term ' , 'like ' , $ inputTerm . '% ' );
146- }
147- })->groupBy ('entity_type ' , 'entity_id ' );
148- $ entitySelect ->join (DB ::raw ('( ' . $ subQuery ->toSql () . ') as s ' ), function (JoinClause $ join ) {
149- $ join ->on ('id ' , '= ' , 'entity_id ' );
150- })->addSelect ($ entity ->getTable () . '.* ' )
151- ->selectRaw ('s.score ' )
152- ->orderBy ('score ' , 'desc ' );
153- $ entitySelect ->mergeBindings ($ subQuery );
154- }
139+ $ this ->applyTermSearch ($ entityQuery , $ searchOpts ->searches , $ entity );
155140
156141 // Handle exact term matching
157142 foreach ($ searchOpts ->exacts as $ inputTerm ) {
158- $ entitySelect ->where (function (EloquentBuilder $ query ) use ($ inputTerm , $ entity ) {
143+ $ entityQuery ->where (function (EloquentBuilder $ query ) use ($ inputTerm , $ entity ) {
159144 $ query ->where ('name ' , 'like ' , '% ' . $ inputTerm . '% ' )
160145 ->orWhere ($ entity ->textField , 'like ' , '% ' . $ inputTerm . '% ' );
161146 });
162147 }
163148
164149 // Handle tag searches
165150 foreach ($ searchOpts ->tags as $ inputTerm ) {
166- $ this ->applyTagSearch ($ entitySelect , $ inputTerm );
151+ $ this ->applyTagSearch ($ entityQuery , $ inputTerm );
167152 }
168153
169154 // Handle filters
170155 foreach ($ searchOpts ->filters as $ filterTerm => $ filterValue ) {
171156 $ functionName = Str::camel ('filter_ ' . $ filterTerm );
172157 if (method_exists ($ this , $ functionName )) {
173- $ this ->$ functionName ($ entitySelect , $ entity , $ filterValue );
158+ $ this ->$ functionName ($ entityQuery , $ entity , $ filterValue );
174159 }
175160 }
176161
177- return $ this ->permissionService ->enforceEntityRestrictions ($ entity , $ entitySelect , $ action );
162+ return $ this ->permissionService ->enforceEntityRestrictions ($ entity , $ entityQuery , $ action );
163+ }
164+
165+ /**
166+ * For the given search query, apply the queries for handling the regular search terms.
167+ */
168+ protected function applyTermSearch (EloquentBuilder $ entityQuery , array $ terms , Entity $ entity ): void
169+ {
170+ if (count ($ terms ) === 0 ) {
171+ return ;
172+ }
173+
174+ $ subQuery = DB ::table ('search_terms ' )->select ([
175+ 'entity_id ' ,
176+ 'entity_type ' ,
177+ DB ::raw ('SUM(score) as score ' ),
178+ ]);
179+
180+ $ subQuery ->where ('entity_type ' , '= ' , $ entity ->getMorphClass ());
181+ $ subQuery ->where (function (Builder $ query ) use ($ terms ) {
182+ foreach ($ terms as $ inputTerm ) {
183+ $ query ->orWhere ('term ' , 'like ' , $ inputTerm . '% ' );
184+ }
185+ })->groupBy ('entity_type ' , 'entity_id ' );
186+ $ entityQuery ->join (DB ::raw ('( ' . $ subQuery ->toSql () . ') as s ' ), function (JoinClause $ join ) {
187+ $ join ->on ('id ' , '= ' , 'entity_id ' );
188+ })->addSelect ($ entity ->getTable () . '.* ' )
189+ ->selectRaw ('s.score ' )
190+ ->orderBy ('score ' , 'desc ' );
191+ $ entityQuery ->mergeBindings ($ subQuery );
178192 }
179193
180194 /**
0 commit comments