Skip to content

Commit

Permalink
Add text queries with ekg-show-notes-for-query via new triples-fts (#197
Browse files Browse the repository at this point in the history
)

Also, set version to 0.7.0.
  • Loading branch information
ahyatt authored Jan 28, 2025
1 parent aed37eb commit 8aabfa6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 16 deletions.
1 change: 1 addition & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Global commands, can be run everywhere, and most should be bound to useful keybi
| =ekg-show-notes-with-tag= | Open a buffer for notes matching the single tag given |
| =ekg-show-notes-with-any-tags= | Open a buffer for notes matching any of the given tags |
| =ekg-show-notes-with-all-tags= | Open a buffer for notes matching all of the given tags |
| ekg-show-notes-for-query | Open a buffer for notes with text matching query |
| =ekg-show-notes-with-tag-prefix= | Open a buffer for notes matching any tag with a tag with the given prefix |
| =ekg-show-notes-in-trash= | Open a tag buffer that shows all notes in the trash |
| =ekg-show-notes-in-drafts= | Open a tag buffer that shows all draft notes (saved but not finalized) |
Expand Down
5 changes: 4 additions & 1 deletion doc/ekg.org
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ triples library is already installed.
(require 'ekg)
#+end_src
* Changelog
** Version 0.6.5
** Version 0.7.0
- Added =ekg-show-notes-for-query= using =triples-fts=.
- Speed up for LLM calls with notes as context
** Version 0.6.4
- Fix display of extended structured data in notes.
Expand Down Expand Up @@ -545,6 +546,8 @@ This is a =ekg-notes-mode= buffer.

=ekg-show-notes-with-all-tags= will show all notes that have all of the tags given.

=ekg-show-notes-for-query= will do a ranked text search, showing all results. The query is token-based so querying for individual characters and syntax will not work; the query has to be words. Prefixes such as "tag", "title" and "text" can be used to restrict matches to tags, titles and text respectively. For example, the query "tag:apartment sink" will show any article with "sink" in it (it could be anywhere, even a tag), but it also has to have a tag with "apartment" in it. Every term is ANDed together. Use quotes if you want to quote a phrase such as "tag:\"apartment problems\" sink". This does not search anything included via inline commands. This is only possible for those using the built-in sqlite backend in Emacs 29.1 or later.

=ekg-show-notes-for-today= will show the notes taken today.

=ekg-show-notes-latest-captured= will show a number of notes from newest to
Expand Down
12 changes: 8 additions & 4 deletions doc/ekg.texi
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Installation
Changelog
* Version 0.6.5: Version 065.
* Version 0.7.0: Version 070.
* Version 0.6.4: Version 064.
* Version 0.6.3: Version 063.
* Version 0.6.2: Version 062.
Expand Down Expand Up @@ -205,7 +205,7 @@ triples library is already installed.
@chapter Changelog

@menu
* Version 0.6.5: Version 065.
* Version 0.7.0: Version 070.
* Version 0.6.4: Version 064.
* Version 0.6.3: Version 063.
* Version 0.6.2: Version 062.
Expand All @@ -225,11 +225,13 @@ triples library is already installed.
* Version 0.2: Version 02.
@end menu

@node Version 065
@section Version 0.6.5
@node Version 070
@section Version 0.7.0

@itemize
@item
Added @samp{ekg-show-notes-for-query} using @samp{triples-fts}.
@item
Speed up for LLM calls with notes as context
@end itemize

Expand Down Expand Up @@ -941,6 +943,8 @@ This is a @samp{ekg-notes-mode} buffer.

@samp{ekg-show-notes-with-all-tags} will show all notes that have all of the tags given.

@samp{ekg-show-notes-for-query} will do a ranked text search, showing all results. The query is token-based so querying for individual characters and syntax will not work; the query has to be words. Prefixes such as "tag", "title" and "text" can be used to restrict matches to tags, titles and text respectively. For example, the query "tag:apartment sink" will show any article with "sink" in it (it could be anywhere, even a tag), but it also has to have a tag with "apartment" in it. Every term is ANDed together. Use quotes if you want to quote a phrase such as "tag:\"apartment problems\" sink". This does not search anything included via inline commands. This is only possible for those using the built-in sqlite backend in Emacs 29.1 or later.

@samp{ekg-show-notes-for-today} will show the notes taken today.

@samp{ekg-show-notes-latest-captured} will show a number of notes from newest to
Expand Down
52 changes: 41 additions & 11 deletions ekg.el
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

;; Author: Andrew Hyatt <[email protected]>
;; Homepage: https://github.com/ahyatt/ekg
;; Package-Requires: ((triples "0.4.0") (emacs "28.1") (llm "0.18.0"))
;; Package-Requires: ((triples "0.5.0") (emacs "28.1") (llm "0.18.0"))
;; Keywords: outlines, hypermedia
;; Version: 0.6.4
;; Version: 0.7.0
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; This program is free software; you can redistribute it and/or
Expand All @@ -29,6 +29,7 @@
(require 'triples)
(require 'triples-backups)
(require 'triples-upgrade)
(require 'triples-fts)
(require 'seq)
(require 'ewoc)
(require 'cl-lib)
Expand Down Expand Up @@ -288,7 +289,12 @@ This includes new notes that start with tags. All functions are
called with the tag as the single argument, and run in the buffer
editing the note.")

(defconst ekg-version "0.6.4"
(defvar ekg-query-pred-abbrevs '(("tag" . "tagged/tag")
("title" . "titled/title")
("text" . "text/text"))
"Abbreviations for predicates in queries.")

(defconst ekg-version "0.7.0"
"The version of ekg.
This is used to understand when the database needs upgrading.")
Expand Down Expand Up @@ -1421,7 +1427,10 @@ If ID is given, force the triple subject to be that value."
(insert text)
(if (and (eq mode 'org-mode)
ekg-notes-display-images)
(org-redisplay-inline-images))
(condition-case nil
(org-redisplay-inline-images)
(wrong-type-argument (message "Problem showing image for note ID %s (content: \"%s\")"
id (ekg-truncate-at text 10)))))
(set-buffer-modified-p nil)
(pop-to-buffer buf)))

Expand Down Expand Up @@ -2073,6 +2082,25 @@ notes are created with additional tags TAGS."
(lambda () (sort (ekg-get-notes-with-tag tag) #'ekg-sort-by-creation-time))
(list tag)))

;;;###autoload
(defun ekg-show-notes-for-query (query)
"Show notes matching QUERY.
This does a token-based search, so single letters or word fragments and
punctuation may not match.
This uses `ekg-query-pred-abbrevs' to provide abbreviations for querying
for special forms of note data. For example, you can search for tags
with `tag:<term>' or `title:<term>', or `text:<term>'. Anything not
prefixed will search any text associated with the subject.
Text included in inline templates is not searched."
(interactive "sQuery: ")
(ekg-connect)
(ekg-setup-notes-buffer
(format "query: %s" query)
(lambda () (seq-filter #'ekg-note-active-p (mapcar #'ekg-get-note-with-id (triples-fts-query-subject ekg-db query ekg-query-pred-abbrevs)))) nil))

;;;###autoload
(defun ekg-show-notes-in-trash ()
"Show notes that have only tags that are trashed."
Expand Down Expand Up @@ -2295,7 +2323,10 @@ backup settings, and will not delete any backups, regardless of
other settings. FROM-VERSION is the version of the database
before the upgrade, in list form. TO-VERSION is the version of
the database after the upgrade, in list form."
(let ((need-trash-upgrade
(let ((need-fts-upgrade
(or (null from-version)
(version-list-< from-version '(0 7 0))))
(need-trash-upgrade
(or (null from-version)
;; Version 0.5.0 changed how trashed tags are handled.
(version-list-< from-version '(0 5 0))))
Expand All @@ -2309,6 +2340,10 @@ the database after the upgrade, in list form."
(or (null from-version)
(version-list-< from-version '(0 6 3)))))
(ekg-connect)
(when (and (eq 'builtin triples-sqlite-interface) need-fts-upgrade)
(triples-fts-setup ekg-db))
(when need-fts-upgrade
(triples-fts-setup ekg-db))
;; In the future, we can separate out the backup from the upgrades.
(when need-type-removal-upgrade
(ekg-backup t)
Expand All @@ -2327,12 +2362,7 @@ the database after the upgrade, in list form."
;; Also, we need to convert any text back to strings. We only need to do
;; this for the builtin sqlite, since that's the only case that
;; triples-upgrade-to-0.3 will do anything.
(when (eq 'builtin triples-sqlite-interface)
(if (fboundp 'sqlite-execute)
(sqlite-execute
ekg-db
"UPDATE OR IGNORE triples SET object = '\"' || CAST(object AS TEXT) || '\"' WHERE predicate = 'text/text' AND typeof(object) = 'integer'")
(error "The triples library incorrectly configured to use sqlite in 29.1, which is not available"))))
)
(when need-trash-upgrade
(ekg-backup t)
(let* ((trash-ids (ekg-tags-with-prefix "trash/"))
Expand Down

0 comments on commit 8aabfa6

Please sign in to comment.