search "a b" "c d" should search "a b" OR "c d" #1828

Closed
opened 2026-02-05 02:00:36 +03:00 by OVERLORD · 1 comment
Owner

Originally created by @tomleesm on GitHub (Aug 17, 2020).

I run codes below in Laravel tinker

// dump SQL after run code
DB::listen(function ($query) { dump($query->sql); dump($query->bindings); dump('time: ' . $query->time . ' ms'); });

use BookStack\Entities\Page;
$page = new Page();
$query = $page->newQuery();

$query->where(function($query) {
  foreach(["a b", "c d"] as $inputTerm) {
    $query->where(function ($query) use ($inputTerm) {
      $query->where('name', 'like', '%' . $inputTerm . '%')
            ->orWhere('text', 'like', '%' . $inputTerm . '%');
    });
  }
})->get();

and then it generates SQL below

SELECT * FROM `pages`
WHERE (
    (`name` LIKE '%a b%' OR `text` LIKE '%a b%')
AND (`name` LIKE '%c d%' OR `text` LIKE '%c d%' )
)

but search "a b" "c d" should search "a b" OR "c d", it means SQL should be like this

SELECT * FROM `pages`
WHERE (
    (`name` LIKE '%a b%' OR `text` LIKE '%a b%')
OR  (`name` LIKE '%c d%' OR `text` LIKE '%c d%' )
)

so the code 87a5340a05/app/Entities/SearchService.php (L175) should change $query->where() to $query->orWhere() .

I do it, and tests pass. (ref 6288016356 )

May you check out, please ?

Originally created by @tomleesm on GitHub (Aug 17, 2020). I run codes below in Laravel tinker ``` php // dump SQL after run code DB::listen(function ($query) { dump($query->sql); dump($query->bindings); dump('time: ' . $query->time . ' ms'); }); use BookStack\Entities\Page; $page = new Page(); $query = $page->newQuery(); $query->where(function($query) { foreach(["a b", "c d"] as $inputTerm) { $query->where(function ($query) use ($inputTerm) { $query->where('name', 'like', '%' . $inputTerm . '%') ->orWhere('text', 'like', '%' . $inputTerm . '%'); }); } })->get(); ``` and then it generates SQL below ``` sql SELECT * FROM `pages` WHERE ( (`name` LIKE '%a b%' OR `text` LIKE '%a b%') AND (`name` LIKE '%c d%' OR `text` LIKE '%c d%' ) ) ``` but search "a b" "c d" should search "a b" OR "c d", it means SQL should be like this ``` sql SELECT * FROM `pages` WHERE ( (`name` LIKE '%a b%' OR `text` LIKE '%a b%') OR (`name` LIKE '%c d%' OR `text` LIKE '%c d%' ) ) ``` so the code https://github.com/BookStackApp/BookStack/blob/87a5340a0526d4f46d4da676ce44f4e26039d281/app/Entities/SearchService.php#L175 should change $query->where() to $query->orWhere() . I do it, and tests pass. (ref https://github.com/tomleesm/BookStack/pull/25/commits/6288016356d8dc9d85fb4651f485ef24dbb122b8 ) May you check out, please ?
Author
Owner

@ssddanbrown commented on GitHub (Sep 5, 2020):

Hi @tomleesm,
Thanks for the clear issue definition.

I think this may come more down to preference. Personally I think the currently implemented "AND" logic is more appropriate and useful. The same logic is applied for the filters and all types of search elements apart from unquoted normal query terms which are effectively grouped together and OR'd.

With this, A user search for content containing "a b" OR "c d" could make two searches if they wished. "a b" AND "c d" can be done in a single search.
If we switched to combining the search elements with OR, then the user would have no way of doing "a b" AND "c d" via the search system.

@ssddanbrown commented on GitHub (Sep 5, 2020): Hi @tomleesm, Thanks for the clear issue definition. I think this may come more down to preference. Personally I think the currently implemented "AND" logic is more appropriate and useful. The same logic is applied for the filters and all types of search elements apart from unquoted normal query terms which are effectively grouped together and OR'd. With this, A user search for content containing `"a b" OR "c d"` could make two searches if they wished. `"a b" AND "c d"` can be done in a single search. If we switched to combining the search elements with OR, then the user would have no way of doing `"a b" AND "c d"` via the search system.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/BookStack#1828