Domanda

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?

È stato utile?

Soluzione

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...

Altri suggerimenti

This query on database will return desired results:

SELECT item_name, '... '||SUBSTR(item_description, p) FROM (
    SELECT *, INSTR(item_description, 'my') AS p FROM items_table WHERE p>0
);

I'm not sure if INSTR and SUBSTR are implemented in Android SQLite engine.

EDIT:

FTS4 will not return match positions, at least I don't think so.

Nonetheless, You may use it to speed up previous query:

SELECT item_name, '... '||SUBSTR(item_description, p) FROM (
    SELECT *, INSTR(item_description, 'my') AS p FROM (
        SELECT * FROM items_table WHERE item_description MATCH 'my'
    )
);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top