Skip to content

Commit

Permalink
search: adding orama demo #407
Browse files Browse the repository at this point in the history
  • Loading branch information
McShelby committed Oct 25, 2024
1 parent b44434c commit fc960aa
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 59 deletions.
2 changes: 1 addition & 1 deletion layouts/partials/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.1.1+28fce6b04c414523280c53ee02f9f3a94d9d23da
7.1.1+b44434ce9957d65f7a7052bfa2db201395d3c960
60 changes: 60 additions & 0 deletions static/js/orama-adapter.js
Original file line number Diff line number Diff line change
@@ -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/[email protected]/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 };
120 changes: 62 additions & 58 deletions static/js/search.js
Original file line number Diff line number Diff line change
@@ -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(){

Expand Down Expand Up @@ -87,61 +88,63 @@ function executeSearch( value ) {
var hint = document.querySelector('.searchhint');
hint.innerText = '';
results.textContent = '';
var a = search( value );
if( a.length ){
hint.innerText = resolvePlaceholders( window.T_N_results_found, [ value, a.length ] );
a.forEach( function(item){
var page = item.page;
var context = [];
if( item.matches ){
var numContextWords = 10;
var contextPattern = '(?:\\S+ +){0,' + numContextWords + '}\\S*\\b(?:' +
item.matches.map( function(match){return match.replace(/\W/g, '\\$&')} ).join('|') +
')\\b\\S*(?: +\\S+){0,' + numContextWords + '}';
context = page.content.match(new RegExp(contextPattern, 'i'));
}
var divsuggestion = document.createElement('a');
divsuggestion.className = 'autocomplete-suggestion';
divsuggestion.setAttribute('data-term', value);
divsuggestion.setAttribute('data-title', page.title);
divsuggestion.setAttribute('href', window.relearn.relBaseUri + page.uri);
divsuggestion.setAttribute('data-context', context);
var divtitle = document.createElement('div');
divtitle.className = 'title';
divtitle.innerText = '» ' + page.title;
divsuggestion.appendChild( divtitle );
var divbreadcrumb = document.createElement('div');
divbreadcrumb.className = 'breadcrumbs';
divbreadcrumb.innerText = (page.breadcrumb || '');
divsuggestion.appendChild( divbreadcrumb );
if( context ){
var divcontext = document.createElement('div');
divcontext.className = 'context';
divcontext.innerText = (context || '');
divsuggestion.appendChild( divcontext );
}
results.appendChild( divsuggestion );
});
window.relearn.markSearch();
}
else if( value.length ) {
hint.innerText = resolvePlaceholders( window.T_No_results_found, [ value ] );
}
input.focus();
setTimeout( adjustContentWidth, 0 );

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

// if we are initiating search because of a browser history
// operation, we have to restore the scrolling postion the
// user previously has used; if this search isn't initiated
// by a browser history operation, it simply does nothing
var state = window.history.state || {};
state = Object.assign( {}, ( typeof state === 'object' ) ? state : {} );
if( state.hasOwnProperty( 'contentScrollTop' ) ){
window.setTimeout( function(){
elc.scrollTop = +state.contentScrollTop;
}, 10 );
return;
}
})();
}

function initSearchAfterLoad(){
Expand All @@ -158,8 +161,9 @@ function initSearchAfterLoad(){
selectorToInsert: 'search:has(.searchbox)',
selector: '#R-search-by',
/* source is the callback to perform the search */
source: function( term, response ) {
response( search( term ) );
source: async function( term, response ) {
let a = await search( term )
response( a );
},
/* renderItem displays individual search results */
renderItem: function( item, term ) {
Expand Down

0 comments on commit fc960aa

Please sign in to comment.