It’s important that the column names of the FTS entity match the corresponding columns in the content entity. The contentEntity parameter points to the Launch class defined in the same file, making this a content-less table that indexes the name and details fields of the Launch entity. Open the Launch.kt file in the db package, and add a new FTS entity class to it with the table name = Launch::class) Feel free to play around with the others, such as the one for custom tokenizers. The FTS annotations support quite a few configuration parameters, but you’ll use only contentEntity in this app. Such entities support only String fields, except for row ID and language ID fields, which can be integers. Room generates tables for classes annotated with On adding an additional annotation of to the entity class, Room generates a virtual table for it instead. Pick one depending on what version of FTS you want in your database. The and annotations shipped with Room can be used to leverage FTS features. They contain the full-text index only for the table they refer to.įor this tutorial, you’ll create a content-less FTS table to index details of the SpaceX launches in the database. Such FTS tables are called content-less because they store no records. They can also be made to index the data in another table using the content option. You can write data to these tables directly. FTS TablesįTS tables can only store TEXT columns, but they internally have an INTEGER rowid column as their primary key. From now on, the tutorial will refer to them as FTS Tables. Virtual Tables make it easy to work with the full text index, because you can read from them as if they were regular SQL tables. In general, you can do anything with a virtual table that you can do with an ordinary table, except create indices or triggers on them. Quoting the official SQLite documentation,Ī virtual table is an interface to an external storage or computation engine that appears to be a table but doesn’t actually store information in the database file. Users interact with it through a Virtual Table. It provides the basis for all operations supported by FTS. Virtual TablesįTS indexes data in a special format called the full-text-index. With the basics out of the way, now look at how FTS is implemented. FTS5 isn’t supported on SQLite shipped with older versions of Android, so Room doesn’t support it.For databases containing hundreds of thousands of entries, this can be problematic. FTS indices occupy more disk space, leading to a larger database file.It is more complicated to use than pattern matching.Room supports only FTS3 and FTS4, and it is recommended to use FTS4 in most cases.Īs with any technology, FTS has a few drawbacks, too: Makes it possible to retrieve additional metadata about each match for custom ranking algorithmsįTS was added to SQLite as a set of extensions and has received a few updates over the years (FTS3, FTS4 and FTS5).Also matches any discrete tokens that can be extracted from the search query.Can match against multiple columns in a row.Is much faster and efficient, especially over large datasets.FTS offers several benefits over that, including that it: However, in Android, it is much more common to search SQLite databases using the LIKE operator (aka pattern matching), due to its ease of use. Its ability to search a large number of documents quickly has led to its wide adoption in search engines, word processors and databases. Full Text Searchįull Text Search (or FTS) is a search technique in which every word stored in a text document is compared against the given query to find matches. First, though, take a quick look at what FTS is. The starter app uses the SQL LIKE operator to find matches. The results update automatically as you type. Go ahead and try searching for something. Each launch has a unique name and a small description. You’ll see the following screen:Īt the top is a search field, followed by a list of SpaceX launches. Then, open the starter project in Android Studio to discover Find My Launch, your sandbox for playing with FTS.īuild and run the app. If you’re new to this, please read the tutorials on Room, and Coroutines with Room Persistence Library first.ĭownload the starter project by clicking on the Download Materials button at the top or bottom of the tutorial. Note: This tutorial assumes you’re familiar with managing databases with Room.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |