We are building a website using the bigspring theme and we added lunr.js to search the content. We noticed that when the search text is in a blog post, then the page (blog post url) is included in the search results. But when the text exist in a page with many sections then the text is not returned.
For example we have created a “services” section with an all services page “_index.md” and service1.md, service2.md
index.md
---
title: "All Services"
subtitle: "Success catalysts"
# meta description
description: "Company services "
draft: false
# content section
section: "services"
---
service1.md
date: "2021-07-12"
draft: false
title: "Service1"
description: "Contact us"
icon: "fas fa-laptop-house"
layout: "layout-3"
######################### banner #####################
banner:
title: "Service1"
image: "images/products/02.jpg"
content : "something"
background_class: "bg-light"
button:
enable : true
label : "Try for Free"
link : "get-demo/"
######################### product_info #####################
product_info:
enable : true
title: "New experiments <br> Launch in minutes"
content: "Some lorel ipsum"
features:
- image: "images/products/01.jpg"
content : "##### For Football Teams
Adipiscing elit Consequat tristique eget amet, tempus eu at consecttur. Leo facilisi nunc viverra tellus. Ac laoreet sit vel consquat. consectetur adipiscing elit. tempus eu at consecttur.
<br><br>
##### For Basketball teams
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Consequat tristique eget amet, tempus eu at consecttur. Leo facilisi nunc viverra tellus. Ac laoreet sit vel consquat.
"
- image: "images/products/02.jpg"
content : "##### For Design Teams
Adipiscing elit Consequat tristique eget amet, tempus eu at consecttur. Leo facilisi nunc viverra tellus. Ac laoreet sit vel consquat. consectetur adipiscing elit. tempus eu at consecttur...
The text eg. “adipiscing” that exists in service1.md is not returned in search results.
Configuration
In config.toml:
[outputs]
home = ["HTML", "RSS", "JSON", "WebAppManifest"]
Code for indexing index.html:
<script>
window.store = {
{{ range .Site.Pages }}
"{{ .Permalink }}": {
"title": "{{ .Title }}",
"tags": [{{ range .Params.Tags }}"{{ . }}",{{ end }}],
"content": {{ .Content | plainify }},
"url": "{{ .Permalink }}"
},
{{ end }}
}
</script>
<script src="/js/lunr.min.js"></script>
<script src="/js/search.js"></script>
The search.js
function displayResults (results, store) {
const searchResults = document.getElementById('results')
if (results.length) {
let resultList = ''
for (const n in results) {
const item = store[results[n].ref]
resultList += `
<li>
<h2>
<a href="${item.url}">${item.title}</a>
</h2>'
<p>${item.content.substring(0, 150)}...</p>
<p>${item.tags}</p>
</li>
`;
}
searchResults.innerHTML = resultList
} else {
searchResults.innerHTML = 'No results found.'
}
}
const params = new URLSearchParams(window.location.search)
const query = params.get('query')
if (query) {
document.getElementById('search-input').setAttribute('value', query)
const idx = lunr(function () {
this.ref('id')
this.field('title', {
// boost search to 15
boost: 15
})
this.field('tags')
this.field('content', {
// boost search to 10
boost: 10
})
for (const key in window.store) {
this.add({
id: key,
title: window.store[key].title,
tags: window.store[key].category,
content: window.store[key].content
})
}
})
const results = idx.search(query)
displayResults(results, window.store)
document.getElementById('search-title').innerText = 'Search Results for ' + query
}
I have tried the .Pages and .Site.RegularPages options in index.html but didn’t work.
Do you have any suggestions?