Switching from scratch to append for a search index

For a fuse search implementation, I was generating the needed index.json via the standard scratch method. @bep mentioned using append in a different context so I tried it.

The original was:

{{- $.Scratch.Add "index" slice -}}
{{- range where .Site.RegularPages "Type" "not in"  (slice "jf2feed" "json") -}}
{{- $.Scratch.Add "index" (dict "title" .Title "ref" .Permalink "tags" .Params.tags "content" .Plain) -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

The shiny new code for my index.json using append is:

{{- $index := slice -}}
{{- range where .Site.RegularPages "Type" "not in"  (slice "jf2feed" "json") -}}
  {{- $index = $index | append (dict "title" .Title "ref" .Permalink "tags" .Params.tags "content" .Plain) -}}
{{- end -}}
{{- $index | jsonify -}}

Both generate the same thing, but FYI, the index.json looks like:

[
    {
        "content": "Ars says Nest died at Google I/O 2019. The current unbridled investment culture where product ecosystems are bought then destroyed, should make you nervous. 😬 Any great or disruptive product or service is an acquisition target, because what business owner can resist quick riches. It assumes our acceptance and trust of the massive companies doing the acquiring. Do you really trust them?\n",
        "ref": "http://localhost:1313/2019/05/11/1557529874/",
        "tags": null,
        "title": "RC Logr 20190511 081114"
    },
    {
        "content": "Reading this article from EasyDNS about Network Solutions scammy business practices, I am reminded that I moved our domains and my client domains off Network Solutions for just these reasons. I have not regretted the decision. Better to avoid anything netsol. 😖\n",
        "ref": "http://localhost:1313/2019/05/09/1557355812/",
        "tags": null,
        "title": "RC Logr 20190509 075011"
    },
    {
        "content": "Todd Austin and colleagues at The U of Michigan, have developed the unhackable chip «MORPHEUS», which randomly shuffles bits of its own code, defeating hackers by changing even while they try to exploit it. They say it is akin to a Rubiks Cube that changes itself every time you blink. 🤖\n",
        "ref": "http://localhost:1313/2019/05/08/1557270922/",
        "tags": null,
        "title": "RC Logr 20190508 081521"
    },
...

Feedback welcome if I’m using append incorrectly.

4 Likes

So, it’s correct?

it’s exactly the same. Just the first time I used it so I was wondering if there is some better way.

No, that would be “my way”, too.

4 Likes

Thanks for the confirmation. Hopefully it helps someone.