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

Issue 290 allow granular search by sections #301

Merged
merged 5 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -864,3 +864,14 @@ enableSearch = true
Live search works even for multilingual sites.

For Chinese-like languages, it may or may not work.

__Search Scope__

- Searching within a section will yield results from that section.

For example, if you have 2 `mainSections` 1/ vegetables 2/ fruits, searching in the fruits section will only produce results for that section.
- Searching outside a section will search the entire site.

For example, with the above `mainSections` setup, searching from the homepage will produce results from both sections and other pages of the site.

Future implementations may include a github-like feature i.e `search all site`. This PR will leave that to a future PR
chipzoller marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 12 additions & 7 deletions assets/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function initializeSearch(index) {
results = results.slice(0,12);
}
resultsFragment.appendChild(resultsTitle);
console.log(results);

results.forEach(function(result){
let item = createEl('a');
item.href = `${result.link}?query=${query}`;
Expand Down Expand Up @@ -85,17 +85,21 @@ function initializeSearch(index) {
showResults.appendChild(resultsFragment);
}

function search(searchTerm, scope = 'post', passive = false) {
function search(searchTerm, scope = null, passive = false) {
if(searchTerm.length) {
let rawResults = index.search(searchTerm);
rawResults = rawResults.map(function(result){
const score = result.score;
const resultItem = result.item;
resultItem.score = (parseFloat(score) * 50).toFixed(0);
return resultItem ;
}).filter(resultItem => {
return resultItem.section == scope;
});
})

if(scope) {
rawResults = rawResults.filter(resultItem => {
return resultItem.section == scope;
});
}

passive ? searchResults(rawResults, searchTerm, true) : searchResults(rawResults, searchTerm);

Expand All @@ -119,7 +123,8 @@ function initializeSearch(index) {
searchField.addEventListener('search', function(){
const searchTerm = searchField.value.trim().toLowerCase();
if(searchTerm.length) {
window.location.href = new URL(`search/?query=${searchTerm}&scope=${searchScope}`, rootURL).href;
const scopeParameter = searchScope ? `&scope=${searchScope}` : '';
window.location.href = new URL(`search/?query=${searchTerm}${ scopeParameter }`, rootURL).href;
}
});
}
Expand All @@ -142,7 +147,7 @@ function initializeSearch(index) {
// search actively after search page has loaded
const searchField = elem(searchFieldClass);

searchScope ? search(searchTerm, searchScope, true) : false;
search(searchTerm, searchScope, true);

if(searchField) {
searchField.addEventListener('input', function() {
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/search/widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{{- $simple = false }}
{{ end }}
<div class="search">
<input type="search" class="search_field form_field" placeholder='{{ $placeholder }}' id="find" autocomplete="off" data-scope='{{ delimit $params.mainSections "" }}'>
<input type="search" class="search_field form_field" placeholder='{{ $placeholder }}' id="find" autocomplete="off" data-scope='{{ .Section }}'>
<label for="find" class="search_label">
{{- partial "sprite" (dict "icon" "search") }}
</label>
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<aside class="sidebar">
<section class="sidebar_inner">
<br>
{{ partialCached "search/widget" . }}
{{ partial "search/widget" . }}
{{- $introDescription := $s.introDescription }}
{{- with .Params.introDescription }}
{{- $introDescription = . }}
Expand Down