filesearch

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2026 License: Apache-2.0 Imports: 25 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

filesearch

A demo of go-sqlite: a command-line tool which indexes folders and searches them. It reads each file's metadata, content and artwork with go-metadata, and stores them in a SQLite database.

It's its own Go module, so go-sqlite itself doesn't depend on go-metadata.

What it demonstrates

It uses the ncruces implementation, which is pure Go.

  • Opening a pool: ncruces.Open gives one writer and several readers, so searches don't wait for indexing. See manager.go.
  • Schema and saved queries: tables, and named queries, are in objects.sql and queries.sql. Migrations run in one transaction, by schema version. See schema.go.
  • Reading: volumes and files are Go types with Get and List, which run on the readers. See volume.go and object.go.
  • Writing: Insert, Update and Delete, with the values package for times, JSON and lists.
  • Transactions: files are indexed in batches of 100. Unchanged files are marked in one bulk transaction, which reuses its statements. Changed files are read outside any transaction, as that's slow, then written in another. Finishing a run and sweeping deleted files is one transaction. See index.go.
  • An extension: search uses SQLite's full-text search, FTS5, which is imported with _ "github.com/mutablelogic/go-sqlite/ncruces/fts5". Results are ranked with bm25, from 0 to 1.
  • Tracing: every operation is an OpenTelemetry span, when there's a tracer.

How it indexes

  • Volumes: a volume is a folder which is indexed.
  • Mark and sweep: indexing again reads only new or changed files. Unchanged files are marked, and files which have gone are swept.
  • Interrupted runs: each batch is committed, so CTRL+C keeps what's done. The next run carries on.
  • Reader: each volume remembers which release of filesearch and go-metadata indexed it, with which build tags, and whether TMDB was used. When that changes, all of its files are read again.

How it searches

  • Words are matched as prefixes, in titles, paths, metadata and content, in that order of weight.
  • Rank is from 0 to 1, higher for better matches. Results under 0.15 are hidden, unless --cutoff is set.
  • Filters: by volume, and by type, such as video, pdf or video/mp4.

Install

go install github.com/mutablelogic/go-sqlite/filesearch/cmd/filesearch@latest

This builds the pure Go handlers: documents, email, text and images. For audio, video, and photos' EXIF, HEIF and RAW, add cgo and the media tag:

CGO_ENABLED=1 go install -tags media github.com/mutablelogic/go-sqlite/filesearch/cmd/filesearch@latest

This needs go-metadata's C libraries: FFmpeg 8, libexif, libheif and LibRaw. On macOS, with Homebrew:

brew install ffmpeg@8 libexif libheif libraw
export PKG_CONFIG_PATH="$(brew --prefix ffmpeg@8)/lib/pkgconfig"
export CGO_CFLAGS_ALLOW="-Xpreprocessor" CGO_LDFLAGS_ALLOW="-Xpreprocessor"
CGO_ENABLED=1 go install -tags media github.com/mutablelogic/go-sqlite/filesearch/cmd/filesearch@latest
  • PKG_CONFIG_PATH: Homebrew doesn't link ffmpeg@8 into its prefix, as it's a versioned formula, so it isn't found otherwise.
  • CGO_*_ALLOW: Homebrew's LibRaw is built with OpenMP, whose -Xpreprocessor flag cgo rejects unless it's allowed.
  • The linker warns about duplicate libraries, which is harmless.

Build

From a clone of the repo:

make filesearch           # build/filesearch: documents, email, text and images, pure Go
make filesearch MEDIA=1   # also audio, video, and photos' EXIF, HEIF and RAW

MEDIA=1 needs cgo and go-metadata's C libraries: FFmpeg 8, libexif, libheif and LibRaw. On macOS, the Makefile finds Homebrew's ffmpeg@8, and allows LibRaw's flags, itself.

Run

filesearch index ~/Documents              # Index a folder, and its folders (--no-recursive not)
filesearch index                          # Index every volume again
filesearch index --reindex                # Read every file of every volume again
filesearch search tax return              # Search, best first
filesearch search --cutoff 0 tax          # Include weak matches
filesearch search --order date tax        # Newest first (also path, size, title)
filesearch search --type video            # List files of a type
filesearch volumes                        # List volumes, with their status and file counts
filesearch volume-delete 1                # Delete a volume, by id or path
filesearch info report.pdf                # What's indexed of a file
filesearch cat report.pdf                 # Its content, as Markdown
filesearch cat --artwork song.mp3 > a.jpg # Its artwork
  • Database: filesearch.db in the user's cache folder, or --db.
  • Files are named by full path, path within a volume, or file name. A name which matches more than one file is an error.
  • Excluded: hidden files, lock files, backups, temporary files, system files and dependency folders, unless --no-default-exclude. Exclude more with --exclude, such as --exclude '\.bak$'.
  • TMDB: with --tmdb-key, or TMDB_TOKEN, videos named as films or episodes get titles, dates, descriptions and posters from TMDB.

Use as a package

manager, err := filesearch.New(ctx, path, filesearch.WithTracer(tracer))
volume, err := manager.IndexVolume(ctx, schema.IndexVolumeRequest{Path: folder, Recursive: true}, progress)
list, err := manager.ListObjects(ctx, schema.ObjectListRequest{Query: types.Ptr("tax return")})
  • Options: WithTMDB, WithExclude, WithoutDefaultExclude, WithReader, WithMaxReaders and WithTracer.
  • Progress: IndexVolume calls its ProgressFn after each batch is committed, and for each file which can't be read. Progress(volume) returns how far a run has got.
  • Handlers: the program imports the go-metadata handlers it wants, such as _ "github.com/mutablelogic/go-metadata/text". See cli.go for the pure Go ones, and cli_media.go for those which need cgo.

Documentation

Overview

Package filesearch indexes the files of volumes, which are folders, of their metadata, content and artwork, and searches them, in a SQLite database, which it creates, and which is swept of what's gone, when a volume is indexed again.

Index

Constants

View Source
const (
	// MaxText is the most of a file's content, as Markdown, which is read,
	// and searched, in bytes
	MaxText = 256 * 1024

	// ArtworkSize is the largest width or height of a file's artwork, in
	// pixels
	ArtworkSize = 256
)

Variables

View Source
var DefaultExclude = []string{
	`^\.`,
	`^~\$`,
	`~$`,
	`^#.*#$`,
	`(?i)\.(tmp|temp|swp|swo|part|crdownload|download)$`,
	`(?i)^(thumbs\.db|desktop\.ini|ehthumbs\.db)$`,
	`(?i)^(\$recycle\.bin|system volume information|lost\+found)$`,
	`^(node_modules|__pycache__)$`,
	`\.pyc$`,
}

DefaultExclude are the names of files and folders which aren't indexed, unless WithoutDefaultExclude is an option: hidden files and folders, such as .git and .DS_Store, Office's lock files, editors' backups and autosaves, temporary files and partial downloads, the files and folders of Windows and of file systems, and the folders of dependencies and builds

Functions

This section is empty.

Types

type Manager

type Manager struct {

	// The progress of the volumes which are indexing, or were, by their paths
	sync.Mutex
	// contains filtered or unexported fields
}

Manager indexes and searches the files of volumes, in its database

func New

func New(ctx context.Context, path string, opts ...Opt) (_ *Manager, err error)

New returns a manager, of the database of the path, which is created when it doesn't exist, or sqlite.Memory, and creates its schema, when it hasn't it.

func (*Manager) Artwork

func (m *Manager) Artwork(ctx context.Context, id int64) (_ []byte, err error)

Artwork returns an object's artwork, such as a thumbnail or a cover, by its id, which is ErrNotFound when it hasn't any

func (*Manager) Close

func (m *Manager) Close() error

Close closes the database, once its connections are idle

func (*Manager) DeleteVolume

func (m *Manager) DeleteVolume(ctx context.Context, id int64) (_ *schema.Volume, err error)

DeleteVolume deletes a volume, by its id, with its objects, and their text, and returns it, which is ErrNotFound when there isn't one, and ErrConflict when this manager is indexing it

func (*Manager) GetObject

func (m *Manager) GetObject(ctx context.Context, path string) (_ *schema.Object, err error)

GetObject returns the object of a path: of its volume's path, and its own, or its own, or the end of it, after a /, such as a file's name. It's ErrNotFound when no object is, and ErrConflict, of their paths, when more than one is.

func (*Manager) GetVolume

func (m *Manager) GetVolume(ctx context.Context, id int64) (_ *schema.Volume, err error)

GetVolume returns a volume, by its id, with the status of its indexing, which is ErrNotFound when there isn't one

func (*Manager) IndexVolume

func (m *Manager) IndexVolume(ctx context.Context, req schema.IndexVolumeRequest, fn ProgressFn) (_ *schema.Volume, err error)

IndexVolume indexes a volume, of a folder, which is added, when it isn't one already. Its run walks it, of its folders, when the request is recursive, and marks each file which is as it was, of its size and when it was modified, and indexes those which are new, or changed. Once it's indexed, the objects which the run didn't see are swept. A file which can't be read isn't indexed, and its error is said to the function.

The function, when it isn't nil, is called with its progress, and the errors of files which can't be read, and when it's finished.

It returns the volume, which is indexed, or failed, with its error, such as when the context is done. A volume which this manager is indexing is ErrConflict.

func (*Manager) ListObjects

func (m *Manager) ListObjects(ctx context.Context, req schema.ObjectListRequest) (_ *schema.ObjectList, err error)

ListObjects lists the objects, of the volumes, or all of them, by their paths, or searches them, when there's a query, the best first, with the request, and the limit which was used, clamped to what's returned

func (*Manager) ListVolumes

func (m *Manager) ListVolumes(ctx context.Context, req schema.VolumeListRequest) (_ *schema.VolumeList, err error)

ListVolumes lists the volumes, by their paths, with the status of their indexing, with the request, and the limit which was used, clamped to what's returned

func (*Manager) Markdown

func (m *Manager) Markdown(ctx context.Context, id int64) (_ string, err error)

Markdown returns an object's content, as Markdown, by its id, which is read from its file, as it isn't kept, all of it, with go-metadata's handlers which are imported. It's ErrNotFound when the object isn't, and empty when the file has no content.

func (*Manager) ObjectPath

func (m *Manager) ObjectPath(ctx context.Context, object *schema.Object) (string, error)

ObjectPath returns the path of an object's file, of its volume's path, and its own

func (*Manager) Ping

func (m *Manager) Ping(ctx context.Context) error

Ping the database

func (*Manager) Progress

func (m *Manager) Progress(volume int64) (schema.Progress, error)

Progress returns how far the run of a volume has indexed it, while it's indexing, or how the last run of this manager did, once it's finished, which is ErrNotFound when this manager hasn't indexed it

func (*Manager) VolumeByPath

func (m *Manager) VolumeByPath(ctx context.Context, path string) (_ *schema.Volume, err error)

VolumeByPath returns a volume, by its path, of the folder which a symbolic link is to, which is ErrNotFound when there isn't one

type Opt

type Opt func(*opt) error

Opt is an option of a manager, when it's created

func WithExclude

func WithExclude(patterns ...string) Opt

WithExclude excludes the files, and folders, whose names match the regular expressions, such as `\.bak$`, as well as DefaultExclude. An excluded folder's files aren't indexed, and what's excluded which was indexed is swept, when its volume is indexed again.

func WithMaxReaders

func WithMaxReaders(n int) Opt

WithMaxReaders sets the most connections which read the database at once, such as to search it, while it's indexed

func WithReader

func WithReader(fingerprint string) Opt

WithReader sets what reads files, as a fingerprint, such as of the versions of filesearch and go-metadata, and the handlers which a program imports. A volume whose last complete run was of another reader, or of none, has all of its files indexed again, as they can be read differently, rather than those which are new, or changed.

func WithTMDB

func WithTMDB(client *tmdb.Client) Opt

WithTMDB looks up the movies and episodes on TMDB which videos are, of their names, with the client, so their titles, dates, descriptions and posters are TMDB's. A video is looked up when it's indexed, which is when it's new, or changed.

func WithTracer

func WithTracer(tracer trace.Tracer) Opt

WithTracer sets the OpenTelemetry tracer, of the spans of the manager, and of its database's queries

func WithoutDefaultExclude

func WithoutDefaultExclude() Opt

WithoutDefaultExclude doesn't exclude DefaultExclude, so hidden files, and the others, are indexed, unless WithExclude excludes them

type ProgressFn

type ProgressFn func(schema.Progress, error)

ProgressFn is called as a volume is indexed: with its progress, after each file is indexed, or marked, and with an error, when a file can't be read, which isn't indexed, and once it's finished, with its error, when it failed. It's called as the volume is walked, so it delays it.

Directories

Path Synopsis
cmd
Package cmd is the commands, and flags, of a file search manager, which a program composes, and which imports the handlers of go-metadata which it reads files with.
Package cmd is the commands, and flags, of a file search manager, which a program composes, and which imports the handlers of go-metadata which it reads files with.
filesearch command
Package schema is the schema of the file indexer: its tables, its queries, and the types which are read from, and written to, them.
Package schema is the schema of the file indexer: its tables, its queries, and the types which are read from, and written to, them.
Package test creates managers for tests, of temporary databases, which are closed, and deleted, when the tests end.
Package test creates managers for tests, of temporary databases, which are closed, and deleted, when the tests end.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL