元のリンク: https://i18n.site/blog/tech/search
順序
数週間の開発を経て、i18n.site (純粋な静的マークダウン多言語翻訳および Web サイト構築ツール) は、純粋なフロントエンド全文検索をサポートするようになりました。
この記事では、i18n.site の純粋なフロントエンド全文検索の技術的な実装について共有します。 i18n.site にアクセスして検索機能を体験してください。
コードはオープンソースです: 検索カーネル / インタラクティブ インターフェイス
サーバーレス全文検索ソリューションの概要
ドキュメントや個人のブログなど、中小規模の純粋に静的な Web サイトの場合、独自に構築した全文検索バックエンドを構築するのは重すぎるため、サービス不要の全文検索がより一般的な選択肢です。
サーバーレス全文検索ソリューションは、次の 2 つの主要カテゴリに分類されます。
1 つ目は、全文検索用のフロントエンド コンポーネントを提供する algolia.com などのサードパーティの検索サービス プロバイダーです。
このようなサービスは検索ボリュームに基づいて料金を支払う必要があり、コンプライアンスの問題により中国本土のユーザーは利用できないことがよくあります。
オフラインやイントラネットでは使用できず、重大な制限があります。この記事ではこれ以上詳しく説明しません。
2 番目のカテゴリは、純粋なフロントエンド全文検索です。
現在、一般的な純粋なフロントエンド全文検索ツールには、lunrjs および ElasticLunr.js (lunrjs に基づく二次開発) が含まれます。
lunrjs にはインデックスを構築するための 2 つの方法があり、どちらも独自の問題があります。
- 事前に構築されたインデックス ファイル
索引には文書のすべての単語が含まれているため、サイズが大きくなります。
ドキュメントが追加または変更されるたびに、新しいインデックス ファイルをロードする必要があります。
これにより、ユーザーの待ち時間が増加し、大量の帯域幅が消費されます。
- ドキュメントを読み込み、その場でインデックスを構築する
インデックスの構築は計算集約型のタスクであり、アクセスのたびにインデックスを再構築すると顕著な遅延が発生し、ユーザー エクスペリエンスの低下につながる可能性があります。
lunrjs に加えて、次のような他の全文検索ソリューションもあります。
fusejs、文字列間の類似性を計算して検索します。
このソリューションはパフォーマンスが低く、全文検索には適していません (Fuse.js の長いクエリに 10 秒以上かかる、最適化する方法を参照してください)。
検索にブルーム フィルターを使用する TinySearch は、接頭辞検索 (例: goo を入力して Good または Google を検索する) を実行できず、オートコンプリート効果も実現できません。
既存のソリューションには欠点があるため、i18n.site は次の機能を備えた新しい純粋なフロントエンド全文検索ソリューションを開発しました。
- コンパクトなサイズで多言語検索をサポートします。検索カーネルは、gzip でパッケージ化した場合、わずか 6.9 KB です (比較すると、lunrjs は 25 KB)
- メモリ使用量が少なく、パフォーマンスが高速な IndexedDB に基づいた転置インデックスを構築します
- ドキュメントが追加/変更されると、追加または変更されたドキュメントのみが再インデックス化されるため、計算量が削減されます
- 接頭辞検索をサポートし、ユーザーの入力に応じて検索結果をリアルタイムに表示できます
- オフラインでの利用可能
i18n.site の技術実装の詳細は以下で紹介されます。
多言語単語の分割
単語のセグメンテーションは、すべての主流ブラウザでサポートされているブラウザのネイティブ Intl.Segmenter を使用します。
単語分割用の Coffeescript コードは次のとおりです:
SEG = new Intl.Segmenter 0, granularity: "word" seg = (txt) => r = [] for {segment} from SEG.segment(txt) for i from segment.split('.') i = i.trim() if i and !'|`'.includes(i) and !/\p{P}/u.test(i) r.push i r export default seg export segqy = (q) => seg q.toLocaleLowerCase()
場所:
-
/p{P}/ は、次のような句読点と一致する正規表現です。 " # $ % & ' ( ) * , - . / : ; ? @ [ ] ^ _ { | } ~. .
- split('.' )Firefox ブラウザの単語分割が分割されないためです。` .
インデックスの構築
IndexedDB に 5 つのオブジェクト ストレージ テーブルが作成されます:
- 単語: ID - 単語
- doc: ID - ドキュメント URL - ドキュメントのバージョン番号
- docWord: ドキュメント ID - 単語 ID の配列
- prefix: prefix - 単語 ID の配列
- rindex: 単語 ID - ドキュメント ID - 行番号の配列
ドキュメント URL とバージョン番号 ver の配列を渡すことにより、ドキュメント テーブルでドキュメントの存在がチェックされます。存在しない場合は、転置インデックスが作成されます。同時に、渡されなかったドキュメントの逆索引は削除されます。
この方法では、増分インデックス作成が可能になり、計算負荷が軽減されます。
In the front-end interface, a progress bar for index loading can be displayed to avoid lag during the initial load. See "Animated Progress Bar, Based on a Single progress + Pure CSS Implementation" English / Chinese.
IndexedDB High Concurrent Writing
The project is developed based on the asynchronous encapsulation of IndexedDB, idb.
IndexedDB reads and writes are asynchronous. When creating an index, documents are loaded concurrently to build the index.
To avoid data loss due to concurrent writes, you can refer to the following coffeescript code, which adds a ing cache between reading and writing to intercept competitive writes.
`coffee
pusher = =>
ing = new Map()
(table, id, val)=>
id_set = ing.get(id)
if id_set
id_set.add val
returnid_set = new Set([val]) ing.set id, id_set pre = await table.get(id) li = pre?.li or [] loop to_add = [...id_set] li.push(...to_add) await table.put({id,li}) for i from to_add id_set.delete i if not id_set.size ing.delete id break return
rindexPush = pusher()
prefixPush = pusher()
`Prefix Real-Time Search
To display search results in real-time as the user types, for example, showing words like words and work that start with wor when wor is entered.
The search kernel uses the prefix table for the last word after segmentation to find all words with that prefix and search sequentially.
An anti-shake function, debounce (implemented as follows), is used in the front-end interaction to reduce the frequency of searches triggered by user input, thus minimizing computational load.
js
export default (wait, func) => {
var timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(func.bind(this, ...args), wait);
};
}
Precision and Recall
The search first segments the keywords entered by the user.
Assuming there are N words after segmentation, the results are first returned with all keywords, followed by results with N-1, N-2, ..., 1 keywords.
The search results displayed first ensure query precision, while subsequent loaded results (click the "Load More" button) ensure recall.
On-Demand Loading
To improve response speed, the search uses the yield generator to implement on-demand loading, returning results after each limit query.
Note that after each yield, a new IndexedDB query transaction must be opened for the next search.
Prefix Real-Time Search
To display search results in real-time as the user types, for example, showing words like words and work that start with wor when wor is entered.
The search kernel uses the prefix table for the last word after segmentation to find all words with that prefix and search sequentially.
An anti-shake function, debounce (implemented as follows), is used in the front-end interaction to reduce the frequency of searches triggered by user input, thus minimizing computational load.
js
export default (wait, func) => {
var timeout;
return function(...args) {
clearTimeout(timeout);
timeout = setTimeout(func.bind(this, ...args), wait);
};
}
Offline Availability
The index table does not store the original text, only words, reducing storage space.
Highlighting search results requires reloading the original text, and using service worker can avoid repeated network requests.
Also, because service worker caches all articles, once a search is performed, the entire website, including search functionality, becomes offline available.
Optimization for Displaying MarkDown Documents
The pure front-end search solution provided by i18n.site is optimized for MarkDown documents.
When displaying search results, the chapter name is shown, and clicking navigates to that chapter.
Summary
The pure front-end implementation of inverted full-text search, without the need for a server, is very suitable for small to medium-sized websites such as documents and personal blogs.
i18n.site's open-source self-developed pure front-end search is compact, responsive, and addresses the various shortcomings of current pure front-end full-text search solutions, providing a better user experience.
以上が純粋なフロントエンド逆引き全文検索の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

JavaScript文字列置換法とFAQの詳細な説明 この記事では、javaScriptの文字列文字を置き換える2つの方法について説明します:内部JavaScriptコードとWebページの内部HTML。 JavaScriptコード内の文字列を交換します 最も直接的な方法は、置換()メソッドを使用することです。 str = str.replace( "find"、 "置換"); この方法は、最初の一致のみを置き換えます。すべての一致を置き換えるには、正規表現を使用して、グローバルフラグGを追加します。 str = str.replace(/fi

それで、あなたはここで、Ajaxと呼ばれるこのことについてすべてを学ぶ準備ができています。しかし、それは正確には何ですか? Ajaxという用語は、動的でインタラクティブなWebコンテンツを作成するために使用されるテクノロジーのゆるいグループ化を指します。 Ajaxという用語は、もともとJesse Jによって造られました

10の楽しいjQueryゲームプラグインして、あなたのウェブサイトをより魅力的にし、ユーザーの粘着性を高めます! Flashは依然としてカジュアルなWebゲームを開発するのに最適なソフトウェアですが、jQueryは驚くべき効果を生み出すこともできます。また、純粋なアクションフラッシュゲームに匹敵するものではありませんが、場合によってはブラウザで予期せぬ楽しみもできます。 jquery tic toeゲーム ゲームプログラミングの「Hello World」には、JQueryバージョンがあります。 ソースコード jQueryクレイジーワードコンポジションゲーム これは空白のゲームであり、単語の文脈を知らないために奇妙な結果を生み出すことができます。 ソースコード jquery鉱山の掃引ゲーム

記事では、JavaScriptライブラリの作成、公開、および維持について説明し、計画、開発、テスト、ドキュメント、およびプロモーション戦略に焦点を当てています。

このチュートリアルでは、jQueryを使用して魅惑的な視差の背景効果を作成する方法を示しています。 見事な視覚的な深さを作成するレイヤー画像を備えたヘッダーバナーを構築します。 更新されたプラグインは、jQuery 1.6.4以降で動作します。 ダウンロードしてください

この記事では、ブラウザでJavaScriptのパフォーマンスを最適化するための戦略について説明し、実行時間の短縮、ページの負荷速度への影響を最小限に抑えることに焦点を当てています。

Matter.jsは、JavaScriptで書かれた2D Rigid Body Physics Engineです。このライブラリは、ブラウザで2D物理学を簡単にシミュレートするのに役立ちます。剛体を作成し、質量、面積、密度などの物理的特性を割り当てる機能など、多くの機能を提供します。また、重力摩擦など、さまざまな種類の衝突や力をシミュレートすることもできます。 Matter.jsは、すべての主流ブラウザをサポートしています。さらに、タッチを検出し、応答性が高いため、モバイルデバイスに適しています。これらの機能はすべて、物理ベースの2Dゲームまたはシミュレーションを簡単に作成できるため、エンジンの使用方法を学ぶために時間をかける価値があります。このチュートリアルでは、このライブラリのインストールや使用法を含むこのライブラリの基本を取り上げ、

この記事では、JQueryとAjaxを使用して5秒ごとにDivのコンテンツを自動的に更新する方法を示しています。 この例は、RSSフィードからの最新のブログ投稿と、最後の更新タイムスタンプを取得して表示します。 読み込み画像はオプションです


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

SublimeText3 英語版
推奨: Win バージョン、コードプロンプトをサポート!

Dreamweaver Mac版
ビジュアル Web 開発ツール

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

ホットトピック



