Search code examples
androidsqliteandroid-sqliteandroid-room

Android Room list of keyword search


I need to search a list of keyword in Room. Currently i can search a single keyword like:

@Query("SELECT * FROM customer where content LIKE '%' || :keyword || '%'  and status = 'published'")
LiveData<List<Customer>> getSearchResult(String keyword);

But how can i search a list of keyword in a easy way.

Is there easy way like IN with Collection arguments like:

@Query("SELECT first_name, last_name FROM user WHERE region IN (:regions)")
public List<NameTuple> loadUsersFromRegions(List<String> regions);

I know i can build query for multiple keyword manually but i want to pass a Collection as a argument.

Thanks in advance.


Solution

  • I have used below code for FTS search. Joined keyword with OR

        String keywordAll = TextUtils.join("* OR *", keywordList);
        keywordAll = "*" + keywordAll + "*";
    

    For query used MATCH for search

    WHERE Posts MATCH :keyword
    

    Hope this will help others.