Class: Query

lunr.Query()

new Query()

A lunr.Query provides a programmatic way of defining queries to be performed against a lunr.Index.

Prefer constructing a lunr.Query using the lunr.Index#query method so the query object is pre-initialized with the right index fields.

Properties:
Name Type Description
clauses Array.<lunr.Query~Clause>

An array of query clauses.

allFields Array.<string>

An array of all available fields in a lunr.Index.

Source:

Members

(static) presence :number

Constants for indicating what kind of presence a term must have in matching documents.

Type:
  • number
Properties:
Name Type Description
OPTIONAL number

Term's presence in a document is optional, this is the default value.

REQUIRED number

Term's presence in a document is required, documents that do not contain this term will not be returned.

PROHIBITED number

Term's presence in a document is prohibited, documents that do contain this term will not be returned.

Source:
See:
Example

query term with required presence

query.term('foo', { presence: lunr.Query.presence.REQUIRED })

(static, constant) wildcard

Constants for indicating what kind of automatic wildcard insertion will be used when constructing a query clause.

This allows wildcards to be added to the beginning and end of a term without having to manually do any string concatenation.

The wildcard constants can be bitwise combined to select both leading and trailing wildcards.

Properties:
Name Type Description
wildcard.NONE number

The term will have no wildcards inserted, this is the default behaviour

wildcard.LEADING number

Prepend the term with a wildcard, unless a leading wildcard already exists

wildcard.TRAILING number

Append a wildcard to the term, unless a trailing wildcard already exists

Source:
See:
Examples

query term with trailing wildcard

query.term('foo', { wildcard: lunr.Query.wildcard.TRAILING })

query term with leading and trailing wildcard

query.term('foo', {
  wildcard: lunr.Query.wildcard.LEADING | lunr.Query.wildcard.TRAILING
})

Methods

clause(clause) → {lunr.Query}

Adds a lunr.Query~Clause to this query.

Unless the clause contains the fields to be matched all fields will be matched. In addition a default boost of 1 is applied to the clause.

Parameters:
Name Type Description
clause lunr.Query~Clause

The clause to add to this query.

Source:
See:
Returns:
Type
lunr.Query

isNegated()

A negated query is one in which every clause has a presence of prohibited. These queries require some special processing to return the expected results.

Source:
Returns:

boolean

term(term, optionsopt) → {lunr.Query}

Adds a term to the current query, under the covers this will create a lunr.Query~Clause to the list of clauses that make up this query.

The term is used as is, i.e. no tokenization will be performed by this method. Instead conversion to a token or token-like string should be done before calling this method.

The term will be converted to a string by calling toString. Multiple terms can be passed as an array, each term in the array will share the same options.

Parameters:
Name Type Attributes Description
term object | Array.<object>

The term(s) to add to the query.

options object <optional>

Any additional properties to add to the query clause.

Source:
See:
Returns:
Type
lunr.Query
Examples

adding a single term to a query

query.term("foo")

adding a single term to a query and specifying search fields, term boost and automatic trailing wildcard

query.term("foo", {
  fields: ["title"],
  boost: 10,
  wildcard: lunr.Query.wildcard.TRAILING
})

using lunr.tokenizer to convert a string to tokens before using them as terms

query.term(lunr.tokenizer("foo bar"))

Type Definitions

Clause

A single clause in a lunr.Query contains a term and details on how to match that term against a lunr.Index.

Type:
  • Object
Properties:
Name Type Attributes Default Description
fields Array.<string>

The fields in an index this clause should be matched against.

boost number <optional>
1

Any boost that should be applied when matching this clause.

editDistance number <optional>

Whether the term should have fuzzy matching applied, and how fuzzy the match should be.

usePipeline boolean <optional>

Whether the term should be passed through the search pipeline.

wildcard number <optional>
lunr.Query.wildcard.NONE

Whether the term should have wildcards appended or prepended.

presence number <optional>
lunr.Query.presence.OPTIONAL

The terms presence in any matching documents.

Source: