# January 9th, 2020

# Document Search with Algolia

Algolia provides a simple to use, full-text search API; that lucky for us, is supported by the default VuePress theme.

In order to integrate with that functionality we need to make an account with Algolia and create an index. Then we need to copy our "Seach-Only API Key", "Application ID", and chosen index name into our VuePress theme config.

# VuePress config.js

themeConfig: {
  algolia: {
    apiKey: '692c04c8f1e443e098cfb2511ab8c99b',
    appId: '2SM2BJA0PV',
    indexName: 'ethanaa'
  }
}
1
2
3
4
5
6
7

Next we need to create a docsearch-scraper config that will instruct the Algolia scraper on how to index our site.

# docsearch-scraper CONFIG

{
  "index_name": "ethanaa",
  "start_urls": [
    "https://ethanaa.com/"
  ],
  "stop_urls": [],
  "selectors": {
    "lvl0": {
      "selector": "p.sidebar-heading.open",
      "global": true,
      "default_value": "Documentation"
    },
    "lvl1": ".theme-default-content h1",
    "lvl2": ".theme-default-content h2",
    "lvl3": ".theme-default-content h3",
    "lvl4": ".theme-default-content h4",
    "lvl5": ".theme-default-content h5",
    "text": ".theme-default-content p, .theme-default-content li",
    "lang": {
      "selector": "/html/@lang",
      "type": "xpath",
      "global": true,
      "default_value": "en-US"
    }
  },
  "strip_chars": " .,;:#",
  "custom_settings": {
    "attributesForFaceting": [
      "lang"
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

Save the config to a file and then use jq to reduce it to a single line:

cat ethanaa.json | jq -r tostring
1

Create a .env file:

APPLICATION_ID=2SM2BJA0PV
API_KEY=${Admin API Key}
CONFIG={"index_name":"ethanaa","start_urls":["https://ethanaa.com/"],"stop_urls":[],"selectors":{"lvl0":{"selector":"p.sidebar-heading.open","global":true,"default_value":"Documentation"},"lvl1":".theme-default-content h1","lvl2":".theme-default-content h2","lvl3":".theme-default-content h3","lvl4":".theme-default-content h4","lvl5":".theme-default-content h5","text":".theme-default-content p, .theme-default-content li","lang":{"selector":"/html/@lang","type":"xpath","global":true,"default_value":"en-US"}},"strip_chars":" .,;:#","custom_settings":{"attributesForFaceting":["lang"]}}
1
2
3

Finally, run the docsearch-scraper docker image against your .env file:

docker run -it --env-file=.env algolia/docsearch-scraper
1

The site will then be scraped and the index updated in Algolia.

> DocSearch: https://ethanaa.com/ 111 records)
> DocSearch: https://ethanaa.com/blog/ 72 records)

Nb hits: 183
1
2
3
4

Algolia Index

Give the search bar a try to see the results in action!

Last Updated: 1/12/2020, 8:59:38 PM