androidsqlitematchfts3

Multiple Column Match in FTS3


I have a simple database of three columns: title, data, info. I'm storing them in a SQLite FTS3 database so I can easily perform full-text searches. I've been basing my app off the Searchable Dictionary example from Android. The one thing I can't figure out how to do is how to search (match) across two columns. I basically want to take the query and search it in both the title and data columns. The following is the method that prepares the query:

public Cursor searchMatches(String query, String[] columns) {
    String selection = KEY_TITLE + " MATCH ?";
    String[] selectionArguements = new String[] {query + "*"};
    return query(selection, selectionArguements, columns);
}

If I do not follow that exact format KEY_TITLE + " MATCH ?" for the selection, nothing else will work despite the fact that the FTS3 document says other ways can work. How can set up this selection in Android to search across two columns? I've tried adding OR but I simply get a SQL logic error. Thanks a lot!


Solution

  • Alright, I found a workable solution although it isn't the prettiest. It involves using UNION to simply combine two MATCH queries.

        String wordQuery = builder.buildQuery(null, KEY_WORD + " MATCH '" + query + "*'", null, null, null, null, null);
        String definitionQuery = builder.buildQuery(null, KEY_DEFINITION + " MATCH '" + query + "*'", null, null, null, null, null);
        String unionQuery = builder.buildUnionQuery(new String[] {wordQuery, definitionQuery}, null, null);
        Cursor result = helper.getReadableDatabase().rawQuery(unionQuery, null);