Full Text Searching, The Book, and You!
The APK edition of The Busy Coder’s Guide to Android Development
offers full-text searching, via a SearchView
widget in the action
bar. Typing in a search expression and pressing Enter
(or clicking the magnifying glass button in the soft keyboard) will
bring up a set of search results, consisting of a snippet of text
surrounding the search hit, along with information regarding what
chapter the snippet comes from. Tapping on an item in the list
jumps you to the paragraph within the chapter corresponding to the
snippet that you tapped upon.
This is all powered by SQLite and an FTS3 table.
Since this is FTS3, and not some crude hand-rolled full-text search system of my own, you get a nice search syntax, supporting:
- simple terms
- wildcards (e.g.,
List*
to matchListView
,ListActivity
, …) - phrases (e.g.,
"balding guy"
) - AND, OR, NOT, and NEAR operators (e.g.,
dynamic NEAR fragment*
) - parentheses to enforce operator priorities
And, FTS3 is very fast. Even with all the prose in the book indexed, results come back in less than a second on decent hardware.
The good news is that this did not dramatically increase the size of the APK file. Partially, that is because much of the APK bulk comes from images, which are not indexed. Also, the index does not currently include the source code boxes, only the contents of paragraphs and bullets.
I create the FTS3 table as part of my book mastering process. I write
the book in a tweaked version of
MultiMarkdown, and I have
a series of Ruby scripts that generate (and further tweak) HTML
from the MultiMarkdown sources. Other Ruby scripts turn that HTML
into the PDF, EPUB, and APK editions (with the Kindle/MOBI edition
coming from the EPUB edition via Amazon’s kindlegen
tool). The
Ruby script for creating the APK edition:
- Creates an empty database with an empty FTS3 table
- Scans through all of the HTML and records all paragraphs and bullets in that table
- Packages the database in a ZIP file in
assets/
of the project - Compiles the APK
The app itself then uses SQLiteAssetHelper
to unpack the database and make it available for querying.
In a future book update, I will provide more coverage of using FTS3
from within Android, as well as using SearchView
for conducting
FTS3 searches.