Full-text search: fulltext

fulltext ranks entities by the words of their text fields. It keeps an index of the fields that its settings name, and the query clause fulltext/match finds the entities whose indexed text matches a query and gives each one a BM25 relevance score. It requires database, and keeps its index in the database storage. Without fulltext, a query has no full-text clause.

SettingAcceptsPurposeDefault
analyzerAn analyzer nameThe analyzer of each field that does not set its own.None: each field sets one
fieldsA map from a field name to true or to {analyzer …}The fields the index holds, each with its analyzer.No fields

Each field is true, which takes the top-level analyzer, or a map with its own analyzer. Each field must get an analyzer from one of the two. The plugin refuses a field without an analyzer, an unknown analyzer or an unknown setting when the database opens.

Dependencies

Configuration

Select this plugin, and database with a path, when opening the database:

json296 B
{
  "plugins": {
    "database": {
      "path": "app.db"
    },
    "fulltext": {
      "analyzer": {
        "#local": "english"
      },
      "fields": {
        "title": true,
        "body": {
          "analyzer": {
            "#local": "french"
          }
        }
      }
    }
  }
}

The index

The index holds the text values of the named fields. It does not hold numbers, lists, maps or field names. The text fields of one entity make one document. Each write updates the index in the same transaction as its facts, so a query always reads the index of the current facts.

A database that opens with other fields or analyzers than its index was built with, or after a host without fulltext wrote to it, builds the index again from the current facts before the first request. The fulltext/rebuild request builds it again on demand.

The clause

json280 B
[
  {
    "#local": "fulltext/match"
  },
  {
    "#local": "query"
  },
  {
    "#local": "?entity"
  },
  {
    "#local": "?score"
  },
  {
    "fields": [
      {
        "#local": "title"
      }
    ],
    "top-k": 10,
    "analyzer": {
      "#local": "english"
    }
  }
]

The clause binds ?entity and ?score for each hit. The query is a text, a :input, a ?variable that the query's in or another clause binds, or a map:

QueryMatches
"red shoes" or {term "red shoes"}Documents with every word
{phrase "red shoes"}The words next to each other, in order
{prefix "sho"}Words that start with the text
{and [q ...]}, {or [q ...]}All of the queries, any of them
{not q}Excludes q, inside an and with a positive query
OptionAcceptsDefault
top-kAn integer of 1 or more: the most hits the clause gives100
analyzerThe analyzer of the query textenglish
fieldsA list of indexed fields: the search reads only their text and statisticsEach indexed field

Hits come best first; hits with the same score come in ascending entity order. top-k keeps the best hits whose entity matches each fact pattern the query's other clauses have on it: a value for the field, and the pattern's value when it names one. Predicates filter the rows afterwards. When the clause is the query's only clause and the query orders by the score descending, limit and offset also lower the number of hits, and the rows are those the query gives without them.

When an earlier clause has bound the entity, the clause keeps that entity only when it is among those top-k hits. The rows never depend on the order the planner picks.

In a write, such as a reactor's query, the clause finds the write's own changes, as the fact clauses do. A subscription to a query with the clause runs again when a write changes an indexed field or a field of the query's other clauses.

A query with as-of reads the indexed fields at that state: the search builds each document from the values of that state.

Analyzers

An analyzer folds case, normalizes Unicode (NFKC), splits text into words and stems each word with the Snowball stemmer of the same name. english also drops English stop words. The analyzers are arabic, armenian, basque, catalan, czech, danish, dutch, dutch_porter, earlymodernenglish, english, esperanto, estonian, finnish, french, german, greek, hindi, hungarian, indonesian, irish, italian, lithuanian, lovins, nepali, norwegian, persian, polish, porter, portuguese, romanian, russian, serbian, sesotho, spanish, swedish, tamil, turkish and yiddish.

Status

The fulltext/status request reports fields, the indexed fields, and documents and length, the number of indexed documents and their words. With params {terms [...]} it also reports frequencies, the number of documents with each analyzed term. With params {fields [...]} it reports fieldCorpus, the documents and words of only those fields.

Contributions

Requests

  • fulltext/status
  • fulltext/rebuild