Skip to content

Commit

Permalink
First attempt at unified search, /-/beta - refs #21
Browse files Browse the repository at this point in the history
Using new feature from dogsheep/dogsheep-beta#29
  • Loading branch information
simonw committed Dec 16, 2020
1 parent 7dcb0ee commit 5e321d6
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 2 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,20 @@ jobs:
run: |
sqlite-utils content.db 'drop table if exists news'
yaml-to-sqlite content.db news news.yaml
- name: Fetch documentation database for search index
run: curl -o docs-index.db https://latest-docs.datasette.io/docs.db
- name: Import stats.json
run: |
curl -O https://raw.githubusercontent.com/simonw/package-stats/main/stats.json
python build_stats.py content.db stats.json
- name: Build search index
run: dogsheep-beta index dogsheep-index.db dogsheep-beta.yml
- name: Soundness check
run: |
datasette . --get "/"
datasette . --get "/plugins"
datasette . --get "/news"
datasette . --get "/-/beta"
- name: Set up Cloud Run
uses: google-github-actions/setup-gcloud@master
with:
Expand All @@ -62,7 +67,7 @@ jobs:
run: |-
gcloud config set run/region us-central1
gcloud config set project datasette-222320
datasette publish cloudrun content.db \
datasette publish cloudrun content.db docs-index.db dogsheep-index.db \
--service datasette-io \
--template-dir=templates \
--metadata=metadata.yml \
Expand All @@ -73,4 +78,5 @@ jobs:
--install=python-dateutil \
--install=datasette-vega \
--install=datasette-atom \
--install=datasette-graphql
--install=datasette-graphql \
--install 'dogsheep-beta>=0.10'
90 changes: 90 additions & 0 deletions dogsheep-beta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
docs-index.db:
sections:
sql: |-
select
id as key,
title as title,
content as search_1,
null as timestamp,
1 as is_public
from
sections
display_sql: |-
select
page,
ref,
snippet(
sections_fts,
-1,
'b4de2a49c8',
'8c94a2ed4b',
'...',
60
) as snippet
from
sections
join sections_fts on sections.rowid = sections_fts.rowid
where
sections_fts match escape_fts(:q)
and id = :key
display: |-
<h3>Docs: <a href="https://docs.datasette.io/en/latest/{{ display.page }}.html#{{ display.ref }}">{{ title }}</a></h3>
<p>{{ display.snippet.replace('b4de2a49c8', '<strong style="background-color: yellow">').replace('8c94a2ed4b', '</strong>')|safe }}</p>
content.db:
plugins:
sql: |-
select
plugins.name as key,
plugins.name as title,
plugins.latest_release_at as timestamp,
plugins.description as search_1,
1 as is_public
from plugins
display: |-
<h3>Plugin: <a href="/plugins/{{ title }}">{{ title }}</a></h3>
<p>{{ search_1 }}</p>
releases:
sql: |-
select
releases.id as key,
repos.name || ' ' || releases.tag_name as title,
releases.published_at as timestamp,
releases.body as search_1,
1 as is_public
from
releases
join repos on releases.repo = repos.id
display_sql: |-
select
render_markdown(releases.body) as rendered_markdown,
html_url
from releases where id = :key
display: |-
<h3>Release: <a href="{{ display.html_url }}">{{ title }}</a></h3>
<p>{{ display.rendered_markdown|safe }}</p>
<p><small>Released {{ timestamp }}</small></p>
news:
sql: |-
select
date as key,
'Datasette News: ' || date as title,
date || 'T00:00:00-08:00' as timestamp,
group_concat(body, '
') as search_1,
1 as is_public
from
news
group by
date
order by
date desc
display_sql: |-
select
render_markdown(group_concat(body, '
')) as rendered_markdown
from news where date = :key
display: |-
<h3><a href="/news/{{ key }}">{{ title }}</a></h3>
<p>{{ display.rendered_markdown|safe }}</p>
5 changes: 5 additions & 0 deletions metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@ databases:
date desc
limit
20
plugins:
dogsheep-beta:
database: dogsheep-index
config_file: dogsheep-beta.yml
template_debug: true
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ datasette-template-sql
datasette-render-markdown
datasette-vega
datasette-atom
dogsheep-beta>=0.10
datasette-graphql
sqlite-utils
yaml-to-sqlite
Expand Down
113 changes: 113 additions & 0 deletions templates/beta.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{% extends "page_base.html" %}

{% block title %}Datasette search{% if q %}: {{ q }}{% endif %}{% endblock %}

{% block content %}
<style>
.result {
padding: 1em 0;
border-bottom: 1px solid #666;
}
.result img {
max-width: 100%;
}
.facet h2 {
font-size: 1em;
}
.facet .x:link,
.facet .x:visited,
.facet .x:hover,
.facet .x:focus,
.facet .x:active {
text-decoration: none;
}
.facet .label {
word-wrap: break-word;
}
aside {
float: right;
width: 30%;
}
section.results {
margin-right: 30%;
padding-right: 2em;
}
pre, blockquote {
white-space: pre-wrap;
}

.beta-search input {
border: 1px solid #ccc;
border-radius: 3px;
padding: 9px 4px;
margin-right: 1em;
}
.beta-search input[type="submit"] {
color: #fff;
background-color: #8484f4;
border-color: #0e0c82;
-webkit-appearance: button;
font-weight: 400;
cursor: pointer;
text-align: center;
vertical-align: middle;
border-width: 1px;
border-style: solid;
padding: .4em 0.8em;
font-size: 0.9rem;
line-height: 1;
border-radius: .25rem;
}
</style>
<h1>Search{% if q %}: {{ q }}{% endif %}</h1>

<form action="/-/beta" method="get" class="beta-search"><div>
<input type="search" name="q" value="{{ q }}" id="q" style="min-width: 50%">
{% if sorted_by != "newest" %}
<input type="hidden" name="sort" value="{{ sorted_by }}">
{% endif %}
<input type="submit" value="Search">
{% for hidden in hiddens %}
<input type="hidden" name="{{ hidden.name }}" value="{{ hidden.value }}">
{% endfor %}
</div></form>

<p>Got {{ intcomma(count) }} result{% if count != 1 %}s{% endif %}, sorted by
<strong>{{ sorted_by }}</strong> /
{% for other_sort_order in other_sort_orders %}
<a href="{{ other_sort_order.url }}">{{ other_sort_order.label }}</a>{% if not loop.last %} / {% endif %}
{% endfor %}
</p>

<aside>
{% if facets %}
{% for facet in facets %}
{% if facet.results %}
<div class="facet">
<h2>{{ facet.name }}</h2>
<ul>
{% for item in facet.results %}
<li{% if item.selected %} class="selected"{% endif %}>
{% if item.selected %}
<span class="label">{{ item.label }}</span> <a href="{{ item.toggle_url }}" class="x"></a>
{% else %}
<a href="{{ item.toggle_url }}" class="label">{{ item.label }}</a>
{% endif %}
- <span class="count">{{ intcomma(item.count) }}</span>
</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% endfor %}
{% endif %}
</aside>

<section class="results">
{% for result in results %}
<div class="result" data-table-key="{{ result.type }}:{{ result.key }}">
{{ result.output|safe }}
</div>
{% endfor %}
</section>
{% endblock %}

0 comments on commit 5e321d6

Please sign in to comment.