2013-01-23 15 views
0

我的問題是我有大約1000多個記錄的Android應用程序索引的Android

string field1; 
string field2; 
string field3; 
string field4; 
//... 

我想在這組記錄進行搜索,並獲得兩個字段(field1field2)最好的結果。

目前我閱讀每條記錄和compare()(字符串比較)與我想要搜索的文本,這需要很長時間。

什麼是執行搜索的最佳方法?

  1. 儲存在的SQLite數據庫的每個記錄,並做「選擇查詢,其中像」
  2. 哈希映射
  3. ?任何其他建議?

或者可以創建記錄索引並進行搜索。

+0

什麼是「最佳」結果?這是完美的搭配嗎? – sschrass

+0

例如給定的字符串:「Fox今天結束等等..」和搜索字符串是「今天」,那麼它應該返回true,因爲字符串存在。 – Nirav

+0

這個信息應該在問題中!你可能想閱讀有關正則表達式,模式和匹配器。 – sschrass

回答

0

如果你想搜索精確匹配,我會盡量讓MyAppRecord的地方

public class MyAppRecord { 
    private String record; 
    private int deviance; 
} 

ArrayList得到每個記錄要找到字符串的偏差:

public static int getLevenshteinDistance (String s, String t) { 
    if (s == null || t == null) { 
     throw new IllegalArgumentException("Strings must not be null"); 
    }  
    int n = s.length(); // length of s 
    int m = t.length(); // length of t 

    if (n == 0) { 
     return m; 
    } else if (m == 0) { 
     return n; 
    } 

    int p[] = new int[n+1]; //'previous' cost array, horizontally 
    int d[] = new int[n+1]; // cost array, horizontally 
    int _d[]; //placeholder to assist in swapping p and d 

    // indexes into strings s and t 
    int i; // iterates through s 
    int j; // iterates through t 

    char t_j; // jth character of t 

    int cost; // cost 

    for (i = 0; i<=n; i++) { 
     p[i] = i; 
    } 

    for (j = 1; j<=m; j++) { 
     t_j = t.charAt(j-1); 
     d[0] = j; 

     for (i=1; i<=n; i++) { 
      cost = s.charAt(i-1)==t_j ? 0 : 1; 
      // minimum of cell to the left+1, to the top+1, diagonally left and up +cost       
      d[i] = Math.min(Math.min(d[i-1]+1, p[i]+1), p[i-1]+cost); 
     } 

     // copy current distance counts to 'previous row' distance counts 
     _d = p; 
     p = d; 
     d = _d; 
    } 

    // our last action in the above loop was to switch d and p, so p now 
    // actually has the most recent cost counts 
    return p[n]; 
    } 
} 

將其保存到您的MyAppRecord -object最後排序您ArrayListMyAppRecord -obje的deviance CTS。

請注意,這可能需要一些時間,具體取決於您的記錄集。並且注意,沒有辦法通過搜索狗來告訴她的狗狗或狗狗B在你的列表中的某個位置上。

閱讀Levensthein距離以瞭解它的工作原理。你可能會想到整理出可能需要長/短的字符串才能獲得可能達到閾值的距離。

也可以將「足夠好」的結果複製到不同的ArrayList

+0

我覺得你已經給了錯誤的函數「getLevenshteinDistance」,因爲我無法關聯它對我的查詢。 – Nirav

+0

你正在隱藏信息。 – sschrass

+0

沒有什麼可隱藏的。無論如何,我發現了一種不同的方式來獲取由特定的SQL查詢過濾的數據。 – Nirav