mirror of
https://github.com/McShelby/hugo-theme-relearn.git
synced 2024-11-23 07:47:54 +00:00
search: adding orama demo #407
This commit is contained in:
parent
b44434ce99
commit
fc960aa5ea
3 changed files with 122 additions and 58 deletions
|
@ -1 +1 @@
|
||||||
7.1.1+28fce6b04c414523280c53ee02f9f3a94d9d23da
|
7.1.1+b44434ce9957d65f7a7052bfa2db201395d3c960
|
60
static/js/orama-adapter.js
Normal file
60
static/js/orama-adapter.js
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
import {
|
||||||
|
create,
|
||||||
|
search as oramaSearch,
|
||||||
|
insertMultiple,
|
||||||
|
} from "https://cdn.jsdelivr.net/npm/@orama/orama@latest/+esm";
|
||||||
|
// "https://unpkg.com/browse/@orama/orama@latest/dist/esm/index.js";
|
||||||
|
// https://cdn.jsdelivr.net/npm/@orama/orama@3.0.1/dist/esm/index.js
|
||||||
|
|
||||||
|
|
||||||
|
//import { createTokenizer } from '@orama/tokenizers/japanese'
|
||||||
|
//import { stopwords as japaneseStopwords } from "@orama/stopwords/japanese";
|
||||||
|
|
||||||
|
let searchEngine = null;
|
||||||
|
|
||||||
|
async function init() {
|
||||||
|
async function initIndex( index ){
|
||||||
|
searchEngine = await create({
|
||||||
|
schema: {
|
||||||
|
title: 'string',
|
||||||
|
content: 'string',
|
||||||
|
uri: 'string',
|
||||||
|
breadcrumb: 'string',
|
||||||
|
description: 'string',
|
||||||
|
tags: 'string[]',
|
||||||
|
},
|
||||||
|
/*
|
||||||
|
defaultLanguage: 'french',
|
||||||
|
components: {
|
||||||
|
tokenizer: {
|
||||||
|
stemmingFn: stemmer,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
*/
|
||||||
|
});
|
||||||
|
await insertMultiple(searchEngine, index);
|
||||||
|
|
||||||
|
window.relearn.isSearchEngineReady = true;
|
||||||
|
window.relearn.executeInitialSearch();
|
||||||
|
}
|
||||||
|
|
||||||
|
if( window.index_js_url ){
|
||||||
|
var js = document.createElement("script");
|
||||||
|
js.src = index_js_url;
|
||||||
|
js.setAttribute("async", "");
|
||||||
|
js.onload = function(){
|
||||||
|
initIndex(relearn_searchindex);
|
||||||
|
};
|
||||||
|
js.onerror = function(e){
|
||||||
|
console.error('Error getting Hugo index file');
|
||||||
|
};
|
||||||
|
document.head.appendChild(js);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function search( term ){
|
||||||
|
const searchResponse = await oramaSearch(searchEngine, {term: term, properties: '*'});
|
||||||
|
return searchResponse.hits.map( hit => ({ page: hit.document }) );
|
||||||
|
}
|
||||||
|
|
||||||
|
export { init, search };
|
|
@ -1,4 +1,5 @@
|
||||||
import { init, search } from './lunr-adapter.js';
|
//import { init, search } from './lunr-adapter.js';
|
||||||
|
import { init, search } from './orama-adapter.js';
|
||||||
|
|
||||||
(function(){
|
(function(){
|
||||||
|
|
||||||
|
@ -87,7 +88,8 @@ function executeSearch( value ) {
|
||||||
var hint = document.querySelector('.searchhint');
|
var hint = document.querySelector('.searchhint');
|
||||||
hint.innerText = '';
|
hint.innerText = '';
|
||||||
results.textContent = '';
|
results.textContent = '';
|
||||||
var a = search( value );
|
(async function(){
|
||||||
|
var a = await search( value );
|
||||||
if( a.length ){
|
if( a.length ){
|
||||||
hint.innerText = resolvePlaceholders( window.T_N_results_found, [ value, a.length ] );
|
hint.innerText = resolvePlaceholders( window.T_N_results_found, [ value, a.length ] );
|
||||||
a.forEach( function(item){
|
a.forEach( function(item){
|
||||||
|
@ -142,6 +144,7 @@ function executeSearch( value ) {
|
||||||
}, 10 );
|
}, 10 );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
function initSearchAfterLoad(){
|
function initSearchAfterLoad(){
|
||||||
|
@ -158,8 +161,9 @@ function initSearchAfterLoad(){
|
||||||
selectorToInsert: 'search:has(.searchbox)',
|
selectorToInsert: 'search:has(.searchbox)',
|
||||||
selector: '#R-search-by',
|
selector: '#R-search-by',
|
||||||
/* source is the callback to perform the search */
|
/* source is the callback to perform the search */
|
||||||
source: function( term, response ) {
|
source: async function( term, response ) {
|
||||||
response( search( term ) );
|
let a = await search( term )
|
||||||
|
response( a );
|
||||||
},
|
},
|
||||||
/* renderItem displays individual search results */
|
/* renderItem displays individual search results */
|
||||||
renderItem: function( item, term ) {
|
renderItem: function( item, term ) {
|
||||||
|
|
Loading…
Reference in a new issue