Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search highlighting snippets #29

Open
simonw opened this issue Oct 19, 2020 · 5 comments
Open

Add search highlighting snippets #29

simonw opened this issue Oct 19, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Collaborator

simonw commented Oct 19, 2020

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

@simonw simonw added the enhancement New feature or request label Oct 19, 2020
@simonw
Copy link
Collaborator Author

simonw commented Oct 19, 2020

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

@simonw
Copy link
Collaborator Author

simonw commented Dec 16, 2020

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
Copy link
Collaborator Author

simonw commented Dec 16, 2020

To do this I'll need the search term to be passed to the display_sql SQL query:

if meta.get("display_sql"):
db = datasette.get_database(type_.split(".")[0])
display_results = await db.execute(
meta["display_sql"], {"key": result["key"]}
)
first = display_results.first()
if first:
result["display"] = dict(first)

@simonw
Copy link
Collaborator Author

simonw commented Dec 16, 2020

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

@simonw
Copy link
Collaborator Author

simonw commented Dec 16, 2020

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

simonw added a commit that referenced this issue Dec 16, 2020
simonw added a commit that referenced this issue Dec 16, 2020
simonw added a commit to simonw/datasette.io that referenced this issue Dec 16, 2020
@simonw simonw closed this as completed Aug 26, 2021
@simonw simonw reopened this Aug 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant