{
  "$schema": "./.schemas/page.schema.json",
  "title": "Search",
  "title_from": {
    "ref": "search",
    "value": {
      "template": "{param.q}"
    }
  },
  "icon": "magnifyingglass",
  "sidebar": false,
  "params": {
    "q": {
      "type": "query",
      "example": "insurance"
    }
  },
  "sources": {
    "search": {
      "url": "http://paperless.local:8000",
      "secret": "paperless",
      "auth": {
        "type": "header",
        "name": "Authorization",
        "value": "Token {secret}"
      },
      "every": "1h",
      "headers": {
        "Accept": "application/json; version=10"
      },
      "requests": {
        "hits": "/api/documents/?query={param.q}&page_size=25&fields=id,title,correspondent,document_type,created,__search_hit__",
        "names": "/api/correspondents/?ordering=-document_count&page_size=1000",
        "types": "/api/document_types/?ordering=-document_count&page_size=1000"
      },
      "derive": [
        {
          "name": "hits_c",
          "value": {
            "each": "hits.results",
            "lookup": {
              "from": "names.results",
              "by": "id",
              "on": "correspondent",
              "as": "c"
            },
            "fields": {
              "id": "id",
              "title": "title",
              "created": "created",
              "dt": "document_type",
              "cname": "c.name",
              "hl": "__search_hit__.highlights"
            }
          }
        },
        {
          "name": "hits_n",
          "value": {
            "each": "hits_c",
            "lookup": {
              "from": "types.results",
              "by": "id",
              "on": "dt",
              "as": "d"
            },
            "fields": {
              "id": "id",
              "title": "title",
              "created": "created",
              "cname": "cname",
              "tname": "d.name",
              "hl": "hl"
            }
          }
        },
        {
          "name": "hits_rows",
          "value": {
            "each": "hits_n",
            "fields": {
              "key": {
                "template": "{id|raw}"
              },
              "text": {
                "if": {
                  "path": "title",
                  "exists": true
                },
                "then": {
                  "template": "{title}"
                },
                "else": {
                  "template": "Untitled"
                }
              },
              "subtitle": {
                "if": {
                  "all": [
                    {
                      "path": "hl",
                      "exists": true
                    },
                    {
                      "path": "hl",
                      "not": ""
                    }
                  ]
                },
                "then": {
                  "if": {
                    "path": "cname",
                    "exists": true
                  },
                  "then": {
                    "template": "{cname} · {hl|plain}"
                  },
                  "else": {
                    "template": "{hl|plain}"
                  }
                },
                "else": {
                  "if": {
                    "path": "cname",
                    "exists": true
                  },
                  "then": {
                    "if": {
                      "path": "tname",
                      "exists": true
                    },
                    "then": {
                      "template": "{cname} · {tname}"
                    },
                    "else": {
                      "template": "{cname}"
                    }
                  },
                  "else": {
                    "if": {
                      "path": "tname",
                      "exists": true
                    },
                    "then": {
                      "template": "{tname}"
                    }
                  }
                }
              },
              "detail": {
                "template": "{created|day}"
              },
              "url": {
                "template": "http://paperless.local:8000/documents/{id|raw}/details"
              }
            }
          }
        }
      ]
    }
  },
  "cards": [
    {
      "id": "results",
      "type": "list",
      "max_items": 7,
      "items": [],
      "source": {
        "ref": "search",
        "empty": {
          "text": "No documents match",
          "when": {
            "path": "hits.count",
            "equals": 0
          }
        },
        "map": {
          "label": {
            "if": {
              "path": "hits.count",
              "gt": 25
            },
            "then": {
              "template": "Results · 25 of {hits.count}"
            },
            "else": {
              "if": {
                "path": "hits.count",
                "equals": 1
              },
              "then": {
                "template": "1 result"
              },
              "else": {
                "template": "{hits.count} results"
              }
            }
          },
          "items": {
            "each": "hits_rows",
            "fields": {
              "key": "key",
              "text": "text",
              "subtitle": "subtitle",
              "detail": "detail",
              "url": "url"
            }
          }
        }
      }
    }
  ]
}
