0

According to official documentation of lunrjs:

"Searches for multiple terms are also supported. If a document matches at least one of the search terms, it will show in the results. The search terms are combined with OR".

Is there any way to achieve that only if a document matches all the search terms it will show the results?

Having these data indexed:

[
  {id: 1, text: "foo", body: "jander"}
  {id: 2, text: "bar", body: "clander"},
  {id: 3, text: "foo bar", body: "jander crispy clander"}
  {id: 4, text: "foz", body: "lorem ipsum"}
  ...
]

If you search by:

idx.search("foo jander clander");

I wish to have only one result ignoring the other two because they don't contain all terms but a few:

// found (1) 
{id: 3, text: "foo bar", body: "jander crispy clander"}

but what I have is:

// found (3) 
{id: 1, text: "foo", body: "jander"}
{id: 2, text: "bar", body: "clander"},
{id: 3, text: "foo bar", body: "jander crispy clander"}

Thanks in advance for any input.

1 Answers1

0

The pull request you mentioned has been merged and released in version 2.2.0.

Lunr calls this term presence. You can't (yet) directly combine terms with a boolean operator, e.g. foo AND bar, instead you indicate the presence of term in a query. By default a term has optional presence, but it can now also be marked as required or prohibited.

The docs (guide, api) for searching have been updated, but for you specific example you would phrase the query as:

idx.search("+foo +jander +clander")

This is also exposed via the lower level query api:

idx.query(function (q) {
  q.term("foo", { presence: lunr.Query.presence.REQUIRED })
  q.term("jander", { presence: lunr.Query.presence.REQUIRED })
  q.term("clander", { presence: lunr.Query.presence.REQUIRED })
})
Oliver Nightingale
  • 1,805
  • 1
  • 17
  • 22