Skip to content

Refactor search plugin - #1418

Merged
waylan merged 34 commits into
mkdocs:masterfrom
waylan:new-search
Mar 6, 2018
Merged

waylan merged 34 commits into
mkdocs:masterfrom
waylan:new-search

Conversation

@waylan

@waylan waylan commented Feb 27, 2018 •

Copy link
Copy Markdown
Contributor

Yeray Diaz Diaz and others added 27 commits February 27, 2018 15:10
Put web worker stuff to a new plugin and leave legacy_search with its
former behavior.
Remove all unnesecary whitespace. Closes mkdocs#1128.
In mkdocs.yml define seperator like this:

  plugins:
      - search:
          seperator: '[\\s\\-\\.]+'

Fixes mkdocs#867.
This only works in a worker.
Fails as the code tries to run before all scripts are loaded. Not sure
how best to deal with that.
Still need to use it.

To display search results the raw docs data is still needed by the
browser. As we need to pass everything to the browser we might as well
put it all in one file.
Testing with MkDocs documentation the index file size went from 219 KB
to 809 KB by adding the prebuilt index to the file.

While having the index prebuilt may be faster, it is not nessecarily
lighter. So let's give users the option to choose.
Users don't need the extra language support files for languages they
don't support. No need to bloat their site.
This makes for more consistent handling. Sometimes worker.js is run in a
seperate thread. Somethimes in the main thread (as a fallback) and
sometimes the index is prebuilt. That's three different envionments
where the index can be build, each with a different possable base_url.
By only applying the base_url in the main thread when search results are
displayed, we ensure consistancy.
If using a single lang, can be a string, otherwise expects a list. Each
lang provided is checked against lang support files and validation error
is raised if support file is not found. Adding new langs is as simple as
adding the support file.
@waylan

waylan commented Feb 27, 2018 •

Copy link
Copy Markdown
Contributor Author

Anyone reviewing this (especially theme authors @squidfunk) should take a close look at the structure of the search_index.json. The docs explain:

The search index is written to a JSON file at search/search_index.json in the
[site_dir]. The JSON object contained within the file may contain up to three
objects.

{
    config: {...},
    data: [...],
    index: {...}
}

If present, the config object contains the key/value pairs of config options
defined for the plugin in the user's mkdocs.yml config file under
plugings.search. The config object was new in MkDocs version 1.0.

The data object contains a list of document objects. Each document object is
made up of a location (URL), a title, and text which can be used to create
a search index and/or display search results.

If present, the index object contains a pre-built index which should load much
faster than building the index in the browser on each page load. Note that the
pre-built index is only created if the necessary dependencies (node.js) are
available when mkdocs build is run. Any failures are ignored silently with the
expectation that the index can be built in the browser. The index object was
new in MkDocs version 1.0.

Note that the data object preexisted and any existing third-party themes which build their own index should continue to work as-is. Themes may choose to make no modifications and continue to ignore the other items or, check for their existence and only act on them if they exist.

A word of caution: if the user does not set prebuild_index=False in their plugins.search config, the prebuilt index will be included even if the theme doesn't need it. While the theme can easily ignore it, that could potentially waste a lot of bandwidth. There is currently no way to set/override that setting from the themes config. In my experience, the pre-built index significantly increases the size of the file for a small site, but has a relatively small effect on the size of the file for a large site. See the discussions in #859 and #1061 for more details about that.

@squidfunk

Copy link
Copy Markdown
Contributor

@waylan Well, I think upgrade complexity and necessary effort are very important aspects. When I'm running an Open Source project as large as Material I try always to minimize breakage across versions, even minor versions. I try to keep them to major releases and so far there have been two.

I don't want my users to bother with more setup than they currently have, only to support search which all users take for granted and which works very well for Material. Introducing such a change would incur a silent penalty (prebuilt index is on by default) that probably nobody would notice. It would mean that the setup for every theme is getting more complex and there are more potential traps to fall into. There are a lot of users asking simple questions on the issue tracker already.

When you want themes to adopt different plugins, you also need themes to explicitly state what they want to support for those plugins. This is the very reason we have the search index configurations in the top-level theme object which were added in 0.17.1. It would be perfect if themes can define which features they support and turn on/off default flags for plugins, if they are available. Otherwise, if the user specifies the plugin themselves, I would expect that the user's choices take precedence.

The configuration options I added are partly there because MkDocs doesn't support some features. Search is probably a topic which Material addresses in a very specific way and therefore added a lot of options. I will do my best to merge them with the new options for the search plugin.

Regarding the languages - Material integrates the languages from lunr-languages and adds support for more languages by configuring stemmers, trimmers and tokenizers in specific ways - see the language files. Language support doesn't include stemming, yes, but otherwise search wouldn't work at all, that's the reason Material does that.

@waylan

waylan commented Feb 28, 2018

Copy link
Copy Markdown
Contributor Author

I think upgrade complexity and necessary effort are very important aspects. When I'm running an Open Source project as large as Material I try always to minimize breakage across versions, even minor versions. I try to keep them to major releases and so far there have been two.

I agree. However, it is important to remember that you are currently developing against a pre-1.0 version of Mkdocs. At this point I'm more interesting in getting things "right" than backward compatibility. The next release of MkDocs (which will include the refactored search) will be 1.0, so from my perspective, if we are going to break anything, now is the time to do it. What we settle on now is what we're stuck with going forward.

I don't want my users to bother with more setup than they currently have

That's understandable and a consideration of mine as well. I realize search is a special case, but it is also the first plugin using our new API so its the testing ground for the API. I want to get that API right and how plugins and themes interact is a much bigger factor than I originally anticipated.

It would be perfect if themes can define which features they support and turn on/off default flags for plugins, if they are available. Otherwise, if the user specifies the plugin themselves, I would expect that the user's choices take precedence.

Thank you. That's helpful feedback. And that more-or-less is what I am proposing. Unfortunately, I expect that means backward incompatible changes with the 1.0 release of MkDocs but I havn't fleshed out the details yet.

Annoyingly I tested against 3.6 but earlier versions are different.
@waylan

waylan commented Feb 28, 2018

Copy link
Copy Markdown
Contributor Author

@yeraydiazdiaz we have a couple jshint errors for your code that I'm not clear how to resolve. Any assistance would be appreciated. As a warning, I rebased the code since your last commit, so you may not be able to merge cleanly.

@yeraydiazdiaz

Copy link
Copy Markdown
Contributor

Should be fixed via yeraydiazdiaz@8597619

Comment thread mkdocs/contrib/search/prebuild-index.js Outdated
} else if (lang.length > 1) {
this.use(lunr.multiLanguage.apply(null, lang));
}
this.field('title', { boost: 10 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As has been mentioned in #1319 and olivernn/lunr.js#312 field boosts are not currently supported by Lunr 2.x. The second parameter here will be ignored, I'm not sure that does any harm, but perhaps a comment might help.

When I get around to implementing field boosts it will almost certainly use this interface (same as Lunr 1.x) so you could leave this here to take advantage of field boosts when they arrive.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I realize boost is being ignored. But your right, no point in leaving it there. It will just cause confusion later when we forget that.

@@ -0,0 +1,2977 @@
/**
* lunr - http://lunrjs.com - A bit like Solr, but much smaller and not as bright - 2.1.5

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI Lunr 2.1.6 was released which includes a performance improvement when building indexes. I don't know how you intend on keeping up with releases of Lunr, but thought you might appreciate faster building!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the heads up. Might as well release with the most recent version.

@waylan

waylan commented Mar 1, 2018 •

Copy link
Copy Markdown
Contributor Author

Thinking about this some more, I think maybe we should change the default for prebuild_index to False. Currently, the index building script is written in JavaScript and runs in Node.js. If the node command is not available on the system PATH, then it fails silently and no prebuilt index is generated (we get the same behavior as prebuild_index: False).

It occurs to me that this could present a problem in that a user could build the same project from two different machines and get two different results if one has node installed and the other doesn't. The way things are now, there would be no warning to the user. Instead, to prebuild the index, we should require the user to explicitly request it and then issue either a warning or error if it fails. I'm thinking perhaps a warning (which would still fail in "strict mode") as things will still "work" without it.

While it's true that we loose the prebuilt index as a default, it really only benefits users with large sites anyway. Those users are going to be willing to do the extra work to set the config setting and install Node.js. If, at some point in the future, we get a Python solution for pre-building the index, then we can reconsider the default at that time.

@squidfunk

Copy link
Copy Markdown
Contributor

While it's true that we loose the prebuilt index as a default, it really only benefits users with large sites anyway. Those users are going to be willing to do the extra work to set the config setting and install Node.js.

That's a very valid point and great idea. Users having problems with the search will want to go the extra mile. You could add a section on performance improvements for search in the documentation and add the prebuild_index option there. All other users won't notice anything. I'm also not a fan of the "do it if available or leave it out" method (regarding Node availability). The proposed behavior is far more transparent. Big up!

@waylan

waylan commented Mar 1, 2018

Copy link
Copy Markdown
Contributor Author

The prebuild_index default has been changed. This feels much better. Baring any other concerns, I'll squash and merge this in a few days.

@yeraydiazdiaz

Copy link
Copy Markdown
Contributor

If, at some point in the future, we get a Python solution for pre-building the index, then we can reconsider the default at that time.

Well, now that you mention it, I just published v0.1.1a1 of Lunr.py 😄

I honestly thought it'd take me longer to port but @olivernn did an excellent job with the JS version, especially with the unit tests which helped quite a bit to get the building blocks sorted quickly.

There's still a few places in the API I'd like to tweak to make it more Pythonic, but I'm happy to say it produces the same results searches on the MkDocs search_index.json that the JS version, both in Python 2.7 and 3. I'll make some tests to ensure serialized indexes can be passed back and forth between versions as a starting point for pre-building the index in pure Python.

Have a look at the repo if you like, of course any feedback is appreciated.

@waylan
waylan merged commit dd7e2d9 into mkdocs:master Mar 6, 2018
@ghost

ghost commented Apr 26, 2018

Copy link
Copy Markdown

@waylan Many thanks for your efforts here - I hope this makes the search also usable for my rather large documentation without blocking the browser. Do you already know when this is going to be released officially?

@waylan

waylan commented Apr 26, 2018

Copy link
Copy Markdown
Contributor Author

@josuahunziker this will be part of the 1.0 release. Version 1.0 will be released when the milestone is complete.

@ghost

ghost commented Apr 26, 2018

Copy link
Copy Markdown

@waylan Ok, many thanks! I'm looking forward! 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants