olivernn / lunr.js

A bit like Solr, but much smaller and not as bright
http://lunrjs.com
MIT License
8.94k stars 548 forks source link

Static site search, I can not access the property. #389

Closed rludgero closed 5 years ago

rludgero commented 5 years ago

Hi, I'm trying to create a small search engine for a static site, but I'm not getting access to the title property.

I can not find what I'm not doing right.

Could anyone help me please?

This is the code https://jsbin.com/jonicedigu/edit?html,js

Thanks in advance. :)

hoelzro commented 5 years ago

Hi @rludgero!

First of all, I noticed from your JS Bin post that you're using lunr 0.7.2 - I highly recommend using a more recent release. If something is preventing you from doing so, please let us know!

Now, back to the problem at hand...

Looking at this line:

searchItem.innerHTML = '<div><h4>'+ [ref].title + '</h4></div>';

With [ref], you're creating an array and trying to look up the title property on that array. Since lunr only returns the ref of the matching documents (plus some metadata including things like scoring, but we can ignore those in this situation), you need to store your actual documents in another location so you can retrieve them based on the search results. So, if you had a global lookup that was keyed on the path attribute of a document, and used the documents themselves as keys, you could change the line above to the following:

searchItem.innerHTML = '<div><h4>'+ lookup[ref].title + '</h4></div>';

...and your program should work.

rludgero commented 5 years ago

Hi @hoelzro , thanks for your prompt answer 👍. Would it be possible to teach me in an example, or better yet, through the jsbin? I have updated the version of lunr to 2.3.5 in the jsbin.

And the initLunr function too.

https://jsbin.com/himamaqahi/edit?html,js,output

hoelzro commented 5 years ago

@rludgero I made some changes here: https://jsbin.com/mavujewiba/2/edit?html,js,console,output

rludgero commented 5 years ago

@hoelzro , 👍 , now I have caught what you told me before.

Thank you very much, for having spent your time and for helping me, now it works perfectly. 💯