按匹配数对搜索结果(来自Android SearchView查询)进行排序

时间:2022-10-13 13:47:59

i'm trying to find a good way to sort the search results according to relevance after performing a search with a SearchView in Android. For me relevance means the number of matches in two SQLite text columns

我正试图找到一种在Android中使用SearchView进行搜索后根据相关性对搜索结果进行排序的好方法。对我而言,相关性是指两个SQLite文本列中的匹配数

I'm using a CursorLoader and there the sort order can be given to the constructor at the end

我正在使用CursorLoader,最后可以给构造函数赋予排序顺序

CursorLoader tLoader = new CursorLoader(
    getActivity(), ContentProviderM.ARTICLE_CONTENT_URI,
    tProj, tSel, tSelArgs, SORT_ORDER);

(or set using the setSortOrder (String sortOrder) method)

(或使用setSortOrder(String sortOrder)方法设置)

But i need more flexibility than this because i'm looking to sort on the number of matches rather than just on one or two columns

但我需要比这更灵活,因为我希望对匹配的数量进行排序,而不是仅仅在一列或两列上

The only solution i can see myself is to add another column in my SQLite table, do some processing, and supply that column as the sort column to the CursorLoader

我能看到的唯一解决方案是在我的SQLite表中添加另一个列,进行一些处理,并将该列作为排序列提供给CursorLoader

Now for my question: What is the best way to supply the sort order information to the CursorLoader using SQLite syntax, avoiding having to add a new column? (And what could this SQLite code look like?) Also, i'd like to ask more in general: Is there a different solution to this problem that i've missed?

现在我的问题是:使用SQLite语法向CursorLoader提供排序顺序信息的最佳方法是什么,避免添加新列? (这个SQLite代码看起来怎么样?)另外,我想问更多一般问题:我错过了这个问题的不同解决方案吗?

Grateful for any help! And with kind regards, Tord

感谢任何帮助!亲切的问候,Tord

2 个解决方案

#1


0  

Depending on the content provider, if it just pass to the orderBy field, you can do anything.

根据内容提供商的不同,如果只是传递给orderBy字段,您可以执行任何操作。

SQLiteDatabase query

orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

orderBy如何对行进行排序,格式为SQL ORDER BY子句(不包括ORDER BY本身)。传递null将使用默认排序顺序,该顺序可能是无序的。

you can do whatever you want, this is just the line after ORDER BY

你可以做任何你想做的事,这只是ORDER BY之后的一行

P.S. It is totally depending on the Content Provider, it it choose to ignore the parameter, you can do nothing.

附:它完全取决于Content Provider,它选择忽略参数,你什么都不做。

#2


0  

i found a "workaround" for this problem.

我发现了这个问题的“解决方法”。

After investigating different ways to write sqlite code i ended up just adding a new table column just for sorting. This column simply stores an integer and is updated every time that the user performs a search, right before the CursorLoader is created

在研究了编写sqlite代码的不同方法后,我最后只是为了排序而添加了一个新的表列。此列只存储一个整数,并在每次用户执行搜索时更新,就在创建CursorLoader之前

Advantages:

  • We can now do all of the relevance calculations in Java code
  • 我们现在可以在Java代码中完成所有相关性计算

Drawbacks:

  • Relevance calculation is done as the search is done so if we have a large number of items it may take some time to process everything
  • 相关性计算在搜索完成时完成,因此如果我们有大量项目,则可能需要一些时间来处理所有内容

#1


0  

Depending on the content provider, if it just pass to the orderBy field, you can do anything.

根据内容提供商的不同,如果只是传递给orderBy字段,您可以执行任何操作。

SQLiteDatabase query

orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

orderBy如何对行进行排序,格式为SQL ORDER BY子句(不包括ORDER BY本身)。传递null将使用默认排序顺序,该顺序可能是无序的。

you can do whatever you want, this is just the line after ORDER BY

你可以做任何你想做的事,这只是ORDER BY之后的一行

P.S. It is totally depending on the Content Provider, it it choose to ignore the parameter, you can do nothing.

附:它完全取决于Content Provider,它选择忽略参数,你什么都不做。

#2


0  

i found a "workaround" for this problem.

我发现了这个问题的“解决方法”。

After investigating different ways to write sqlite code i ended up just adding a new table column just for sorting. This column simply stores an integer and is updated every time that the user performs a search, right before the CursorLoader is created

在研究了编写sqlite代码的不同方法后,我最后只是为了排序而添加了一个新的表列。此列只存储一个整数,并在每次用户执行搜索时更新,就在创建CursorLoader之前

Advantages:

  • We can now do all of the relevance calculations in Java code
  • 我们现在可以在Java代码中完成所有相关性计算

Drawbacks:

  • Relevance calculation is done as the search is done so if we have a large number of items it may take some time to process everything
  • 相关性计算在搜索完成时完成,因此如果我们有大量项目,则可能需要一些时间来处理所有内容