Search code examples
androidsqlitefts4

How to shift the displayed suggestions text to the matched word


I don't know if my question well written, sorry for it, but here's what I need.

I have an Android app with a search activity and my own dictionary for suggestions.

When I show a suggestion I use the item's name in the first row, and a second row with the item's description.

The problem is that there's only room for a few words in the second row and it's showing the description from the beginning. I'd like to shift this second row text to show the matched word and what comes next.

I'd like it to work like Google. When it present you the results, it shows the page title, and a second text field with the relevant text where your words were found.

For instance, if I have a DB like this:

item_name  |   item_description
Item1      |   This is my description for stackoverflow.
Item2      |   This is my second description for stackoverflow.

and the user types "my" I'd like to show my suggestions like this:

Item1
...my description for stacko
Item2
...my second description for

I'm using SQLite DB with a Content Provider and SearchWidget.

How can it be done?


Solution

  • When using FTS, you can use the snippet function:

    The snippet function is used to create formatted fragments of document text for display as part of a full-text query results report.

    > SELECT snippet(items, '', '', '...', -1, 3)
      FROM items
      WHERE items MATCH 'my';
    ...is my description...
    ...is my second...