dogsheep / dogsheep-beta

Build a search index across content from multiple SQLite database tables and run faceted searches against it using Datasette
https://dogsheep.github.io/
188 stars 6 forks source link

Add search highlighting snippets #29

Open simonw opened 4 years ago

simonw commented 4 years ago

Like on https://til.simonwillison.net/til/search?q=Snippet

simonw commented 4 years ago

Might just be a documented pattern for how to configure this in YAML templates.

simonw commented 3 years ago

I think I can do this as a cunning trick in display_sql. Consider this example query: https://til.simonwillison.net/tils?sql=select%0D%0A++path%2C%0D%0A++snippet%28til_fts%2C+-1%2C+%27b4de2a49c8%27%2C+%278c94a2ed4b%27%2C+%27...%27%2C+60%29+as+snippet%0D%0Afrom%0D%0A++til%0D%0A++join+til_fts+on+til.rowid+%3D+til_fts.rowid%0D%0Awhere%0D%0A++til_fts+match+escape_fts%28%3Aq%29%0D%0A++and+path+%3D+%27asgi_lifespan-test-httpx.md%27%0D%0A&q=pytest

select
  path,
  snippet(til_fts, -1, 'b4de2a49c8', '8c94a2ed4b', '...', 60) as snippet
from
  til
  join til_fts on til.rowid = til_fts.rowid
where
  til_fts match escape_fts(:q)
  and path = 'asgi_lifespan-test-httpx.md'

The and path = 'asgi_lifespan-test-httpx.md' bit means we only get back a specific document - but the snippet highlighting is applied to it.

simonw commented 3 years ago

To do this I'll need the search term to be passed to the display_sql SQL query: https://github.com/dogsheep/dogsheep-beta/blob/4890ec87b5e2ec48940f32c9ad1f5aae25c75a4d/dogsheep_beta/__init__.py#L164-L171

simonw commented 3 years ago

Should I pass any other details to the display_sql here as well?

simonw commented 3 years ago

I'm just going to add q for the moment.