Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search a document which contains an array #264

Closed
JedCarrNV opened this issue Jun 19, 2024 · 2 comments
Closed

Search a document which contains an array #264

JedCarrNV opened this issue Jun 19, 2024 · 2 comments

Comments

@JedCarrNV
Copy link

Hi,

I have the following document

  {
    first:'Abby',
    last: 'Bloom',
    info: [
      {title:'ABC'},
      {title:'CNN'},
      {title:'MSN'}
    ]
  }

How do I search/extract title from the info array?

Thanks!

@lucaong
Copy link
Owner

lucaong commented Jul 1, 2024

Hi @JedCarrNV ,
do you want to consider each title as a separate document (and therefore a separate search result), or should one be able to search indifferently for any of the titles in the array and get the main document? In other words, is the data structure you shared above a single document or three separate ones?

In case it should be considered a single document, you can specify a custom extractField function that accesses each element in the array and concatenates it into a single field value:

const miniSearch = new MiniSearch({
  fields: ['first', 'last', 'info'],
  extractField: (doc, fieldName) => {
    if (fieldName === 'info') {
      return doc.info.map((entry) => entry.title).join(' ')
    } else {
      return doc[fieldName]
    }
  }
})

Does this solve your issue?

@lucaong
Copy link
Owner

lucaong commented Jul 8, 2024

My comment above should answer the original question, so I will go on and close this issue, but @JedCarrNV feel free to comment if you have more questions.

@lucaong lucaong closed this as completed Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants