0

I am currently using the Sample from the SearchableDictionary which uses FTS, however when I enter the word "apple" it displays all apple words instead of only the word that starts with apple. I would like to know if there are anyway to display a list eg) 1) Apple 2) Apple pie

instead of 1) Adam's apple 2) Apple 3) Apple pie 4) Big apple

using android FTS.

According to what I understand from FTS is that it uses MATCH instead of LIKE and it functions like the LIKE %something% of the normal SQLite Query

I would appreciate it very much if someone can tell me how should I do it.

Thank you.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
AuroraBlaze
  • 421
  • 2
  • 8
  • 25
  • Both "Adam's apple" and "Big apple" have "apple" in them, and therefore would match `%LIKE%`. – CommonsWare Jan 11 '14 at 16:24
  • is there anyway to change it so that it will only display words that start with Apple?. – AuroraBlaze Jan 11 '14 at 16:35
  • or a method to make it similar to LIKE something% ? ty – AuroraBlaze Jan 11 '14 at 16:42
  • "Big apple" consists of two words. What do you mean with "word"? The entire contents of a column? – CL. Jan 11 '14 at 23:45
  • Yes Big apple consist of 2 words but I would like to make it in such a way that it will only display the word that starts with "apple".eg) like the Oxford dictionary, when I enter the word "apple", it will display all words that start with apple - 1) Apple and 2) Apple pie instead of displaying Adam's apple and Big apple.Also, I noticed that when I enter a char "B", it will display all words that starts with "B" in the oxford dictionary, but in this searchable dicationary sample, it will display "Asomething followed by Bsomething (eg.A-bomb) instead of displaying the words that start with B. – AuroraBlaze Jan 12 '14 at 05:03

2 Answers2

1

This is what I did, inside the DictionaryDatabase.java file of the SearchableDictionary. I modified some of the method codes into these:

     public Cursor getWordMatches(String query, String[] columns)
     {
        String selection = KEY_WORD + " >='" + query +"'"; 
        return query(selection, columns);
     }


     private Cursor query(String selection, String[] columns)
     {

            SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
            builder.setTables(FTS_VIRTUAL_TABLE);
            builder.setProjectionMap(mColumnMap);

            Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
                   columns,selection,null, null,  null, KEY_WORD );

            if (cursor == null) {
                return null;
            } else if (!cursor.moveToFirst()) {
                cursor.close();
                return null;
            }
            return cursor;
        }
AuroraBlaze
  • 421
  • 2
  • 8
  • 25
0

Try this Query in your code:

SELECT * FROM TABLE_NAME WHERE Column_name MATCH 'apple*';

Check this link: http://www.sqlite.org/fts3.html#section_3

The FTS engine provides that information via the offsets virtual function. Try this

SELECT offsets(docs) FROM docs WHERE docs MATCH 'apple*';

Enter this in your code:

 Cursor cursor = database.rawQuery("SELECT column_name, offsets(Tablename) FROM entries WHERE column_name MATCH '"apple *"'",null);

Then use cursor.

Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
  • I had tried this method before and it give me Adam's apple, Big apple, apple pie and apple instead of only Apple and apple pie. – AuroraBlaze Jan 12 '14 at 12:03
  • Chk this link same question asked by you .http://stackoverflow.com/questions/4432314/using-sqlite-with-fts-on-android.. I am still trying to find out .. till that time check this link please post the answer if it works – Zohra Khan Jan 12 '14 at 15:39
  • 1
    http://stackoverflow.com/questions/2734828/sqlite-fts3-simulate-like-somestring .. Check this link too – Zohra Khan Jan 12 '14 at 17:48
  • thank you very much for all the links and guidance, but may I know which code of line should I modify so that it will become "SELECT offsets(docs) FROM docs WHERE docs MATCH 'apple*';" ? I am not very sure which one i should change. Do I have to change this "Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(), columns, selection, selectionArgs, null, null, KEY_WORD );" to this Cursor cursor = mDatabaseOpenHelper.getReadableDatabase().rawQuery("select * from FTS_VIRTUAL_TABLE MATCH 'apple*' " , null);" ? – AuroraBlaze Jan 13 '14 at 06:40
  • I had also tried changing this String[] col = new String[] { BaseColumns._ID, DictionaryDatabase.KEY_WORD, DictionaryDatabase.KEY_DEFINITION, SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID}; to this , String[] col = new String[] { BaseColumns._ID, "offset("+DictionaryDatabase.KEY_WORD+")", DictionaryDatabase.KEY_DEFINITION, SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID}; but it seems like the application lost it function when i change it, May I know if I changed the wrong thing? ty – AuroraBlaze Jan 13 '14 at 07:32
  • and I had also tried the last link that u gave me . I had tried replacing this Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(), col,selection, selectionArgs, null, null, KEY_WORD ); with this for testing Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(), col,"suggest_text_1 >= ? AND suggest_text_1 <'apple z' ", new String[]{"apple"}, null, null, KEY_WORD ); and it still doesnt seem to work T.T. or is it that I wrote the codes wrongly ? – AuroraBlaze Jan 13 '14 at 08:09
  • hi I tried the link that u gave me again just now and this one works for me String selection = KEY_WORD + " >='" + convertedQuery +"'"; =). Thank you very much Zohra Khan, u are my life saviour . *hugs* XD. I appreciate it very much for your help . Thank you !!! . – AuroraBlaze Jan 13 '14 at 11:27
  • I had already posted the exact code below, hope that it can help others too . and ty again =). – AuroraBlaze Jan 13 '14 at 14:38