Text Search How-To

This page walks through the text search features with worked examples you can reproduce on a small dataset. The companion pages cover the feature overview and the availability rules; the internals page is for engine developers.

The example dataset

Save this as books.ttl — a small catalogue with titles, abstracts, authors, and one named graph:

@prefix ex:   <http://example.org/> .
@prefix base: <http://example.org/graphs/> .

ex:book1 a ex:Book ;
    ex:title "The Laser Printer Handbook" ;
    ex:abstract "A practical guide to laser printers, toner, and maintenance."@en ;
    ex:author ex:ada .

ex:book2 a ex:Book ;
    ex:title "Inkjet Printing at Home" ;
    ex:abstract "Choosing and running an inkjet printer on a budget."@en ;
    ex:author ex:ada .

ex:book3 a ex:Book ;
    ex:title "Laser Physics" ;
    ex:abstract "How stimulated emission produces coherent light."@en ;
    ex:author ex:bruno .

ex:ada ex:name "Ada Bell" .
ex:bruno ex:name "Bruno Costa" .

base:archive {
    ex:book4 a ex:Book ;
        ex:title "Le Tonner Moderne"@fr ;
        ex:abstract "Un guide des imprimantes laser."@fr ;
        ex:author ex:bruno .
}

Start a cluster with the text index enabled and load the file:

./target/debug/waratah start --load-demo --with-text --foreground
waratah import books.ttl --connect

The index builds automatically at startup and stays current: every string the cluster accepts is indexed before its triple becomes visible (strings longer than 1 MiB are rejected with the write — see text-search.adoc#size-limit).

Find strings that match a full-text query

The Jena-style function text:query searches the analyzed text of every string literal. As a FILTER beside a pattern that binds the variable:

PREFIX ex: <http://example.org/>
PREFIX text: <http://jena.apache.org/text#>

SELECT ?title WHERE {
  ?book ex:title ?title .
  FILTER(text:query(?title, "laser"))
}

Answers: The Laser Printer Handbook and Laser Physics. The match is on whole tokens after analysis — "laser" matches the title token, and "print" matches printing and printed under a stemming analyzer (en_stem) but not printer. With the default simple analyzer, tokens match exactly after lowercasing.

Multi-word queries are disjunctions — a term matches if ANY of the words matches:

FILTER(text:query(?abstract, "laser printer"))

This answers the abstracts containing "laser" OR "printer" — book1 and book2 in the unnamed graph (book4’s French abstract matches too, once its graph is in scope); book3 has neither token. For conjunction, write it out:

FILTER(text:query(?abstract, "laser AND toner"))

Both terms must match the same abstract, so only book1 qualifies.

Restrict the search to a language

Add a trailing language argument to consider only literals with that tag:

SELECT ?abstract WHERE {
  ?book ex:abstract ?abstract .
  FILTER(text:query(?abstract, "laser", "lang:fr"))
}

Answers: Un guide des imprimantes laser. — the English abstracts are outside the window.

Find the subjects whose property matches

The Jena subject form names the predicate in the call and answers with the SUBJECTS whose property text matches. Bind the subject with other patterns — its type, for instance — and leave the title unbound; the searched predicate appears only inside text:query:

SELECT ?book WHERE {
  ?book a ex:Book .
  FILTER(text:query(?book, ex:title, "laser"))
}

Answers: ex:book1 and ex:book3. A subject with several matching values appears once, and you can pass several predicates — the search runs over all of them:

SELECT ?book WHERE {
  ?book a ex:Book .
  FILTER(text:query(?book, ex:title, ex:abstract, "inkjet"))
}

Answers: ex:book2 — both its title and its abstract mention inkjet.

Combine text search with other patterns

The text filter composes with the rest of the query like any other filter — the engine answers the text question from the index and joins the survivors with the other patterns:

SELECT ?title ?authorName WHERE {
  ?book ex:title ?title ;
        ex:author ?author .
  ?author ex:name ?authorName .
  FILTER(text:query(?title, "laser"))
}

Every ex:book1 / ex:book3 row is joined against its author as usual — answers: The Laser Printer Handbook / Ada Bell and Laser Physics / Bruno Costa.

Graphs and datasets

The text search sees exactly the graphs the surrounding query sees. A bare BGP matches the unnamed graph:

SELECT ?book WHERE {
  ?book a ex:Book .
  FILTER(text:query(?book, ex:title, "tonner"))
}

No answers: the only title containing "tonner" belongs to ex:book4, which lives in the named graph base:archive and is invisible here — exactly as it would be without the text filter. Name the graph to reach it:

SELECT ?title WHERE {
  GRAPH base:archive {
    ?book ex:title ?title .
    FILTER(text:query(?title, "tonner"))
  }
}

Answers: Le Tonner Moderne.

With --default-union, the unnamed and all named graphs merge into one default window, and the text search follows that same window. FROM <g> restricts the search to g the same way.

Prefix, suffix, and anchored regex

The three ordinary string functions consult the same index when the pattern has an anchor:

SELECT ?title WHERE {
  ?book ex:title ?title .
  FILTER(STRSTARTS(?title, "Laser"))
}
SELECT ?title WHERE {
  ?book ex:title ?title .
  FILTER(REGEX(?title, "^Laser"))
}
SELECT ?title WHERE {
  ?book ex:title ?title .
  FILTER(STRENDS(?title, "Handbook"))
}

Answers: Laser Physics for the first two, The Laser Printer Handbook for the last. All three stay correct for ANY pattern — a general regex (with ., |, quantifiers, or flags) simply runs as a plain filter without the index shortcut. These functions never error when the index is unavailable; they just answer from the plain filter.

text:query behaves differently: when the index is missing, unbuilt, or was built with different settings, it fails the query with a message naming the repair command. That error is deliberate — there is no slow path for a full-text query, and a silent empty answer would hide the problem.

Keeping the index usable

The index follows the store automatically while it is trusted. Two things put it out of step:

  • Writing with the plugin disabled (or a startup build that failed), then enabling it later.

  • Changing analyzer or seed_chars in text.toml — the index identity changes and the old contents no longer match.

An online import (waratah import … --with-text, attached to a running or freshly spawned cluster) checks the index before loading anything: if the database has no trusted text index yet, the import builds one first — covering the rows already in the store — and then loads, so the finished index answers text:query over everything. The running cluster keeps answering text:query with the repair hint until its next restart, because a server reads its index state at startup. If the existing index was built by other settings (a changed analyzer or seed length), the import refuses instead — re-analyzing a live index is not supported:

waratah stop --data-dir ./waratah-data
waratah text rebuild-index --data-dir ./waratah-data

Rebuild offline and check the state:

waratah stop --data-dir ./waratah-data
waratah text rebuild-index --data-dir ./waratah-data
waratah text status --data-dir ./waratah-data

Limits of this release

A string literal longer than 1 MiB cannot be written: clustered stores reject it in the string registry with the plugin on or off, and single-process stores reject it while the text plugin is enabled (text-search.adoc#size-limit). Single-process stores with the plugin disabled are the only way to hold such literals. CONTAINS and general (unanchored) REGEX are not index-accelerated. text:query has no score, highlight, or limit arguments, and STR(?v)-wrapped arguments are not rewritten. The query string is a full-text search expression (terms, phrases, field prefixes, boolean operators) — it is not a SPARQL expression.