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

Use extractField to extract document id field #59

Closed
mquinn-leverege opened this issue Aug 6, 2020 · 6 comments
Closed

Use extractField to extract document id field #59

mquinn-leverege opened this issue Aug 6, 2020 · 6 comments

Comments

@mquinn-leverege
Copy link

I've found that using a document structure with an id that is nested, like this for example where tracker.id is the document id:

{
  tracker : {
    id : 'documentId',
    ...
  },
  asset : { ... }
}

is not possible. I'm not sure if there are other considerations preventing this, but it seems as if using extractField to extract the idField in addition to regular fields might solve the issue. I would like to avoid changing the document structure if possible.

@lucaong
Copy link
Owner

lucaong commented Aug 7, 2020

@mquinn-leverege thanks for reporting this. Yes, I agree that it would be nice to be able to tread the id field like the other fields via extractField. I'll look into this, it might impact a few places.

@lucaong
Copy link
Owner

lucaong commented Aug 13, 2020

Hi @mquinn-leverege , this issue is resolved by this commit, now the ID field is consistent with the other fields, and its extraction can be customized with extractField.

This change is already released as part of version v2.5.0. This should solve your case, so I will close this issue, but feel free to comment if not.

@lucaong lucaong closed this as completed Aug 13, 2020
@noraj
Copy link

noraj commented May 15, 2021

@lucaong I have a project where I have no uniq field but I could have a uniq string by combining three fields.

Could I do something like

idField: ['field1', 'field2', 'field3']

or

function join(...strings) {
  return strings.join('')
}
...
idField: join('field1', 'field2', 'field3')

or create a virtual field with extractField and pass it to idField?

@lucaong
Copy link
Owner

lucaong commented May 17, 2021

Hi @noraj ,
Yes, you can use extractField to implement a “virtual field”:

const miniSearch = new MiniSearch({
  fields: [],
  extractField: (document, fieldName) => {
    if (fieldName === 'id') {
      return `#{document.field1}-#{document.field2}`
    } else {
      return document[fieldName]
    }
  }
})

In the example above, the ID field is composed by joining two other fields.

@noraj
Copy link

noraj commented May 18, 2021

Thanks it works well

const miniSearch = new MiniSearch({
  fields: [],
  extractField: (document, fieldName) => {
    if (fieldName === 'id') {
      return `${document.field1}-${document.field2}`
    } else {
      return document[fieldName]
    }
  }
})

@noraj
Copy link

noraj commented May 18, 2021

PS: It could be nice to create a expo gallery in the documentation or github wiki to link projects using minisearch so people could look at real-life/production examples.

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

3 participants