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

How to get the actual records after search? #500

Open
abalter opened this issue Jun 27, 2021 · 1 comment
Open

How to get the actual records after search? #500

abalter opened this issue Jun 27, 2021 · 1 comment

Comments

@abalter
Copy link

abalter commented Jun 27, 2021

Here is some code I'm trying out:

var data = 
[
  {"id":1, "color":"green", "size": 23},
  {"id":2, "color":"blue", "size": 345},
  {"id":3, "color":"blue", "size": 7}
];

console.log(data);

var idx = lunr(function () {
  this.ref('id')
  this.field('color')

  data.forEach(function (doc) {
    this.add(doc)
  }, this)
});

result = idx.search("blue");

console.log(result);

When I run it, the console output is:

[ { id: 1, color: 'green', size: 23 },
  { id: 2, color: 'blue', size: 345 },
  { id: 3, color: 'blue', size: 7 } ]
[ { ref: '2', score: 0.47, matchData: { metadata: [Object] } },
  { ref: '3', score: 0.47, matchData: { metadata: [Object] } } ]

I have retrieved the id's for the matching records, but suppose I want the sizes? Shouldn't I be able to use the index to get the matching records themselves?

https://replit.com/@abalter/lunr-search-1

@solyarisoftware
Copy link

Hi

just collect documents by ref (that's equal id attribute):

    const result = idx.search(query)
    
    // result as an array of objects (documents)
    console.log( result.map(item => 
      documents.find(post => item.ref === post.id)
    ) 

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