emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* agenda-view: match tags: Sorting by number of tags matched?
@ 2020-06-03 10:39 Budiman Snowman
  2020-06-04  4:30 ` Kyle Meyer
  0 siblings, 1 reply; 3+ messages in thread
From: Budiman Snowman @ 2020-06-03 10:39 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1385 bytes --]

Suppose I have this document:

* topic1                                               :tag1:tag2:tag3:
* topic2                                               :tag1:tag2:tag4:
* topic3                                               :tag1:tag3:tag4:
* topic4                                               :tag2:tag3:tag4:
* topic5                                               :tag1:tag2:
* topic6                                               :tag1:tag4:
* topic7                                               :tag3:tag4:
* topic8                                               :tag1:

And I turn on org-agenda m, then search for tag1|tag3|tag4. Emacs will then
show the results (all entries match) in the above order. Is there a way for
Emacs to show based on the number of tags matched ("sort based on
relevance"), e.g.:

* topic2                                               :tag1:tag2:tag4:
* topic1                                               :tag1:tag2:tag3:
* topic3                                               :tag1:tag3:tag4:
* topic4                                               :tag2:tag3:tag4:
* topic6                                               :tag1:tag4:
* topic5                                               :tag1:tag2:
* topic7                                               :tag3:tag4:
* topic8                                               :tag1:

Regards,
BS

[-- Attachment #2: Type: text/html, Size: 1985 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: agenda-view: match tags: Sorting by number of tags matched?
  2020-06-03 10:39 agenda-view: match tags: Sorting by number of tags matched? Budiman Snowman
@ 2020-06-04  4:30 ` Kyle Meyer
  2020-06-04 15:22   ` Budiman Snowman
  0 siblings, 1 reply; 3+ messages in thread
From: Kyle Meyer @ 2020-06-04  4:30 UTC (permalink / raw)
  To: Budiman Snowman, emacs-orgmode

Budiman Snowman writes:

> Suppose I have this document:
>
> * topic1                                               :tag1:tag2:tag3:
> * topic2                                               :tag1:tag2:tag4:
> * topic3                                               :tag1:tag3:tag4:
> * topic4                                               :tag2:tag3:tag4:
> * topic5                                               :tag1:tag2:
> * topic6                                               :tag1:tag4:
> * topic7                                               :tag3:tag4:
> * topic8                                               :tag1:
>
> And I turn on org-agenda m, then search for tag1|tag3|tag4. Emacs will then
> show the results (all entries match) in the above order.

org-agenda-sorting-strategy is the main option that controls sorting in
the agenda.  Going through the values there, I don't see anything
fitting your description.  But there is a user-defined-{up,down} option
that lets you implement your own logic via org-agenda-cmp-user-defined.
Here's an example.  It's probably brittle and inefficient in a number of
ways (and maybe completely wrong), but hopefully it gives you something
to start with.

  (add-to-list 'org-agenda-sorting-strategy
               '(tags user-defined-down))
  
  (defun my/org-agenda-cmp-num-tags-matched (a b)
    (when (and (eq (car org-agenda-redo-command) 'org-tags-view)
               org-agenda-query-string
               (string-match-p "|" org-agenda-query-string))
      (let* ((query-tags (split-string org-agenda-query-string "|"))
             (count (lambda (entry)
                      (length
                       (cl-intersection query-tags
                                        (get-text-property 0 'tags entry)
                                        :test #'equal))))
             (a-nmatched (funcall count a))
             (b-nmatched (funcall count b)))
        (cond
         ((> a-nmatched b-nmatched) 1)
         ((< a-nmatched b-nmatched) -1)))))
  
  (setq org-agenda-cmp-user-defined #'my/org-agenda-cmp-num-tags-matched)

> Is there a way for Emacs to show based on the number of tags matched
> ("sort based on relevance"), e.g.:
>
> * topic2                                               :tag1:tag2:tag4:
> * topic1                                               :tag1:tag2:tag3:
> * topic3                                               :tag1:tag3:tag4:
> * topic4                                               :tag2:tag3:tag4:
> * topic6                                               :tag1:tag4:
> * topic5                                               :tag1:tag2:
> * topic7                                               :tag3:tag4:
> * topic8                                               :tag1:

Hmm, your example query is "tag1|tag3|tag4", so a few of those lines
seem off.  For example, shouldn't topic 3 be at the top?  Anyway,
perhaps I'm misunderstanding your goal, but you should be able to adapt
the above approach to what you want.

Here are the results with the above custom sorting strategy and your
example query:

  topic3                                                            :tag1:tag3:tag4:
  topic1                                                            :tag1:tag2:tag3:
  topic2                                                            :tag1:tag2:tag4:
  topic4                                                            :tag2:tag3:tag4:
  topic6                                                                 :tag1:tag4:
  topic7                                                                 :tag3:tag4:
  topic5                                                                 :tag1:tag2:
  topic8                                                                      :tag1:


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: agenda-view: match tags: Sorting by number of tags matched?
  2020-06-04  4:30 ` Kyle Meyer
@ 2020-06-04 15:22   ` Budiman Snowman
  0 siblings, 0 replies; 3+ messages in thread
From: Budiman Snowman @ 2020-06-04 15:22 UTC (permalink / raw)
  To: Kyle Meyer; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2204 bytes --]

On Thu, Jun 4, 2020 at 11:31 AM Kyle Meyer <kyle@kyleam.com> wrote:

> Budiman Snowman writes:
>
> > Suppose I have this document:
> >
> > * topic1                                               :tag1:tag2:tag3:
> > * topic2                                               :tag1:tag2:tag4:
> > * topic3                                               :tag1:tag3:tag4:
> > * topic4                                               :tag2:tag3:tag4:
> > * topic5                                               :tag1:tag2:
> > * topic6                                               :tag1:tag4:
> > * topic7                                               :tag3:tag4:
> > * topic8                                               :tag1:
> >
> > And I turn on org-agenda m, then search for tag1|tag3|tag4. Emacs will
> then
> > show the results (all entries match) in the above order.
>
> org-agenda-sorting-strategy is the main option that controls sorting in
> the agenda.  Going through the values there, I don't see anything
> fitting your description.  But there is a user-defined-{up,down} option
> that lets you implement your own logic via org-agenda-cmp-user-defined.
> Here's an example.  It's probably brittle and inefficient in a number of
> ways (and maybe completely wrong), but hopefully it gives you something
> to start with.
>
<snip>

Thanks! I'll play with it later.

> * topic2                                               :tag1:tag2:tag4:
> > * topic1                                               :tag1:tag2:tag3:
> > * topic3                                               :tag1:tag3:tag4:
> > * topic4                                               :tag2:tag3:tag4:
> > * topic6                                               :tag1:tag4:
> > * topic5                                               :tag1:tag2:
> > * topic7                                               :tag3:tag4:
> > * topic8                                               :tag1:
>
> Hmm, your example query is "tag1|tag3|tag4", so a few of those lines
> seem off.  For example, shouldn't topic 3 be at the top?


Ah yes, I wrote incorrectly, I thought I wrote the query "tag1|tag2|tag4".
You understood my goal perfectly.

Regards,
BS

[-- Attachment #2: Type: text/html, Size: 3302 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-06-04 15:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-03 10:39 agenda-view: match tags: Sorting by number of tags matched? Budiman Snowman
2020-06-04  4:30 ` Kyle Meyer
2020-06-04 15:22   ` Budiman Snowman

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).