Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/clarketm/hugo-elasticsearch

Generate Elasticsearch indexes for Hugo static sites by parsing front matter
https://github.com/clarketm/hugo-elasticsearch

cli command-line-tool elasticsearch hugo index indexer indexing indexing-engine toml yaml

Last synced: 3 months ago
JSON representation

Generate Elasticsearch indexes for Hugo static sites by parsing front matter

Awesome Lists containing this project

README

        

# [Hugo-Elasticsearch (HES)](https://blog.travismclarke.com/project/hugo-elasticsearch/)

[![npm-badge](https://img.shields.io/npm/v/hugo-elasticsearch.svg)](https://www.npmjs.com/package/hugo-elasticsearch)
[![codacy-badge](https://api.codacy.com/project/badge/Grade/1ce2505fd16c4e5ab284c7b36b666a08)](https://www.codacy.com/app/clarketm/hugo-elasticsearch?utm_source=github.com&utm_medium=referral&utm_content=clarketm/hugo-elasticsearch&utm_campaign=Badge_Grade)
[![circleci-badge](https://circleci.com/gh/clarketm/hugo-elasticsearch.svg?style=shield)](https://circleci.com/gh/clarketm/hugo-elasticsearch)

Generate [Elasticsearch](https://www.elastic.co/products/elasticsearch) indexes for [Hugo](https://gohugo.io/) static sites by parsing front matter.







## Installation

### Install with npm
```bash
$ npm install hugo-elasticsearch
```

## Demo

![usage demo](https://github.com/clarketm/hugo-elasticsearch/blob/master/usage.gif)

## Usage

### CLI
```bash

NAME:
hugo-elasticsearch
hes (alias)

SYNOPSIS:
hes [ opts... ]

DESCRIPTION:
Generate Elasticsearch indexes from Hugo front matter.

OPTIONS:
-i, --input path Input path. (default: "content/**")
-o, --output path Output path. (default: "public/elasticsearch.json")
-l, --language lang Language [toml | yaml]. (default: "toml")
-d, --delimiter delim Delimiter [toml: +++ | yaml: ---]. (optional)
-n, --name name Index name. (optional)

```

##### Long form
```bash
$ hugo-elasticsearch \
--input "content/**" \
--output "public/elasticsearch.json" \
--language "toml" \
--delimiter "+++" \
--index-name "posts"
```

##### Short form
```bash
$ hes \
-i "content/**" \
-o "public/elasticsearch.json" \
-l "toml" \
-d "+++" \
-n "posts"
```

### NPM Scripts
```javascript
...
"scripts": {
"index": "hes -i 'content/**' -o 'public/elasticsearch.json'"
"index:toml": "hes -i 'content/toml/**' -o 'public/toml/elasticsearch.json' -l 'toml' -d '+++'"
"index:yaml": "hes -i 'content/yaml/**' -o 'public/yaml/elasticsearch.json' -l 'yaml' -d '---'"
},
...
```

### API
```javascript
const hes = require('hugo-elasticsearch');

const Indexer = new hes({
input: 'content/blog/**',
output: 'public/static/elasticsearch.json',
language: 'yaml',
delimiter: '---',
indexName: 'posts'
});

// Create index
Indexer.index()

// Setters
Indexer.setInput('content/blog/**');
Indexer.setOutput('public/static/elasticsearch.json');
Indexer.setLanguage('yaml');
Indexer.setDelimiter('---');
Indexer.setIndexName('posts');
```

## Example

#### 1. Create a directory named `content`.
```bash
$ mkdir 'content'
```

#### 2. Create a markdown file with `toml` front matter in a file named `content/test-toml.md`.
```markdown
$ cat > 'content/test-toml.md' < {
return new Promise((resolve, reject) => {
let lines = [];

fs.createReadStream("./public/elasticsearch.json")
.pipe(ndjson.parse())
.on("data", line => lines.push(line))
.on("end", () => resolve(lines))
.on("error", err => reject(err));
});
};

// Perform the bulk index operations in a single API call.
const bulkUpload = async () => {
const json = await this.fetchBulkJson();
return await client.bulk({ body: json });
};
```

**[Java](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html), [Python](https://www.elastic.co/guide/en/elasticsearch/client/python-api/current/index.html), [Ruby](https://www.elastic.co/guide/en/elasticsearch/client/ruby-api/current/index.html), ...**
> Although the bulk upload examples above are only for cUrl and JavaScript, this format will work seamlessly with **any** one of the *numerous* Elasticsearch [clients](https://www.elastic.co/guide/en/elasticsearch/client/index.html).

#### 5. You content is now successfully indexed in Elasticsearch 👍. Happy elastic searching!

> Refer to the [`content`](content) directory in the root of *this* project for examples of both *yaml* and *toml* content (i.e. `.md` files).

> Refer to the [`public`](public) directory in the root of *this* project for examples of ndjson files (i.e. Elasticsearch index files) generated from both *yaml* and *toml* content.

## Sites using hugo-elasticsearch
* https://blog.travismclarke.com/

## License
Apache-2.0 © [Travis Clarke](https://www.travismclarke.com/)