search: adding orama demo #407
Some checks failed
docs-build-deployment / Run deploy (push) Has been cancelled
docs-build / Run build (push) Has been cancelled

This commit is contained in:
Sören Weber 2024-10-25 21:30:43 +02:00
parent b44434ce99
commit fc960aa5ea
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
3 changed files with 122 additions and 58 deletions

View file

@ -1 +1 @@
7.1.1+28fce6b04c414523280c53ee02f9f3a94d9d23da 7.1.1+b44434ce9957d65f7a7052bfa2db201395d3c960

View 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 };

View file

@ -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,61 +88,63 @@ 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(){
if( a.length ){ var a = await search( value );
hint.innerText = resolvePlaceholders( window.T_N_results_found, [ value, a.length ] ); if( a.length ){
a.forEach( function(item){ hint.innerText = resolvePlaceholders( window.T_N_results_found, [ value, a.length ] );
var page = item.page; a.forEach( function(item){
var context = []; var page = item.page;
if( item.matches ){ var context = [];
var numContextWords = 10; if( item.matches ){
var contextPattern = '(?:\\S+ +){0,' + numContextWords + '}\\S*\\b(?:' + var numContextWords = 10;
item.matches.map( function(match){return match.replace(/\W/g, '\\$&')} ).join('|') + var contextPattern = '(?:\\S+ +){0,' + numContextWords + '}\\S*\\b(?:' +
')\\b\\S*(?: +\\S+){0,' + numContextWords + '}'; item.matches.map( function(match){return match.replace(/\W/g, '\\$&')} ).join('|') +
context = page.content.match(new RegExp(contextPattern, 'i')); ')\\b\\S*(?: +\\S+){0,' + numContextWords + '}';
} context = page.content.match(new RegExp(contextPattern, 'i'));
var divsuggestion = document.createElement('a'); }
divsuggestion.className = 'autocomplete-suggestion'; var divsuggestion = document.createElement('a');
divsuggestion.setAttribute('data-term', value); divsuggestion.className = 'autocomplete-suggestion';
divsuggestion.setAttribute('data-title', page.title); divsuggestion.setAttribute('data-term', value);
divsuggestion.setAttribute('href', window.relearn.relBaseUri + page.uri); divsuggestion.setAttribute('data-title', page.title);
divsuggestion.setAttribute('data-context', context); divsuggestion.setAttribute('href', window.relearn.relBaseUri + page.uri);
var divtitle = document.createElement('div'); divsuggestion.setAttribute('data-context', context);
divtitle.className = 'title'; var divtitle = document.createElement('div');
divtitle.innerText = '» ' + page.title; divtitle.className = 'title';
divsuggestion.appendChild( divtitle ); divtitle.innerText = '» ' + page.title;
var divbreadcrumb = document.createElement('div'); divsuggestion.appendChild( divtitle );
divbreadcrumb.className = 'breadcrumbs'; var divbreadcrumb = document.createElement('div');
divbreadcrumb.innerText = (page.breadcrumb || ''); divbreadcrumb.className = 'breadcrumbs';
divsuggestion.appendChild( divbreadcrumb ); divbreadcrumb.innerText = (page.breadcrumb || '');
if( context ){ divsuggestion.appendChild( divbreadcrumb );
var divcontext = document.createElement('div'); if( context ){
divcontext.className = 'context'; var divcontext = document.createElement('div');
divcontext.innerText = (context || ''); divcontext.className = 'context';
divsuggestion.appendChild( divcontext ); divcontext.innerText = (context || '');
} divsuggestion.appendChild( divcontext );
results.appendChild( divsuggestion ); }
}); results.appendChild( divsuggestion );
window.relearn.markSearch(); });
} window.relearn.markSearch();
else if( value.length ) { }
hint.innerText = resolvePlaceholders( window.T_No_results_found, [ value ] ); else if( value.length ) {
} hint.innerText = resolvePlaceholders( window.T_No_results_found, [ value ] );
input.focus(); }
setTimeout( adjustContentWidth, 0 ); input.focus();
setTimeout( adjustContentWidth, 0 );
// if we are initiating search because of a browser history // if we are initiating search because of a browser history
// operation, we have to restore the scrolling postion the // operation, we have to restore the scrolling postion the
// user previously has used; if this search isn't initiated // user previously has used; if this search isn't initiated
// by a browser history operation, it simply does nothing // by a browser history operation, it simply does nothing
var state = window.history.state || {}; var state = window.history.state || {};
state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} ); state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} );
if( state.hasOwnProperty( 'contentScrollTop' ) ){ if( state.hasOwnProperty( 'contentScrollTop' ) ){
window.setTimeout( function(){ window.setTimeout( function(){
elc.scrollTop = +state.contentScrollTop; elc.scrollTop = +state.contentScrollTop;
}, 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 ) {