mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-15 14:51:04 +00:00
Searching for full-text fields
This commit is contained in:
@@ -19,7 +19,7 @@ import org.houxg.leamonax.model.RelationshipOfNoteTag;
|
|||||||
import org.houxg.leamonax.model.Tag;
|
import org.houxg.leamonax.model.Tag;
|
||||||
import org.houxg.leamonax.model.Tag_Table;
|
import org.houxg.leamonax.model.Tag_Table;
|
||||||
|
|
||||||
@Database(name = "leanote_db", version = 4)
|
@Database(name = "leanote_db", version = 5)
|
||||||
public class AppDataBase {
|
public class AppDataBase {
|
||||||
|
|
||||||
@Migration(version = 2, database = AppDataBase.class)
|
@Migration(version = 2, database = AppDataBase.class)
|
||||||
@@ -157,4 +157,12 @@ public class AppDataBase {
|
|||||||
cursor.close();
|
cursor.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Migration(version = 5, database = AppDataBase.class)
|
||||||
|
public static class UpdateNoteContentToFTS extends BaseMigration {
|
||||||
|
@Override
|
||||||
|
public void migrate(DatabaseWrapper database) {
|
||||||
|
database.execSQL("CREATE VIRTUAL TABLE fts_note USING fts4 (content='note', content)");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,10 +1,14 @@
|
|||||||
package org.houxg.leamonax.database;
|
package org.houxg.leamonax.database;
|
||||||
|
|
||||||
|
|
||||||
|
import android.database.Cursor;
|
||||||
|
|
||||||
|
import com.raizlabs.android.dbflow.config.FlowManager;
|
||||||
import com.raizlabs.android.dbflow.sql.language.Join;
|
import com.raizlabs.android.dbflow.sql.language.Join;
|
||||||
import com.raizlabs.android.dbflow.sql.language.NameAlias;
|
import com.raizlabs.android.dbflow.sql.language.NameAlias;
|
||||||
import com.raizlabs.android.dbflow.sql.language.SQLite;
|
import com.raizlabs.android.dbflow.sql.language.SQLite;
|
||||||
import com.raizlabs.android.dbflow.sql.language.property.IProperty;
|
import com.raizlabs.android.dbflow.sql.language.property.IProperty;
|
||||||
|
import com.raizlabs.android.dbflow.structure.database.DatabaseWrapper;
|
||||||
|
|
||||||
import org.houxg.leamonax.model.Account;
|
import org.houxg.leamonax.model.Account;
|
||||||
import org.houxg.leamonax.model.Note;
|
import org.houxg.leamonax.model.Note;
|
||||||
@@ -16,8 +20,10 @@ import org.houxg.leamonax.model.Tag;
|
|||||||
import org.houxg.leamonax.model.Tag_Table;
|
import org.houxg.leamonax.model.Tag_Table;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
public class NoteDataStore {
|
public class NoteDataStore {
|
||||||
public static List<Note> searchByTitle(String keyword) {
|
public static List<Note> searchByTitle(String keyword) {
|
||||||
@@ -31,6 +37,31 @@ public class NoteDataStore {
|
|||||||
.queryList();
|
.queryList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static List<Note> searchByFullTextSearch(String keyword) {
|
||||||
|
Set<Long> set = new LinkedHashSet<>();
|
||||||
|
DatabaseWrapper databaseWrapper = FlowManager.getWritableDatabase(AppDataBase.class);
|
||||||
|
String query = "select id from note where userid = ? and istrash = 0 and isdeleted = 0 and id in " +
|
||||||
|
"(select rowid from fts_note where fts_note match ?)";////查询Content中满足条件的记录
|
||||||
|
Cursor cursor = databaseWrapper.rawQuery(query, new String[]{Account.getCurrent().getUserId(), keyword});
|
||||||
|
while(cursor.moveToNext()) {
|
||||||
|
set.add(cursor.getLong(cursor.getColumnIndex("id")));
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
|
||||||
|
query = "select id from note where userid = ? and istrash = 0 and isdeleted = 0 and title like ?";//查询title中满足条件的记录
|
||||||
|
cursor = databaseWrapper.rawQuery(query, new String[]{Account.getCurrent().getUserId(), "%" + keyword + "%"});//查询Content中满足条件的记录
|
||||||
|
while(cursor.moveToNext()) {
|
||||||
|
set.add(cursor.getLong(cursor.getColumnIndex("id")));
|
||||||
|
}
|
||||||
|
cursor.close();
|
||||||
|
|
||||||
|
return SQLite.select()
|
||||||
|
.from(Note.class)
|
||||||
|
.where(Note_Table.id.in(set))
|
||||||
|
.queryList();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static Note getByServerId(String serverId) {
|
public static Note getByServerId(String serverId) {
|
||||||
return SQLite.select()
|
return SQLite.select()
|
||||||
.from(Note.class)
|
.from(Note.class)
|
||||||
|
@@ -163,7 +163,7 @@ public class NoteFragment extends Fragment implements NoteAdapter.NoteAdapterLis
|
|||||||
notes = NoteDataStore.getByTagText(mode.tagText, Account.getCurrent().getUserId());
|
notes = NoteDataStore.getByTagText(mode.tagText, Account.getCurrent().getUserId());
|
||||||
break;
|
break;
|
||||||
case SEARCH:
|
case SEARCH:
|
||||||
notes = NoteDataStore.searchByTitle(mode.keywords);
|
notes = NoteDataStore.searchByFullTextSearch(mode.keywords);
|
||||||
mNoteList.setHighlight(mode.keywords);
|
mNoteList.setHighlight(mode.keywords);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
Reference in New Issue
Block a user