fts_table and fts_pk metadata configs, available for both tables and views

pull/358/head
Simon Willison 2018-08-05 17:44:47 -07:00
rodzic 5629aaca67
commit 7d0299edd4
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 17E2DEA2588B7F52
2 zmienionych plików z 30 dodań i 2 usunięć

Wyświetl plik

@ -309,7 +309,10 @@ class TableView(RowTableShared):
where_clauses, params = filters.build_where_clauses()
# _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(
pair for pair in special_args.items() if pair[0].startswith("_search")
)
@ -320,8 +323,9 @@ class TableView(RowTableShared):
# Simple ?_search=xxx
search = search_args["_search"]
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_pk=escape_sqlite(fts_pk)
)
)
search_descriptions.append('search matches "{}"'.format(search))

Wyświetl plik

@ -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.
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
------------------------------------------------