什麼事是docsearch
Algolia DocSearch 是一個文件搜尋引擎,它可以讓開發人員和用戶輕鬆地搜尋並發現特定專案的相關文件。它由搜尋引擎服務公司 Algolia 提供支援,旨在與文件網站配合使用,提供快速、準確和相關的搜尋功能。DocSearch 會爬取並索引網站上的文件,然後提供搜尋界面供用戶找到所需的內容。它通常用於軟體庫、框架和其他技術文件中。
前言
經過了好幾十天的等待最終docsearch還是拒絕了我的申請,原因是因為我們網站不是技術文檔 ,且DOCSEARCH有缺點就是不會在你更新時爬蟲而是在固定時間這意味著你肯能有段時間搜尋不到你最新的內容
cmfcmf/docusaurus-search-local
因為經docsearch拒絕後我們經過因為經docsearch拒絕後我們經過了長時間搜尋,最後選定了一款search工具cmfcmf/docusaurus-search-local,一開始它看起來很棒且沒有廣告,但我們發現它雖然本地搜尋,但他對中文繁體很不友善,如此影片:https://www.youtube.com/watch?v=qqp3LWsmPlc 而且內部還是簡體中文
最終解決方案
我們團隊突然想到有個東西較github action,那就對了我就在每次更新我的網站時自動讓在github action運行這串
docker run -it --env-file=.env -e "CONFIG=$(cat /path/to/your/config.json | jq -r tostring)" algolia/docsearch-scraper
代碼不就好了嗎?
最終代碼
首先在代碼庫創建conifg.json 內容是
config.json
{
  "index_name": "你的index name",
  "start_urls": [
    "你的網站url"
  ],
  "sitemap_urls": [
    "https://www.ssangyongsports.org/sitemap.xml"
  ],
  "sitemap_alternate_links": true,
  "stop_urls": [
  ],
  "selectors": {
    "lvl0": {
      "selector": "(//ul[contains(@class,'menu__list')]//a[contains(@class, 'menu__link menu__link--sublist menu__link--active')]/text() | //nav[contains(@class, 'navbar')]//a[contains(@class, 'navbar__link--active')]/text())[last()]",
      "type": "xpath",
      "global": true,
      "default_value": "Documentation"
    },
    "lvl1": "header h1",
    "lvl2": "article h2",
    "lvl3": "article h3",
    "lvl4": "article h4",
    "lvl5": "article h5, article td:first-child",
    "lvl6": "article h6",
    "text": "article p, article li, article td:last-child"
  },
  "strip_chars": " .,;:#",
  "custom_settings": {
    "separatorsToIndex": "_",
    "attributesForFaceting": [
      "language",
      "version",
      "type",
      "docusaurus_tag"
    ],
    "attributesToRetrieve": [
      "hierarchy",
      "content",
      "anchor",
      "url",
      "url_without_anchor",
      "type"
    ]
  }
}
在來在.github/workflows 上創建docsearch-crawler.yml 內容是
.github/workflows/docsearch-crawler.yml
name: docsearch抓取
on: 
  push:
    branches:
      - main
  
jobs:
  deploy:
   name: docsearch抓取
on: 
  push:
    branches:
      - main
jobs:
  deploy:
    name: docsearch抓取
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          node-version: 18
          cache: yarn
      - name: docsearch抓取
        env:
          ALGOLIA_APP_ID: "你的app id"
          ALGOLIA_API_KEY: "你的API KEY"
        run: |
          docker run \
          -e ALGOLIA_APP_ID -e ALGOLIA_API_KEY \
          -e CONFIG="$(cat config.json)" \
          algolia/docsearch-scraper
限制
如果你是使用自行運行DOCSEARCH方法的話要注意ALOGLIA免費的搜尋限制 ,如果可以申請成功還是建議你使用他們的因為沒有搜尋限制
