Subject: Re: bug#35588: [PATCH] ui: Search matches additional package outputs.

From: Chris Marusich <[email-masked]>
Date: Mon, 06 May 2019 17:57:46 -0700

Ludovic Courtès <[email-masked]> writes:

> Could we have just this hunk or is there something I’m missing that
> would make it insufficient?

That hunk alone is not enough, but I think the attached patch would do
the trick.  We just need to allow for the new possibility that the
"field" procedure returns a list of strings.

What do you think?

-- 
Chris
From c1150a217a416ef4ceccf87c56e36e8e921f873a Mon Sep 17 00:00:00 2001
From: Chris Marusich <[email-masked]>
Date: Mon, 6 May 2019 01:51:30 -0700
Subject: [PATCH] ui: Make package outputs searchable.

* guix/ui.scm (relevance): Allow the "field" procedure of a metric to
return a list, and handle that case appropriately.  Update docstring.
(%package-metrics): Add a metric for package outputs.
* guix/scripts/package.scm (find-packages-by-description): Update
docstring.

Co-authored-by: Tobias Geerinckx-Rice <[email-masked]>
---
 guix/scripts/package.scm |  6 +++---
 guix/ui.scm              | 24 +++++++++++++++++++-----
 2 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/guix/scripts/package.scm b/guix/scripts/package.scm
index aa27984ea2..06e4cf5b9c 100644
--- a/guix/scripts/package.scm
+++ b/guix/scripts/package.scm
@@ -180,9 +180,9 @@ hooks\" run when building the profile."
 ;;;
 
 (define (find-packages-by-description regexps)
-  "Return two values: the list of packages whose name, synopsis, or
-description matches at least one of REGEXPS sorted by relevance, and the list
-of relevance scores."
+  "Return two values: the list of packages whose name, synopsis, description,
+or output matches at least one of REGEXPS sorted by relevance, and the list of
+relevance scores."
   (let ((matches (fold-packages (lambda (package result)
                                   (if (package-superseded package)
                                       result
diff --git a/guix/ui.scm b/guix/ui.scm
index 92c845e944..b7ccd8312a 100644
--- a/guix/ui.scm
+++ b/guix/ui.scm
@@ -11,6 +11,8 @@
 ;;; Copyright © 2016 Benz Schenk <[email-masked]>
 ;;; Copyright © 2018 Kyle Meyer <[email-masked]>
 ;;; Copyright © 2018 Ricardo Wurmus <[email-masked]>
+;;; Copyright © 2019 Chris Marusich <[email-masked]>
+;;; Copyright © 2019 Tobias Geerinckx-Rice <[email-masked]>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -1370,9 +1372,9 @@ WIDTH columns.  EXTRA-FIELDS is a list of symbol/value pairs to emit."
 (define (relevance obj regexps metrics)
   "Compute a \"relevance score\" for OBJ as a function of its number of
 matches of REGEXPS and accordingly to METRICS.  METRICS is list of
-field/weight pairs, where FIELD is a procedure that returns a string
-describing OBJ, and WEIGHT is a positive integer denoting the weight of this
-field in the final score.
+field/weight pairs, where FIELD is a procedure that returns a string or list
+of strings describing OBJ, and WEIGHT is a positive integer denoting the
+weight of this field in the final score.
 
 A score of zero means that OBJ does not match any of REGEXPS.  The higher the
 score, the more relevant OBJ is to REGEXPS."
@@ -1394,8 +1396,11 @@ score, the more relevant OBJ is to REGEXPS."
             ((field . weight)
              (match (field obj)
                (#f  relevance)
-               (str (+ relevance
-                       (* (score str) weight)))))))
+               ((? string? str) (+ relevance
+                                   (* (score str) weight)))
+               ((? list? lst) (+ relevance
+                                 (* weight
+                                    (apply + (map score lst)))))))))
         0
         metrics))
 
@@ -1404,6 +1409,15 @@ score, the more relevant OBJ is to REGEXPS."
   ;; of regexps.
   `((,package-name . 4)
 
+    ;; Match against uncommon outputs.
+    (,(lambda (package)
+        (filter (lambda (output)
+                  (not (member output
+                               ;; Some common outpus shared by many packages.
+                               '("out" "debug" "doc" "static"))))
+                (package-outputs package)))
+     . 1)
+
     ;; Match regexps on the raw Texinfo since formatting it is quite expensive
     ;; and doesn't have much of an effect on search results.
     (,(lambda (package)
-- 
2.20.1