Skip to content

Add search to Rails Guides #39299

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LukasSkywalker
Copy link
Contributor

@LukasSkywalker LukasSkywalker commented May 15, 2020

For better discoverability and to prevent users from having to leave
the Guides webiste, this commit implements a client side search based
on the open lunr library.

Different libraries were compared:

  • lucaong/minisearch, where it was not possible to pre-build an index.
    Building it on the fly would take about 3 seconds and render the
    browser unresponsive.
  • krisk/Fuse, where it was possible to pre-build an index, but the
    fuzzy search was very slow and would block for about 600ms.
  • olivernn/lunr.js, where the index cloud be pre-built and the fuzzy
    search could be disabled.

The new search uses Lunr with a pre-built index and by using a lookup
table where the id of the documents are mapped to their respective url
to keep the index size small.

To build the index, the Guides HTML is parsed and split into documents
that are then indexed using ExecJS. This generates an index of about
3.3 MB in size which is only loaded when the user focuses the search
input.

The index is then stored in the browsers localstorage for up to a week
for performance.

Demo here

image

Summary

Other Information

References:

@rails-bot rails-bot bot added the docs label May 15, 2020
@LukasSkywalker LukasSkywalker force-pushed the guide-search branch 2 times, most recently from 60a706c to 3f1178e Compare May 17, 2020 12:28
Copy link

@lebalz lebalz left a comment

Choose a reason for hiding this comment

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

Thanks for this addition, nice job!

@LukasSkywalker
Copy link
Contributor Author

cc @fxn

@rolandasb
Copy link
Contributor

Nice work 👍 I'm not sure about the positioning between navigation items. Maybe it would make sense to put that in the top bar?

Screenshot 2020-05-27 at 11 49 35

@LukasSkywalker
Copy link
Contributor Author

LukasSkywalker commented May 27, 2020

@rolandasb I was thinking about that as well, but thought it was a bit of a mismatch – the content of the top bar references items outside the Guides while the red navigation bar references things contained in the Guides. Since the search only works inside the Guides, but not on rubyonrails.org, I put it in the lower bar.

But I don't have a very strong stance on this.

@p8
Copy link
Member

p8 commented Jun 16, 2020

Is anyone else getting an endless "Please wait.."?
image
Edit: Ah, that is a "QuotaExceededError: The quota has been exceeded."

@LukasSkywalker
Copy link
Contributor Author

@p8 It should work if you try again. The "Please wait..." indicator appears the first time you use the search since the index needs to be downloaded. Due to the size (3.2MB) it is lazily loaded.

@rails-bot
Copy link

rails-bot bot commented Sep 17, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot removed the stale label Sep 24, 2020
@rails-bot
Copy link

rails-bot bot commented Dec 23, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 23, 2020
@rails-bot rails-bot bot removed the stale label Dec 30, 2020
Base automatically changed from master to main January 14, 2021 17:01
@jaredcwhite
Copy link

What can we do to push this along? I think search would be hugely welcome addition to the Rails guides. Better documentation and onboarding seems to be top of mind for a number of Rubyists these days and this could help. @rafaelfranca @fxn

@yorch
Copy link

yorch commented Nov 8, 2021

I don't really understand the lack of interest for adding search capabilities from the maintainers. For those of us not extremely familiar with the guides, search is a huge help, and as other have commented before, many other projects have embraced adding searching capabilities to their docs.

@fxn
Copy link
Member

fxn commented Nov 8, 2021

/cc @zzak

@LukasSkywalker LukasSkywalker force-pushed the guide-search branch 2 times, most recently from 49ffdc5 to 33ef886 Compare November 8, 2021 16:42
link_map_js = "var linkMap = #{link_map.to_json};"
File.write("#{@output_dir}/javascripts/lunr-documents.js", documents_js + link_map_js)

lunr = open("https://unpkg.com/lunr@2.3.8/lunr.js").read
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be simpler to just 'vendorize' the lunr source, instead of downloading it everytime.

@Austio
Copy link
Contributor

Austio commented Jul 20, 2022

Bump :) seems like a really great addition

Copy link
Contributor

@Schwad Schwad left a comment

Choose a reason for hiding this comment

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

A good hunk of this leans on vanilla JS which isn't my forte so will defer to others for their opinions on how it's put together 😅

But from a product feel this seems like a big win to me. would be a nifty addition for Rails 7.1!

As I haven't generated the guides to tophat a change in a while can someone link me the quick TLDR on tophatting this locally? Thanks!

When going through the demo - I think one of the guides strengths already is to navigate via headers.

But what I could really use is searching by text - e.g. form_with returns nothing. How hard would it be to search text body so I can search code snippets to find the right guide?

@Schwad
Copy link
Contributor

Schwad commented Oct 5, 2022

Also 10/10 for giving us a working demo!

@skipkayhil
Copy link
Member

As I haven't generated the guides to tophat a change in a while can someone link me the quick TLDR on tophatting this locally? Thanks!

https://guides.rubyonrails.org/ruby_on_rails_guides_guidelines.html#html-guides-generation
Under /guides run $ bundle exec rake guides:generate which puts them in /guides/output. To serve them you can use any static file server, I like this ruby one liner (in the output folder): ruby -run -ehttpd . -p8000 -b0.0.0.0

@LukasSkywalker LukasSkywalker force-pushed the guide-search branch 2 times, most recently from 9e0bded to 180374d Compare October 6, 2022 06:20
For better discoverability and to prevent users from having to leave
the Guides webiste, this commit implements a client side search based
on the open lunr library.

Different libraries were compared:

* lucaong/minisearch, where it was not possible to pre-build an index.
  Building it on the fly would take about 3 seconds and render the
  browser unresponsive.
* krisk/Fuse, where it was possible to pre-build an index, but the
  fuzzy search was very slow and would block for about 600ms.
* olivernn/lunr.js, where the index cloud be pre-built and the fuzzy
  search could be disabled.

The new search uses Lunr with a pre-built index and by using a lookup
table where the id of the documents are mapped to their respective url
to keep the index size small.

To build the index, the Guides HTML is parsed and split into documents
that are then indexed using ExecJS. This generates an index of about
4.8 MB in size which is only loaded when the user focuses the search
input.

The index is then stored in the browsers localstorage for up to a week
for performance.
@LukasSkywalker
Copy link
Contributor Author

@Schwad I added the code blocks to the index in the above demo. You need to invalidate the cache for the new index to load by typing localStorage.clear() into the browser console.

@Schwad
Copy link
Contributor

Schwad commented Oct 6, 2022

Screenshot 2022-10-06 at 12 02 57

@LukasSkywalker nice! 🙌

@p8
Copy link
Member

p8 commented Oct 6, 2022

For consistency I think it would be nice if it used the same styles as the index dropdown.
Something like:
image

We could probably also remove the "Contribute" item from the menu as it's already present in the top menu.
And swap the index with the search input, to make it a bit easier to find. Maybe even add an inner shadow to make it look more like an input.
I also think it can be a bit smaller as most search terms will probably fit easily.
image

@JohnAnon9771
Copy link
Contributor

There is a version of the guide for Brazilian Portuguese where this search is already implemented, maybe it will be a good reference.

https://guiarails.com.br/

@akirataguchi115
Copy link

One idea that hasn't been mentioned here yet is the way Ruby on Rails API implements search functionality: https://api.rubyonrails.org/.

Right now I'll have to use the Google web search operators f.ex. site:guides.rubyonrails.org destroy.

@akirataguchi115
Copy link

There is a version of the guide for Brazilian Portuguese where this search is already implemented, maybe it will be a good reference.

https://guiarails.com.br/

This is great and I would definitely implement this and make a pull request to this repository. The translation however is not forked and (thus) not licensed, so the contributors own the copyright to the search implementation.

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

Successfully merging this pull request may close these issues.