Debian Codegraph @

docs/full_text_search.rst

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 .. _full_text_search:
2
3 Full-text search
4 ================
5
6 SQLite includes `a powerful mechanism for enabling full-text search <https://www.sqlite.org/fts3.html>`_ against SQLite records. Datasette can detect if a table has had full-text search configured for it in the underlying database and display a search interface for filtering that table.
7
8 Here's `an example search <https://register-of-members-interests.datasettes.com/regmem/items?_search=hamper&_sort_desc=date>`__:
9
10 .. image:: https://raw.githubusercontent.com/simonw/datasette-screenshots/0.62/non-retina/regmem-search.png
11 :alt: Screenshot showing a search for hampers against a table full of items - 453 results are returned.
12
13 Datasette automatically detects which tables have been configured for full-text search.
14
15 .. _full_text_search_table_view_api:
16
17 The table page and table view API
18 ---------------------------------
19
20 Table views that support full-text search can be queried using the ``?_search=TERMS`` query string parameter. This will run the search against content from all of the columns that have been included in the index.
21
22 Try this example: `fara.datasettes.com/fara/FARA_All_ShortForms?_search=manafort <https://fara.datasettes.com/fara/FARA_All_ShortForms?_search=manafort>`__
23
24 SQLite full-text search supports wildcards. This means you can easily implement prefix auto-complete by including an asterisk at the end of the search term - for example::
25
26 /dbname/tablename/?_search=rob*
27
28 This will return all records containing at least one word that starts with the letters ``rob``.
29
30 You can also run searches against just the content of a specific named column by using ``_search_COLNAME=TERMS`` - for example, this would search for just rows where the ``name`` column in the FTS index mentions ``Sarah``::
31
32 /dbname/tablename/?_search_name=Sarah
33
34
35 .. _full_text_search_advanced_queries:
36
37 Advanced SQLite search queries
38 ------------------------------
39
40 SQLite full-text search includes support for `a variety of advanced queries <https://www.sqlite.org/fts5.html#full_text_query_syntax>`__, including ``AND``, ``OR``, ``NOT`` and ``NEAR``.
41
42 By default Datasette disables these features to ensure they do not cause errors or confusion for users who are not aware of them. You can disable this escaping and use the advanced queries by adding ``&_searchmode=raw`` to the table page query string.
43
44 If you want to enable these operators by default for a specific table, you can do so by adding ``"searchmode": "raw"`` to the metadata configuration for that table, see :ref:`full_text_search_table_or_view`.
45
46 If that option has been specified in the table metadata but you want to over-ride it and return to the default behavior you can append ``&_searchmode=escaped`` to the query string.
47
48 .. _full_text_search_table_or_view:
49
50 Configuring full-text search for a table or view
51 ------------------------------------------------
52
53 If a table has a corresponding FTS table set up using the ``content=`` argument to ``CREATE VIRTUAL TABLE`` shown below, Datasette will detect it automatically and add a search interface to the table page for that table.
54
55 You can also manually configure which table should be used for full-text search using query string parameters or :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.
56
57 Use ``?_fts_table=x`` to over-ride the FTS table for a specific page. If the primary key was something other than ``rowid`` you can use ``?_fts_pk=col`` to set that as well. This is particularly useful for views, for example:
58
59 https://latest.datasette.io/fixtures/searchable_view?_fts_table=searchable_fts&_fts_pk=pk
60
61 The ``fts_table`` metadata 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.
62
63 The ``"searchmode": "raw"`` property can be used to default the table to accepting SQLite advanced search operators, as described in :ref:`full_text_search_advanced_queries`.
64
65 Here is an example which enables full-text search (with SQLite advanced search operators) 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:
66
67 .. code-block:: json
68
69 {
70 "databases": {
71 "russian-ads": {
72 "tables": {
73 "display_ads": {
74 "fts_table": "ads_fts",
75 "fts_pk": "id",
76 "searchmode": "raw"
77 }
78 }
79 }
80 }
81 }
82
83 .. _full_text_search_custom_sql:
84
85 Searches using custom SQL
86 -------------------------
87
88 You can include full-text search results in custom SQL queries. The general pattern with SQLite search is to run the search as a sub-select that returns rowid values, then include those rowids in another part of the query.
89
90 You can see the syntax for a basic search by running that search on a table page and then clicking "View and edit SQL" to see the underlying SQL. For example, consider this search for `manafort is the US FARA database <https://fara.datasettes.com/fara/FARA_All_ShortForms?_search=manafort>`_::
91
92 /fara/FARA_All_ShortForms?_search=manafort
93
94 If you click `View and edit SQL <https://fara.datasettes.com/fara?sql=select%0D%0A++rowid%2C%0D%0A++Short_Form_Termination_Date%2C%0D%0A++Short_Form_Date%2C%0D%0A++Short_Form_Last_Name%2C%0D%0A++Short_Form_First_Name%2C%0D%0A++Registration_Number%2C%0D%0A++Registration_Date%2C%0D%0A++Registrant_Name%2C%0D%0A++Address_1%2C%0D%0A++Address_2%2C%0D%0A++City%2C%0D%0A++State%2C%0D%0A++Zip%0D%0Afrom%0D%0A++FARA_All_ShortForms%0D%0Awhere%0D%0A++rowid+in+%28%0D%0A++++select%0D%0A++++++rowid%0D%0A++++from%0D%0A++++++FARA_All_ShortForms_fts%0D%0A++++where%0D%0A++++++FARA_All_ShortForms_fts+match+escape_fts%28%3Asearch%29%0D%0A++%29%0D%0Aorder+by%0D%0A++rowid%0D%0Alimit%0D%0A++101&search=manafort>`_ you'll see that the underlying SQL looks like this:
95
96 .. code-block:: sql
97
98 select
99 rowid,
100 Short_Form_Termination_Date,
101 Short_Form_Date,
102 Short_Form_Last_Name,
103 Short_Form_First_Name,
104 Registration_Number,
105 Registration_Date,
106 Registrant_Name,
107 Address_1,
108 Address_2,
109 City,
110 State,
111 Zip
112 from
113 FARA_All_ShortForms
114 where
115 rowid in (
116 select
117 rowid
118 from
119 FARA_All_ShortForms_fts
120 where
121 FARA_All_ShortForms_fts match escape_fts(:search)
122 )
123 order by
124 rowid
125 limit
126 101
127
128 .. _full_text_search_enabling:
129
130 Enabling full-text search for a SQLite table
131 --------------------------------------------
132
133 Datasette takes advantage of the `external content <https://www.sqlite.org/fts3.html#_external_content_fts4_tables_>`_ mechanism in SQLite, which allows a full-text search virtual table to be associated with the contents of another SQLite table.
134
135 To set up full-text search for a table, you need to do two things:
136
137 * Create a new FTS virtual table associated with your table
138 * Populate that FTS table with the data that you would like to be able to run searches against
139
140 Configuring FTS using sqlite-utils
141 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142
143 `sqlite-utils <https://sqlite-utils.datasette.io/>`__ is a CLI utility and Python library for manipulating SQLite databases. You can use `it from Python code <https://sqlite-utils.datasette.io/en/latest/python-api.html#enabling-full-text-search>`__ to configure FTS search, or you can achieve the same goal `using the accompanying command-line tool <https://sqlite-utils.datasette.io/en/latest/cli.html#configuring-full-text-search>`__.
144
145 Here's how to use ``sqlite-utils`` to enable full-text search for an ``items`` table across the ``name`` and ``description`` columns::
146
147 $ sqlite-utils enable-fts mydatabase.db items name description
148
149 Configuring FTS using csvs-to-sqlite
150 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
151
152 If your data starts out in CSV files, you can use Datasette's companion tool `csvs-to-sqlite <https://github.com/simonw/csvs-to-sqlite>`__ to convert that file into a SQLite database and enable full-text search on specific columns. For a file called ``items.csv`` where you want full-text search to operate against the ``name`` and ``description`` columns you would run the following::
153
154 $ csvs-to-sqlite items.csv items.db -f name -f description
155
156 Configuring FTS by hand
157 ~~~~~~~~~~~~~~~~~~~~~~~
158
159 We recommend using `sqlite-utils <https://sqlite-utils.datasette.io/>`__, but if you want to hand-roll a SQLite full-text search table you can do so using the following SQL.
160
161 To enable full-text search for a table called ``items`` that works against the ``name`` and ``description`` columns, you would run this SQL to create a new ``items_fts`` FTS virtual table:
162
163 .. code-block:: sql
164
165 CREATE VIRTUAL TABLE "items_fts" USING FTS4 (
166 name,
167 description,
168 content="items"
169 );
170
171 This creates a set of tables to power full-text search against ``items``. The new ``items_fts`` table will be detected by Datasette as the ``fts_table`` for the ``items`` table.
172
173 Creating the table is not enough: you also need to populate it with a copy of the data that you wish to make searchable. You can do that using the following SQL:
174
175 .. code-block:: sql
176
177 INSERT INTO "items_fts" (rowid, name, description)
178 SELECT rowid, name, description FROM items;
179
180 If your table has columns that are foreign key references to other tables you can include that data in your full-text search index using a join. Imagine the ``items`` table has a foreign key column called ``category_id`` which refers to a ``categories`` table - you could create a full-text search table like this:
181
182 .. code-block:: sql
183
184 CREATE VIRTUAL TABLE "items_fts" USING FTS4 (
185 name,
186 description,
187 category_name,
188 content="items"
189 );
190
191 And then populate it like this:
192
193 .. code-block:: sql
194
195 INSERT INTO "items_fts" (rowid, name, description, category_name)
196 SELECT items.rowid,
197 items.name,
198 items.description,
199 categories.name
200 FROM items JOIN categories ON items.category_id=categories.id;
201
202 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.
203
204 .. _full_text_search_fts_versions:
205
206 FTS versions
207 ------------
208
209 There are three different versions of the SQLite FTS module: FTS3, FTS4 and FTS5. You can tell which versions are supported by your instance of Datasette by checking the ``/-/versions`` page.
210
211 FTS5 is the most advanced module but may not be available in the SQLite version that is bundled with your Python installation. Most importantly, FTS5 is the only version that has the ability to order by search relevance without needing extra code.
212
213 If you can't be sure that FTS5 will be available, you should use FTS4.