question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Nested field support for index setup

See original GitHub issue

When I have documents with a structure like:

{
  name: "Some name",
  address: {
    street: "Main Street",
    number: "42",
    city: "Some Town"
  }
}

I’d like to search among the city field but I can only specify fields on the top level of the document when setting up my index:

var index = lunr(function () {
    this.field('address')
});

It would be nice, if I could specify a nested field for indexing. Something like this:

var index = lunr(function () {
    this.field('address.city');
    //or maybe:
    this.field(['address', 'city']);
});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
olivernncommented, Jun 24, 2015

This is a request that comes up quite a lot.

The reason I haven’t implemented this change is that I think it can (and should) be done outside of lunr, since the structure of the document is very specific to each user.

In your use case you would just need to have a conversion function that maps your document to something that can be indexed:

var mapper = function (document) {
  return {
    name: document.name,
    city: document.address.city,
    street: document.address.street
  }
}

var idx = lunr(function () {
  this.field('name')
  this.field('city')
  this.field('street')
})

idx.add(mapper(document))

This also gives you the flexibility to combine certain fields into a single field to be indexed by lunr or doing any other conversion you want before lunr starts to index the fields.

0reactions
olivernncommented, Feb 19, 2018

@Petesta the search method does not return the documents that match, instead it returns a reference, i.e. the id of the document. The results are covered in more detail in the search results docs, they look like this:

[{
  "ref": "123",
  "score": 0.123456,
  "metadata": {...}
}]
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nested field type | Elasticsearch Guide [8.5] | Elastic
The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way...
Read more >
Object Fields VS. Nested Field Types in Elasticsearch - Opster
Nested is a special type of object that is indexed as a separate document, and a reference to each of these inner documents...
Read more >
Adding nested fields ( objects in array ) query support to ...
You need to go to the respective indexes and enable them. For doing that, go to Management -> Nested fields.
Read more >
Nested - OpenSearch documentation
A nested field type is a special type of object field type. Any object field can take an array of objects. Each of...
Read more >
Indexing Nested Documents :: Apache Solr Reference Guide
Example Indexing Syntax: Pseudo-Fields; Schema Configuration; Maintaining Integrity with Updates and Deletes; Indexing Anonymous Children. Solr supports ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found