mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
fts_table and fts_pk metadata configs, available for both tables and views
This commit is contained in:
parent
61f51d9ae6
commit
6bfc1507ff
2 changed files with 30 additions and 2 deletions
|
|
@ -309,7 +309,10 @@ class TableView(RowTableShared):
|
||||||
where_clauses, params = filters.build_where_clauses()
|
where_clauses, params = filters.build_where_clauses()
|
||||||
|
|
||||||
# _search support:
|
# _search support:
|
||||||
fts_table = info[name]["tables"].get(table, {}).get("fts_table")
|
fts_table = table_metadata.get(
|
||||||
|
"fts_table", info[name]["tables"].get(table, {}).get("fts_table")
|
||||||
|
)
|
||||||
|
fts_pk = table_metadata.get("fts_pk", "rowid")
|
||||||
search_args = dict(
|
search_args = dict(
|
||||||
pair for pair in special_args.items() if pair[0].startswith("_search")
|
pair for pair in special_args.items() if pair[0].startswith("_search")
|
||||||
)
|
)
|
||||||
|
|
@ -320,8 +323,9 @@ class TableView(RowTableShared):
|
||||||
# Simple ?_search=xxx
|
# Simple ?_search=xxx
|
||||||
search = search_args["_search"]
|
search = search_args["_search"]
|
||||||
where_clauses.append(
|
where_clauses.append(
|
||||||
"rowid in (select rowid from {fts_table} where {fts_table} match :search)".format(
|
"{fts_pk} in (select rowid from {fts_table} where {fts_table} match :search)".format(
|
||||||
fts_table=escape_sqlite(fts_table),
|
fts_table=escape_sqlite(fts_table),
|
||||||
|
fts_pk=escape_sqlite(fts_pk)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
human_description_extras.append('search matches "{}"'.format(search))
|
human_description_extras.append('search matches "{}"'.format(search))
|
||||||
|
|
|
||||||
|
|
@ -69,6 +69,30 @@ And then populate it like this:
|
||||||
|
|
||||||
You can use this technique to populate the full-text search index from any combination of tables and joins that makes sense for your project.
|
You can use this technique to populate the full-text search index from any combination of tables and joins that makes sense for your project.
|
||||||
|
|
||||||
|
Configuring full-text search for a table or view
|
||||||
|
------------------------------------------------
|
||||||
|
|
||||||
|
If a table has a corresponding FTS table set up using the ``content=`` argument to ``CREATE VIRTUAL TABLE`` shown above, Datasette will detect it automatically and add a search interface to the table page for that table.
|
||||||
|
|
||||||
|
You can also manually configure which table should be used for full-text search using :ref:`metadata`. You can set the associated FTS table for a specific table and you can also set one for a view - if you do that, the page for that SQL view will offer a search option.
|
||||||
|
|
||||||
|
The ``fts_table`` property can be used to specify an associated FTS table. If the primary key column in your table which was used to populate the FTS table is something other than ``rowid``, you can specify the column to use with the ``fts_pk`` property.
|
||||||
|
|
||||||
|
Here is an example which enables full-text search for a ``display_ads`` view which is defined against the ``ads`` table and hence needs to run FTS against the ``ads_fts`` table, using the ``id`` as the primary key::
|
||||||
|
|
||||||
|
{
|
||||||
|
"databases": {
|
||||||
|
"russian-ads": {
|
||||||
|
"tables": {
|
||||||
|
"display_ads": {
|
||||||
|
"fts_table": "ads_fts",
|
||||||
|
"fts_pk": "id"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Setting up full-text search using csvs-to-sqlite
|
Setting up full-text search using csvs-to-sqlite
|
||||||
------------------------------------------------
|
------------------------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue