Android SearchRecentSuggestionsProvider query limit
Asked Answered
I

2

5

I'm working with search suggestion framework and here is the case. I get some query string and make query from content resolver, but problem is that I can't limit the results. Found several solutions but they dont work for me.

My content provider is extending SearchRecentSuggestionsProvider and declared in manifest, here query uri Uri URI = Uri.parse("content://" + AUTHORITY + "/search_suggest_query");

sol 1: adding query parameter to uri

SearchSuggestionProvider.URI.buildUpon().appendQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT, String.valueOf(5)).build()

sol 2: adding limit in sortOrder query parameter

getContentResolver().query(URI, null, "?", new String[]{searchQuery}, "_id asc limit 5");

In both cases query returns all rows from search suggestions. Does anyone know how to limit such a query?

Idalla answered 24/6, 2015 at 7:40 Comment(2)
SUGGEST_PARAMETER_LIMIT Query parameter added to suggestion queries to limit the number of suggestions returned. This limit is only advisory and suggestion providers may chose to ignore it.Kermie
There is a better solution, a completely different way, I added the answer belowKazoo
K
4

Below is the query method in the base class SearchRecentSuggestionsProvider in the framework

/**
     * This method is provided for use by the ContentResolver.  Do not override, or directly
     * call from your own code.
     */
    // TODO: Confirm no injection attacks here, or rewrite.
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 
            String sortOrder) {

Potentially you can override it and implement the limiting - but as you can see the comment above it clear states "do not override". In the existing method no limiting is enforced.

However I see in some of the other answers on stack-overflow and other sites that people do override it.

Example : Use SearchRecentSuggestionsProvider with some predefined terms?

Based on this I think you can try it out - override the query method and add your own implementation which supports limiting the results. Parse the SearchManager.SUGGEST_PARAMETER_LIMIT that you have used in the query method and use it to limit the results returned.

Kob answered 21/1, 2016 at 9:52 Comment(3)
"use this limit when actually querying the database or whatever your data source is" This question is related to SearchRecentSuggestionsProvider specifically. Please be more specific on how to override query for this case.Eure
Sorry I misunderstood the question initially. Added more specific details now.Kob
There is a better solution on a completely different way, I added the answer belowKazoo
K
5

Actually, we were searching in the wrong place. After all this investigations, finally I found the trick.

On the results screen (Where we save the query to the search SearchRecentSuggestions) we call this method

SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                    SearchSuggestionProvider.AUTHORITY, SearchSuggestionProvider.MODE);
suggestions.saveRecentQuery(query, null);

SearchRecentSuggestions was the responsible to save this query to the content provider.

This class has a protected method called truncateHistory() with this documentation

Reduces the length of the history table, to prevent it from growing too large.

So the solution is simply, create a custom class, override this method, and call the super implementation with the required limit.

Here is the example

public class SearchRecentSuggestionsLimited extends SearchRecentSuggestions {

    private int limit;

    public SearchRecentSuggestionsLimited(Context context, String authority, int mode, int limit) {
        super(context, authority, mode);
        this.limit = limit;
    }

    @Override
    protected void truncateHistory(ContentResolver cr, int maxEntries) {
        super.truncateHistory(cr, limit);
    }
}
Kazoo answered 23/8, 2016 at 6:36 Comment(2)
if I understood right, that will limit whole table size, but what I was asking is how to limit query rows amount? or your solution won't affect table size?Idalla
Yes, it affects the table size, but still save the top 5 recent. By reducing the table size to 5 will affect the query rows. But if you want to have an option like show more history query, this will not help you.Kazoo
K
4

Below is the query method in the base class SearchRecentSuggestionsProvider in the framework

/**
     * This method is provided for use by the ContentResolver.  Do not override, or directly
     * call from your own code.
     */
    // TODO: Confirm no injection attacks here, or rewrite.
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 
            String sortOrder) {

Potentially you can override it and implement the limiting - but as you can see the comment above it clear states "do not override". In the existing method no limiting is enforced.

However I see in some of the other answers on stack-overflow and other sites that people do override it.

Example : Use SearchRecentSuggestionsProvider with some predefined terms?

Based on this I think you can try it out - override the query method and add your own implementation which supports limiting the results. Parse the SearchManager.SUGGEST_PARAMETER_LIMIT that you have used in the query method and use it to limit the results returned.

Kob answered 21/1, 2016 at 9:52 Comment(3)
"use this limit when actually querying the database or whatever your data source is" This question is related to SearchRecentSuggestionsProvider specifically. Please be more specific on how to override query for this case.Eure
Sorry I misunderstood the question initially. Added more specific details now.Kob
There is a better solution on a completely different way, I added the answer belowKazoo

© 2022 - 2024 — McMap. All rights reserved.