olivernn / lunr.js

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

how to use lunr do paging, pagination, page? #440

Closed hoogw closed 4 years ago

hoogw commented 4 years ago

I want get first 10 record (page 1) base on whatever search result (search what can I get the whole dataset as result?)

second, I want get page 2, 10 record, offset=10, (starting from 11, next 10 record)

and so on.

So far, look like lunr only can search keywords. No pagination build in.

I have a way to work around it.

             `idx.search('')  

                 or 

                 idx.search(' ')

                 or 

                  idx.search('  ')

`

search empty, or space, or 2 space, you will get all dataset as result.

For page 1: idx.search('') get all dataset as result. Now you use for loop through, only get 1 to 10 records,then break for loop.

For page 2: idx.search('') search again, you should get the same result set as last time, but you this time you use for loop, from 11 to 20. Because offset is 10(you skip first 10).

and so on

For page x: you idx.search('') search again, you should get the same result set as last time, but this time you use for loop, from (x 10+1) to ((x 10+1) +10). Because offset is x 10 (you skip first x 10).

olivernn commented 4 years ago

Sorry, I'm not entirely sure I understand what it is you are trying to achieve.

As you said, Lunr will return all matching results. You can then paginate this array however best suits your application.

search empty, or space, or 2 space, you will get all dataset as result

I don't understand why you want to search for an empty string to get all the documents?

hoogw commented 4 years ago

I don't understand why you want to search for an empty string to get all the documents?

Here is sample I use lunr.js do paging: https://transparentgov.net:3200/socrata/dataset/default2?url=https://data.lacity.org/api/views/q2hc-nu4g/rows.json&layer=BusinessesinDistrict1&layer_id=q2hc-nu4g

I download all json into browser memory, then build lunr index, idx, now, when you click page 2, I will do idx.search('')

I get all data as 'ref'. But I only need page 2(from 11-20, 10 per page).

When you click page 3, I will do idx.search('') again, But I only need page 3(from 21-30, 10 per page).

I only show those 10 item each time for each page.

hoogw commented 4 years ago

while, I could just loop through original json, find 10 item(11-20) for page 2. is same as I do idx.search(''),

When user want to filter by keywords, I will do idx.search('keywords'), then paging as same.

One page max shows 10 records. when search keywords, search result still need paging.

So I make a standard process, if no search keywords, still use idx.search(''). if there is keywords, idx.search('keywords')

the paging code will be same piece of code, re-use by above 2 case.

Otherwise, no keywords, loop original json, one set of paging code, with keywords, idx.search(keyword), another set of paging code.

Now, one code for both case, always, use idx.search()

olivernn commented 4 years ago

As mentioned earlier, there is no paging in the results from Lunr.

It is much more efficient to loop through your original items than doing a search in Lunr to return all items.

hoogw commented 4 years ago

@olivernn Yes, I agree, that is the best solution