| 1 |
.. _facets: |
| 2 |
|
| 3 |
Facets |
| 4 |
====== |
| 5 |
|
| 6 |
Datasette facets can be used to add a faceted browse interface to any database table. |
| 7 |
With facets, tables are displayed along with a summary showing the most common values in specified columns. |
| 8 |
These values can be selected to further filter the table. |
| 9 |
|
| 10 |
Here's `an example <https://congress-legislators.datasettes.com/legislators/legislator_terms?_facet=type&_facet=party&_facet=state&_facet_size=10>`__: |
| 11 |
|
| •12 |
`Screenshot showing facets against a table of congressional legislators. Suggested facets include state_rank and start and end dates, and the displayed facets are state, party and type. Each facet lists values along with a count of rows for each value. <https://raw.githubusercontent.com/simonw/datasette-screenshots/0.62/non-retina/faceting-details.png>`__ |
| 13 |
|
| 14 |
Facets can be specified in two ways: using query string parameters, or in ``metadata.json`` configuration for the table. |
| 15 |
|
| 16 |
Facets in query strings |
| 17 |
----------------------- |
| 18 |
|
| 19 |
To turn on faceting for specific columns on a Datasette table view, add one or more ``_facet=COLUMN`` parameters to the URL. |
| 20 |
For example, if you want to turn on facets for the ``city_id`` and ``state`` columns, construct a URL that looks like this:: |
| 21 |
|
| 22 |
/dbname/tablename?_facet=state&_facet=city_id |
| 23 |
|
| 24 |
This works for both the HTML interface and the ``.json`` view. |
| 25 |
When enabled, facets will cause a ``facet_results`` block to be added to the JSON output, looking something like this: |
| 26 |
|
| 27 |
.. code-block:: json |
| 28 |
|
| 29 |
{ |
| 30 |
"state": { |
| 31 |
"name": "state", |
| 32 |
"results": [ |
| 33 |
{ |
| 34 |
"value": "CA", |
| 35 |
"label": "CA", |
| 36 |
"count": 10, |
| 37 |
"toggle_url": "http://...?_facet=city_id&_facet=state&state=CA", |
| 38 |
"selected": false |
| 39 |
}, |
| 40 |
{ |
| 41 |
"value": "MI", |
| 42 |
"label": "MI", |
| 43 |
"count": 4, |
| 44 |
"toggle_url": "http://...?_facet=city_id&_facet=state&state=MI", |
| 45 |
"selected": false |
| 46 |
}, |
| 47 |
{ |
| 48 |
"value": "MC", |
| 49 |
"label": "MC", |
| 50 |
"count": 1, |
| 51 |
"toggle_url": "http://...?_facet=city_id&_facet=state&state=MC", |
| 52 |
"selected": false |
| 53 |
} |
| 54 |
], |
| 55 |
"truncated": false |
| 56 |
} |
| 57 |
"city_id": { |
| 58 |
"name": "city_id", |
| 59 |
"results": [ |
| 60 |
{ |
| 61 |
"value": 1, |
| 62 |
"label": "San Francisco", |
| 63 |
"count": 6, |
| 64 |
"toggle_url": "http://...?_facet=city_id&_facet=state&city_id=1", |
| 65 |
"selected": false |
| 66 |
}, |
| 67 |
{ |
| 68 |
"value": 2, |
| 69 |
"label": "Los Angeles", |
| 70 |
"count": 4, |
| 71 |
"toggle_url": "http://...?_facet=city_id&_facet=state&city_id=2", |
| 72 |
"selected": false |
| 73 |
}, |
| 74 |
{ |
| 75 |
"value": 3, |
| 76 |
"label": "Detroit", |
| 77 |
"count": 4, |
| 78 |
"toggle_url": "http://...?_facet=city_id&_facet=state&city_id=3", |
| 79 |
"selected": false |
| 80 |
}, |
| 81 |
{ |
| 82 |
"value": 4, |
| 83 |
"label": "Memnonia", |
| 84 |
"count": 1, |
| 85 |
"toggle_url": "http://...?_facet=city_id&_facet=state&city_id=4", |
| 86 |
"selected": false |
| 87 |
} |
| 88 |
], |
| 89 |
"truncated": false |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
If Datasette detects that a column is a foreign key, the ``"label"`` property will be automatically derived from the detected label column on the referenced table. |
| 94 |
|
| 95 |
The default number of facet results returned is 30, controlled by the :ref:`setting_default_facet_size` setting. |
| 96 |
You can increase this on an individual page by adding ``?_facet_size=100`` to the query string, up to a maximum of :ref:`setting_max_returned_rows` (which defaults to 1000). |
| 97 |
|
| 98 |
.. _facets_metadata: |
| 99 |
|
| 100 |
Facets in metadata.json |
| 101 |
----------------------- |
| 102 |
|
| 103 |
You can turn facets on by default for specific tables by adding them to a ``"facets"`` key in a Datasette :ref:`metadata` file. |
| 104 |
|
| 105 |
Here's an example that turns on faceting by default for the ``qLegalStatus`` column in the ``Street_Tree_List`` table in the ``sf-trees`` database: |
| 106 |
|
| 107 |
.. code-block:: json |
| 108 |
|
| 109 |
{ |
| 110 |
"databases": { |
| 111 |
"sf-trees": { |
| 112 |
"tables": { |
| 113 |
"Street_Tree_List": { |
| 114 |
"facets": ["qLegalStatus"] |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
Facets defined in this way will always be shown in the interface and returned in the API, regardless of the ``_facet`` arguments passed to the view. |
| 122 |
|
| 123 |
You can specify :ref:`array <facet_by_json_array>` or :ref:`date <facet_by_date>` facets in metadata using JSON objects with a single key of ``array`` or ``date`` and a value specifying the column, like this: |
| 124 |
|
| 125 |
.. code-block:: json |
| 126 |
|
| 127 |
{ |
| 128 |
"facets": [ |
| 129 |
{"array": "tags"}, |
| 130 |
{"date": "created"} |
| 131 |
] |
| 132 |
} |
| 133 |
|
| 134 |
You can change the default facet size (the number of results shown for each facet) for a table using ``facet_size``: |
| 135 |
|
| 136 |
.. code-block:: json |
| 137 |
|
| 138 |
{ |
| 139 |
"databases": { |
| 140 |
"sf-trees": { |
| 141 |
"tables": { |
| 142 |
"Street_Tree_List": { |
| 143 |
"facets": ["qLegalStatus"], |
| 144 |
"facet_size": 10 |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
Suggested facets |
| 152 |
---------------- |
| 153 |
|
| 154 |
Datasette's table UI will suggest facets for the user to apply, based on the following criteria: |
| 155 |
|
| 156 |
For the currently filtered data are there any columns which, if applied as a facet... |
| 157 |
|
| 158 |
* Will return 30 or less unique options |
| 159 |
* Will return more than one unique option |
| 160 |
* Will return less unique options than the total number of filtered rows |
| 161 |
* And the query used to evaluate this criteria can be completed in under 50ms |
| 162 |
|
| 163 |
That last point is particularly important: Datasette runs a query for every column that is displayed on a page, which could get expensive - so to avoid slow load times it sets a time limit of just 50ms for each of those queries. |
| 164 |
This means suggested facets are unlikely to appear for tables with millions of records in them. |
| 165 |
|
| 166 |
Speeding up facets with indexes |
| 167 |
------------------------------- |
| 168 |
|
| 169 |
The performance of facets can be greatly improved by adding indexes on the columns you wish to facet by. |
| 170 |
Adding indexes can be performed using the ``sqlite3`` command-line utility. Here's how to add an index on the ``state`` column in a table called ``Food_Trucks``:: |
| 171 |
|
| 172 |
$ sqlite3 mydatabase.db |
| 173 |
SQLite version 3.19.3 2017-06-27 16:48:08 |
| 174 |
Enter ".help" for usage hints. |
| 175 |
sqlite> CREATE INDEX Food_Trucks_state ON Food_Trucks("state"); |
| 176 |
|
| 177 |
Or using the `sqlite-utils <https://sqlite-utils.datasette.io/en/stable/cli.html#creating-indexes>`__ command-line utility:: |
| 178 |
|
| 179 |
$ sqlite-utils create-index mydatabase.db Food_Trucks state |
| 180 |
|
| 181 |
.. _facet_by_json_array: |
| 182 |
|
| 183 |
Facet by JSON array |
| 184 |
------------------- |
| 185 |
|
| 186 |
If your SQLite installation provides the ``json1`` extension (you can check using :ref:`JsonDataView_versions`) Datasette will automatically detect columns that contain JSON arrays of values and offer a faceting interface against those columns. |
| 187 |
|
| 188 |
This is useful for modelling things like tags without needing to break them out into a new table. |
| 189 |
|
| 190 |
Example here: `latest.datasette.io/fixtures/facetable?_facet_array=tags <https://latest.datasette.io/fixtures/facetable?_facet_array=tags>`__ |
| 191 |
|
| 192 |
.. _facet_by_date: |
| 193 |
|
| 194 |
Facet by date |
| 195 |
------------- |
| 196 |
|
| 197 |
If Datasette finds any columns that contain dates in the first 100 values, it will offer a faceting interface against the dates of those values. |
| 198 |
This works especially well against timestamp values such as ``2019-03-01 12:44:00``. |
| 199 |
|
| 200 |
Example here: `latest.datasette.io/fixtures/facetable?_facet_date=created <https://latest.datasette.io/fixtures/facetable?_facet_date=created>`__ |