google / docsy

A set of Hugo doc templates for launching open source content.
https://docsy.dev
Apache License 2.0
2.51k stars 877 forks source link

Local search with Lunr documentation #1760

Open Marzal opened 7 months ago

Marzal commented 7 months ago

Local search with Lunr says

ensure Algolia DocSearch is set to false

but params.algolia_docsearch is deprecated.

https://www.docsy.dev/docs/adding-content/search/#customizing-algolia-templates says to create either layouts/partials/algolia/head.html or layouts/partials/algolia/scripts.html and leave it empty.

While the deprecation warning is use 'params.search.algolia'

Probably it's a lack of knowledge, but I find the doc rather confusing.

Is there any example of using Lunr ?

deining commented 7 months ago

Please note that there is a new pull request related to use of Lunr seach engine. Are you still struggling with Lunr search engine? Maybe this PR can be of help for you?

Marzal commented 7 months ago

Hi, thanks for the reference. I have Lunr working, but I still find the documentation a bit confusing.

1771 is about putting offlineSearch in params, I already had that (I use the example repo as reference). My problem started when I tried to eliminate the warning about algolia_docsearch being deprecated, without this line I'm not sure what is really necessary to enable Lunr.

I did try creating the empty files that customizing-algolia-templates says without luck.

So not sure if you have to create one or both plus configure params.search.algolia. And this last option is a mystery to my, I haven't found any example of how to configure it.

hurrellit commented 6 months ago

Hello, I am struggling with enabling Lunr local search using Docsy 0.8.0. I would like to use this for now (later I would like to use GCS).

My hugo.toml contains:

[params]
# Google Custom Search Engine ID. Remove or comment out to disable search.
#gcs_engine_id = "e39b3a4e4bff6475c"

# Enable Algolia DocSearch
params.search.algolia = false

# Enable Lunr.js offline search
offlineSearch = true

When I run a local build I get this warning:

WARN  You have more than one site-search option configured: choose only one.
      For details, see https://www.docsy.dev/docs/adding-content/search.

And my site now contains two search boxes in the nav bar and the left side bar like this:

two

I have also tried like @Marzal to create the empty files that customizing-algolia-templates explains, but that did nothing.

It seems that Algolia search is still enabled despite params.search.algolia = false.

Can anyone help? What am I doing wrong?

EDIT: I looked in https://github.com/google/docsy/pull/1771 but my offlineSearch = true is already in the[params]section, unless I misunderstood that?

EDIT2: OK do have to add params. in front of offlineSearch = true - is that correct? Doing this gets rid of the warning about having two search engines enabled, and there is indeed only one search box in the site now:

[params]
# Google Custom Search Engine ID. Remove or comment out to disable search.
#gcs_engine_id = "e39b3a4e4bff6475c"

# Enable Algolia DocSearch
params.search.algolia = false

# Enable Lunr.js offline search
params.offlineSearch = true

But using that search box reverts to GCS search..... I'm now totally confused....

deining commented 6 months ago

It seems that Algolia search is still enabled despite params.search.algolia = false.

Please try to delete or comment out this line (just a guess, untested).

hurrellit commented 6 months ago

@deining thanks for your reply.

OK I now have this - I commented out the algolia line entirely, and I removed params. in front of offlineSearch:

[params]
# Google Custom Search Engine ID. Remove or comment out to disable search.
#gcs_engine_id = "e39b3a4e4bff6475c"

# Enable Algolia DocSearch
#params.search.algolia = false

# Enable Lunr.js offline search
offlineSearch = true

With that config, running a local build with hugo server --disableFastRender now gives me the warning that I have two search engines enabled. And indeed I have two search boxes in the site. If I attempt to use one of the search boxes it directs to "http://localhost:1313/search/?q=test" which looks likes it's trying to do GCSE search and the other one does nothing at all.

When I run the build in Cloudflare, the result is here: https://doc-test.castsoftware.com/docs/get-started/ - same behaviour: two search boxes, one does nothing and the other tries to load GCSE search.

Looking at the HTML, it seems that the search box that does nothing IS attempting to run Lunr:

<input type="search" class="td-search__input form-control" placeholder="Search this site…" aria-label="Search this site…" autocomplete="off" data-offline-search-index-json-src="/offline-search-index.json" data-offline-search-base-href="/" data-offline-search-max-results="10">

Cloudflare tells me it generated and uploaded this file: offline-search-index.587e7e3a7a0ce08f074a6bb48cd4da9f.json - so perhaps that's why it's not working.

Something it really not right...

I'm wondering whether this is related to running 0.8.0, or at least not running an older release. I have had Lunr working previously, I think with 0.7.0...

EDIT: and with just this line in hugo.toml I still have the warning in a local build about two search engines enabled, and two search boxes in the site:

[params]
# Enable Lunr.js offline search
offlineSearch = true

EDIT2: and adding .params gets rid of the warning in a local build about two search engines, and leaves only one search engine in the UI, but using this search engine redirects to "http://localhost:1313/search/?q=test"... which is GCSE...

[params]
# Enable Lunr.js offline search
params.offlineSearch = true
hurrellit commented 6 months ago

OK so I have fixed my issue. In my main repo where the duplicated search error was located, I am mounting a remote repository module during the Hugo build and this remote repository also has its own hugo.toml file so that we can build that repo just to test that its contents are good without having to build the main repo. The hugo.toml file in the remote repo rather embarrassingly had GCSE search enabled and it was this search that was causing the issue with the duplicated search in the build from the main repo. As soon as I disabled all search options in the remote repo and rebuilt the main, the problem was solved and lunr is working in the main repo.

I only discovered this when I changed the main repo search from GCSE to lunr. Since both repos were using the same GCSE search, it wasn't causing an error during the build.

Apologies for hijacking the thread and apologies for spamming! Anyway, lesson learnt.....!