Skip to content
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

Control which search results are shown via a predicate function #275

Closed
hs-saunved opened this issue Oct 11, 2022 · 13 comments · Fixed by #279
Closed

Control which search results are shown via a predicate function #275

hs-saunved opened this issue Oct 11, 2022 · 13 comments · Fixed by #279

Comments

@hs-saunved
Copy link

hs-saunved commented Oct 11, 2022

We have a use case where we want to show only specific search results for specific pages.
E.g. If the user is in the /android path, only results from this path should be shown to them.

Is it possible to achieve this currently? Is there a hook that we can use to filter the search results ourselves?
If not, would it be possible to create a mode that allows for a narrower search within the base path only?

Thanks!

Edit: I believe this issue is also related #99

@weareoutman
Copy link
Member

I think it's possible, just like those using multi-versions, we can generate multiple indexes for each of your doc directories.

I think we can provide an option to allow developers to do it by base path or specific directories.

@weareoutman
Copy link
Member

weareoutman commented Oct 11, 2022

By the way, in your scenario, do you wish to search all docs when you're in the homepage /? This may change how we implement it.

@weareoutman
Copy link
Member

By the way, in your scenario, do you wish to search all docs when you're in the homepage /? This may change how we implement it.

If the answer is yes, we can't put the docs into different indexes, because if we do that, the search will not work in /. Instead, with a single index file, and we can only filter the search results manually.

@hs-saunved
Copy link
Author

Thanks for the quick response @weareoutman!

For our use case, we are okay with not having a search bar on the home page. The home page is a list/index of all of our SDKs and fairly easy to navigate. If required, we can always create a custom search bar on the homepage itself.

Separate indexes per base path would be fantastic. It will cause less confusion for users when searching, since they expect to get results from the existing context itself.

@hs-saunved
Copy link
Author

@weareoutman
Can we do something like this?

      [
        require.resolve("@easyops-cn/docusaurus-search-local"),
        {
          // ... other options
          // creates separate indexes for these base paths
          createIndexesByPaths: ["android", "ios", "web", ...]
        }
      ]

AND/OR

      [
        require.resolve("@easyops-cn/docusaurus-search-local"),
        {
          // ... other options
          // deep: false won't pick up any nested folders, while deep:true picks up all nested folders
          createIndexesByPaths: [{path: "android", deep: false}, {path: "android/legacy", deep: true}] 
        }
      ]

I think both mechanisms would be useful. I was looking at the source code to see how this could be implemented. I am slightly confused about where the index is getting generated as well as how the autocomplete component works. Thanks!

@weareoutman
Copy link
Member

There is a WIP branch, I couldn't finish it yet. I will continue this work this weekend if I got time.

@hs-saunved
Copy link
Author

Checked the commits. Do you think it's a good idea to provide the user an option to give explicit groups of base paths like in my comment example above?

@weareoutman
Copy link
Member

Yes, I think an option with explicit paths would be better, it's also easier to implement.

@weareoutman
Copy link
Member

@hs-saunved Try v0.33.0

@hs-saunved
Copy link
Author

Hi @weareoutman
The functionality is working as expected. However, there are two small things that need to be fixed/checked:

Issue 1

  • Let's say you are in the android context
  • You switch to the ios context by clicking on an internal link
  • The search context is still set to android, and the results show up from android
  • This is only fixed if you refresh the page
  • Ideally the context should be set whenever the user switches base paths imo

Issue 2

  • For pages which are not specified in the searchContextByPaths key - I believe the search bar should stay hidden. Same for other pages where the base path has not been explicitly specified
  • E.g. for the home page, the search bar is visible and is doing a global search even if I haven't explicitly added it

Please let me know if you'd like me to create separate issues for this or if this is okay. Thank you!

@weareoutman
Copy link
Member

weareoutman commented Oct 17, 2022

Issue 1 should have already been handled, needs investigation.

For issue 2, I think we'd better to provide a new option, currently all pages which are not specified, will be considered as with a search context of ROOT.

@weareoutman
Copy link
Member

@hs-saunved Try v.0.33.1 with a new option: hideSearchBarWithNoSearchContext: true

@hs-saunved
Copy link
Author

@weareoutman It is working as expected! I will test this out over the week and report any potential issues or optimizations. Thank you very much!

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

Successfully merging a pull request may close this issue.

2 participants