Loading...

Loading...

Search Configuration

This page is the configuration reference for search: the config blocks of the three engines (Local / Algolia / Pagefind), how the default engine is chosen and switched, and multi-language result filtering with unified highlighting. For usage steps (opening the popup, shortcuts) see Layout & Search.

The authoritative source is the search block in userConfig/_config.tmp.yml, injected into the theme config at build time.

Reading guide: every section follows 「How to enable → Common options → All tunable keys → Boundaries & conflicts」 — check the Effect first, then the Corresponding config that produces it, and copy it to use.

1. Engine Overview

Purpose: know which three indexes exist, and where each one’s artifacts land.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

1.1 How to enable

Effect: once the search popup is open, typing a keyword shows results; the popup also has an engine dropdown for on-the-spot switching, and all three engines share the same result-page layout.

Corresponding config (minimal setup, all three enabled):

search:
  default: "algolia"
  algolia:
    enable: true
  local:
    enable: true
  pagefind:
    enable: true

1.2 Common options

EngineConfig keyIndex location
Localsearch.local/search.json (generated by theme generator)
Algoliasearch.algoliaremote index (pushed at build time)
Pagefindsearch.pagefindpublic/pagefind/ (scanned after build)

① Keep only local search

Effect: no external service is needed; the index ships as an in-site JSON file, but the first search has to download it.

Corresponding config:

search:
  default: "local"
  local:
    enable: true
  algolia:
    enable: false
  pagefind:
    enable: false

1.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
search.defaultstring"algolia"local / algolia / pagefindDefault engine
search.local.enablebooltruetrue / falseLocal search switch
search.algolia.enablebooltruetrue / falseAlgolia switch
search.pagefind.enablebooltruetrue / falsePagefind switch

1.4 Boundaries & conflicts

  • All three can be enabled at once and switched via the dropdown in the popup; disabling an engine removes it from the dropdown.
  • Pagefind needs an extra scan after the build (see §5); otherwise that engine has no index.
  • Algolia needs the index pushed at build time; credentials are separated as described in §4.

1.5 Summary

  • Minimal setup: enable only local
  • Want zero services: local or pagefind
  • Want relevance/fault tolerance: algolia
  • Don’t change only default without enabling engines: it degrades automatically

2. Default Engine & Switching

Purpose: decide which engine the popup uses first, and how a reader’s switch is remembered.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

2.1 How to enable

Effect: opening the search popup searches with the engine named by search.default; after a reader switches engines in the dropdown, the next open keeps their choice.

Corresponding config:

search:
  default: "local"

2.2 Common options

① Change the default engine

Effect: the popup’s preferred search backend changes, and result ordering and matching change with it.

Corresponding config:

search:
  default: "pagefind"

② Persist the reader’s choice

Effect: after switching engines the choice is written to localStorage, so it need not be picked again.

Corresponding config: none — the frontend handles it automatically.

2.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
search.defaultstring"algolia"local / algolia / pagefindDefault engine
  • Resolution order: localStorage['search_engine'] memory → search.default → Algolia → first enabled engine.
  • If the default engine has enable: false, it degrades to the first enabled engine and logs a warning.
  • Switch entry: the engine dropdown inside the search popup (#searchEngineMenu, items data-engine=local|algolia|pagefind); switching destroys the old engine and inits the new one, and the choice is written to localStorage.

2.4 Boundaries & conflicts

  • localStorage has the highest priority — after changing search.default, returning readers still use the engine they picked last (expected behavior).
  • If every engine is disabled the popup shows no results, with no fallback message.

2.5 Summary

  • Minimal setup: default + enable: true on the matching engine
  • Changed the default but nothing happened: clear localStorage['search_engine'] first
  • Degradation warning: means the default engine is not enabled

3. Local Search search.local

Purpose: search in the browser using a theme-generated JSON index, with no external service.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

3.1 How to enable

Effect: the build produces /search.json; opening the popup and typing shows results (trigger: auto) with hits highlighted; the first search downloads the index.

Corresponding config:

search:
  local:
    enable: true
    path: /search.json
    field: [post, wiki]
    content: true
    trigger: auto

3.2 Common options

① Search titles only (smaller index)

Effect: the index shrinks noticeably and loads faster, but body keywords are no longer findable.

Corresponding config:

search:
  local:
    content: false

② Widen the search scope

Effect: pages (page) and wiki (wiki) also enter the results, not just posts.

Corresponding config:

search:
  local:
    field: all

③ Change the trigger mode

Effect: with manual, typing does not search; you must press Enter or click the button.

Corresponding config:

search:
  local:
    trigger: manual

④ Snippets per article

Effect: 1 shows one hit snippet per post, -1 shows all of them.

Corresponding config:

search:
  local:
    top_n_per_article: -1

3.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
search.local.enablebooltruetrue / falseMaster switch
search.local.pathstring/search.jsonoutput pathIndex file location
search.local.fieldstring / array[post, wiki]post / page / wiki / all / array combinationSearch scope
search.local.contentbooltruetrue / falseWhether to index body text (false = titles only)
search.local.formatstringstriptagsstriptags / rawBody text format in the index
search.local.triggerstringautoauto / manualTrigger mode
search.local.top_n_per_articlenumber1positive integer / -1 (all)Hit snippets per article
search.local.unescapeboolfalsetrue / falseUnescape HTML entities (reserved, not wired up)
search.local.preloadboolfalsetrue / falsePrefetch the index on page load (reserved, not wired up)

3.4 Boundaries & conflicts

  • unescape / preload are reserved options, not wired up in the current implementation; changing them has no effect.
  • format: raw keeps HTML, making the index noticeably larger.
  • With content: false, body keywords cannot be found — only titles match.

3.5 Summary

  • Minimal setup: enable: true (keep the rest at defaults)
  • Index too large: content: false or narrow field
  • Want instant open: wait for preload to be wired up, or switch to Pagefind
  • Don’t fiddle with reserved options: unescape/preload currently do nothing

4. Algolia search.algolia

Purpose: remote index search with better relevance ranking and fault tolerance; requires a push at build time.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

4.1 How to enable

Effect: search goes through the Algolia service, and results carry excerpts and timing stats; the index is pushed at build time, so new posts are unfindable until pushed.

Corresponding config:

search:
  algolia:
    enable: true
    applicationID: "your App ID"
    apiKey: "your Search-Only API Key"
    indexName: "blog"

4.2 Common options

① Control the indexed body length

Effect: the larger content_length, the wider the searchable body range, but the larger each record — records over 10KB are rejected by Algolia.

Corresponding config:

search:
  algolia:
    content_length: 2500

② Control excerpt length

Effect: the length of the excerpt snippet in results changes.

Corresponding config:

search:
  algolia:
    excerpt_length: 300

③ Results per page and batch size

Effect: the number of results per page and the push batch size at build time change.

Corresponding config:

search:
  algolia:
    batch_size: 1000
    hits:
      per_page: 10

④ UI copy

Effect: the search input placeholder, the no-results message and the result-stats copy change.

Corresponding config:

search:
  algolia:
    labels:
      input_placeholder: "Search for Posts"
      hits_empty: "We didn't find any results for the search: ${query}"
      hits_stats: "${hits} results found in ${time} ms"

4.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
search.algolia.enablebooltruetrue / falseMaster switch
search.algolia.applicationIDstring—Algolia App IDApplication identifier
search.algolia.apiKeystring—Search-Only API KeyFrontend search credential (safe to expose)
search.algolia.indexNamestring—index nameTarget index
search.algolia.content_lengthnumber2500positive integer (chars); 2000–2500 recommended for CJKBody truncation length
search.algolia.excerpt_lengthnumber300positive integer (chars); 150–300 recommendedExcerpt truncation length
search.algolia.batch_sizenumber1000positive integer (whole batch ≤ 10MB)Push batch size
search.algolia.hits.per_pagenumber10positive integerResults per page
search.algolia.labels.input_placeholderstring—any stringInput placeholder
search.algolia.labels.hits_emptystring—string containing ${query}No-results message
search.algolia.labels.hits_statsstring—contains ${hits}/${time}Result stats copy
deploy.algolia_write_keystring—Write API KeyBuild-time push credential (never injected into the frontend)
  • content_length: number of body plain-text characters to keep; excerpt_length: number of excerpt characters to keep.
  • batch_size: push batch size; hits.per_page: results per page.
  • labels.*: input_placeholder / hits_empty / hits_stats.
  • The write credential is separated from the frontend: build-time pushes use deploy.algolia_write_key, which is never injected into the frontend.

4.4 Boundaries & conflicts

  • Algolia has a hard per-record limit of 10KB (10240 bytes); anything larger is rejected on push (HTTP 400). UTF-8 CJK takes 3 bytes per character, and after subtracting the overhead of title/tags fields (about 500B): roughly 3000 CJK characters, or about 9000 ASCII characters. content_length: 25000 is guaranteed to fail.
  • apiKey is the frontend search credential and is safe to expose; deploy.algolia_write_key is the write credential and must stay on the build side only.
  • If you change content without rebuilding and pushing, search results still come from the old index.

4.5 Summary

  • Minimal setup: enable + applicationID + apiKey + indexName
  • Push returns 400: content_length is too large — cut it to the 10KB limit
  • New posts unfindable: the index was not pushed
  • Never put the write credential into apiKey

5. Pagefind search.pagefind

Purpose: a purely static index generated by scanning public/ after the build, searched in the browser with no server and no external network.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

5.1 How to enable

Effect: index files appear under public/pagefind/, and searching happens entirely in the frontend; encrypted posts do not appear in results.

Corresponding config:

search:
  pagefind:
    enable: true
    output_subdir: pagefind

Build step: after hexo generate and before deploy, run npx pagefind --site public --output-subdir pagefind.

5.2 Common options

① Change the index subdirectory

Effect: the index is output to public/<name>/, and the search request path changes accordingly.

Corresponding config:

search:
  pagefind:
    output_subdir: myindex

5.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
search.pagefind.enablebooltruetrue / falseMaster switch
search.pagefind.output_subdirstringpagefindsubdirectory name relative to public/Index output location
  • output_subdir: index subdirectory (relative to public/), default pagefind.
  • Build step: after hexo generate and before deploy, run npx pagefind --site public --output-subdir pagefind.
  • Encrypted posts are excluded automatically via conditional data-pagefind-body rendering.

5.4 Boundaries & conflicts

  • Avoid directory names starting with an underscore: GitHub Pages (Jekyll) ignores directories/files starting with _.
  • Running only hexo generate without pagefind leaves this engine with no index and no results.
  • The index is a build artifact; post updates require re-scanning.

5.5 Summary

  • Minimal setup: enable: true + run pagefind once after the build
  • Nothing found: confirm public/pagefind/ exists
  • Directory name: don’t start it with _
  • Encrypted posts: excluded automatically, no config needed

6. Multi-language & Highlighting

Purpose: result grouping and language preference on multi-language sites, plus the highlighting implementation shared by all three engines.

Learning path: How to enable → Common options → All tunable keys → Boundaries & conflicts

6.1 How to enable

Effect: search results show the current language version first and other languages after it; hits are wrapped in <mark>.

Corresponding config: no separate switch — it works automatically with the preference language block and the search config.

6.2 Common options

① Language preference

Effect: results are ordered by the language the reader chose last.

Corresponding config:

preference:
  default_lang: zh-cn

The reader preference is stored in localStorage['wiki_lang']; invalid values fall back to the default language.

6.3 All tunable keys

KeyTypeDefaultAllowed values / rangeDescription
preference.default_langstringzh-cnan enabled languageFallback language when the preference is invalid
search.local.fieldstring / array[post, wiki]see §3Decides which language versions enter the index
  • Result filtering: applyLangFilter → groupAndFilter in search.js, grouping by the path’s language prefix.
  • Language preference: read from localStorage['wiki_lang']; invalid values fall back to the default language.
  • Display priority: preferred language → default language → first hit.
  • Highlighting: all three engines share SearchEngine._highlight, wrapping hits in <mark> with HTML escaping (XSS-safe).

6.4 Boundaries & conflicts

  • Language grouping is decided by the path prefix; a translated page missing lang is grouped under the default language.
  • Highlighting always goes through the shared SearchEngine._highlight; changing a single engine’s highlight styles has no effect.

6.5 Summary

  • Defaults are correct: no extra configuration needed
  • Results in a jumbled language order: check preference.default_lang and the translated pages’ lang
  • Highlight styles: change the shared _highlight, not a single engine
Comments
Data Loading...