[aur-dev] [PATCH] Use keyword search by default
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org> --- web/lib/pkgfuncs.inc.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index c71358a..8fd629f 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -601,9 +601,11 @@ function pkg_search_page($SID="") { } else { /* Search by name and description (default). */ - $K = "%" . addcslashes($_GET['K'], '%_') . "%"; - $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . " OR "; - $q_where .= "Description LIKE " . $dbh->quote($K) . ") "; + foreach (str_getcsv($_GET['K'], ' ') as $term) { + $term = "%" . addcslashes($term, '%_') . "%"; + $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($term) . " OR "; + $q_where .= "Description LIKE " . $dbh->quote($term) . ") "; + } } } -- 2.4.1
On Fri, 22 May 2015 at 12:06:52, Lukas Fleischer wrote:
Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org> --- web/lib/pkgfuncs.inc.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) [...]
Forgot to mark this as RFC. What this patch does is change the default search mode such that the query foo bar "john doe" returns all packages that contain "foo", "bar" and "john doe" (either in the package name or in the description). We still don't support fuzzy matching but this is much better than what we use now. You can test this on aur-dev.archlinux.org. I will resubmit the patch with a proper commit message later.
Change the default search mode such that packages that contain all of the space-separated search terms are returned. For example, the query image edit "command line" returns all packages where "image", "edit" and "command line" occurs in the package name or description. This is much more convenient and general than a simple substring search (one can still perform a substring search by quoting the whole search term). Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org> --- web/lib/pkgfuncs.inc.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index c71358a..8fd629f 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -601,9 +601,11 @@ function pkg_search_page($SID="") { } else { /* Search by name and description (default). */ - $K = "%" . addcslashes($_GET['K'], '%_') . "%"; - $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($K) . " OR "; - $q_where .= "Description LIKE " . $dbh->quote($K) . ") "; + foreach (str_getcsv($_GET['K'], ' ') as $term) { + $term = "%" . addcslashes($term, '%_') . "%"; + $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($term) . " OR "; + $q_where .= "Description LIKE " . $dbh->quote($term) . ") "; + } } } -- 2.4.1
Specifying a huge number of search terms currently results in complex SQL queries. In practice, queries with more than 20 terms are rarely needed. Ignore everything apart from the first 20 keywords to prevent from potential abuse. Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org> --- web/lib/pkgfuncs.inc.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index 8fd629f..11ca591 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -601,10 +601,21 @@ function pkg_search_page($SID="") { } else { /* Search by name and description (default). */ + $count = 0; + foreach (str_getcsv($_GET['K'], ' ') as $term) { + if ($term == "") { + continue; + } + $term = "%" . addcslashes($term, '%_') . "%"; $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($term) . " OR "; $q_where .= "Description LIKE " . $dbh->quote($term) . ") "; + + $count++; + if ($count >= 20) { + break; + } } } } -- 2.4.1
On Fri 22 May 2015 17:29 +0200, Lukas Fleischer wrote:
Specifying a huge number of search terms currently results in complex SQL queries. In practice, queries with more than 20 terms are rarely needed. Ignore everything apart from the first 20 keywords to prevent from potential abuse.
Should that maybe be a configurable limit?
This adds very basic support for boolean search queries such as "video or movie" or "lin and not linux". However, nested queries such as "(video or movie) and editing" are not (yet) supported. Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org> --- web/lib/pkgfuncs.inc.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php index 11ca591..2b5afaa 100644 --- a/web/lib/pkgfuncs.inc.php +++ b/web/lib/pkgfuncs.inc.php @@ -602,21 +602,38 @@ function pkg_search_page($SID="") { else { /* Search by name and description (default). */ $count = 0; + $q_where .= "AND ("; + $op = ""; foreach (str_getcsv($_GET['K'], ' ') as $term) { if ($term == "") { continue; } + if ($count > 0 && strtolower($term) == "and") { + $op = "AND "; + continue; + } + if ($count > 0 && strtolower($term) == "or") { + $op = "OR "; + continue; + } + if ($count > 0 && strtolower($term) == "not") { + $op .= "NOT "; + continue; + } $term = "%" . addcslashes($term, '%_') . "%"; - $q_where .= "AND (Packages.Name LIKE " . $dbh->quote($term) . " OR "; + $q_where .= $op . " (Packages.Name LIKE " . $dbh->quote($term) . " OR "; $q_where .= "Description LIKE " . $dbh->quote($term) . ") "; $count++; if ($count >= 20) { break; } + $op = "AND "; } + + $q_where .= ") "; } } -- 2.4.1
participants (2)
-
Loui Chang
-
Lukas Fleischer