Is there anyone give me any hint or guide me what algorithm should i use for search query function in large database (music or songs almost 1000x10) what i have tried so far is Linear search(time O(n) using iteration for filtering query) which consumes too much memory and also it's too slow to search on given query here's part of the codes:-
on Activity - >
SearchViewModel searchViewModel = new ViewModelProvider(this).get(SearchViewModel.class);
songAdapter = new SongAdapter(getApplicationContext(),new ArrayList<>());
layoutBinding.searchRecyclerviewForSongs.setAdapter(songAdapter);
layoutBinding.searchEdittextBox.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
// task.searchFilterData(editable.toString());
searchViewModel.getResultList(SearchHome.this, editable.toString())
.observe(SearchHome.this,
new Observer<List<SongsPOJO>>() {
@Override
public void onChanged(List<SongsPOJO> songsPOJOS) {
Log.i(TAG, "onChanged: searchViewModel->" + songsPOJOS.size());
// Update UI with the new data
songAdapter.clearSongData(songsPOJOS);
}
});
}
});
i am using editText as query searchable box with textWatcher -> afterTextChanged(Editable editable) here i am using viewmodel and passing search query to background and returning result on ui
ViewModel Code->
public MutableLiveData<List<SongsPOJO>> getResultList(Context context,String _searchTxt) {
myapp = (App) context.getApplicationContext();
service = myapp.getExecutorService();
service.submit(new Runnable() {
@Override
public void run() {
List<SongsPOJO> filteredList = filteredData(context, _searchTxt);
setList.postValue(filteredList);
}
});
return setList;
}
private List<SongsPOJO> filteredData(Context context,String _query){
// Filter logic here:-
List<SongsPOJO> filterList = new ArrayList<>();
// Projection for the columns you want to retrieve
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
// Add other columns you need
};
// Selection criteria for the query
String selection = MediaStore.Audio.Media.TITLE + " LIKE ?";
String[] selectionArgs = new String[]{"%" + _query + "%"};
// Sorting order for the results
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
// Perform the query
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
sortOrder
);
// Check if the cursor is not null
if (cursor != null) {
try {
// Use the cursor to retrieve data
while (cursor.moveToNext()) {
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
// Use a single list for both data loading and filtering
SongsPOJO songsPOJO = new SongsPOJO();
songsPOJO.setSongName(title);
filterList.add(songsPOJO);
}
} finally {
// Close the cursor when done
cursor.close();
}
}
return filterList;
}
please tell me also if i am approaching this in wrong way.
What i want to achieve, is that a search function like other offline music app has? where when a person types any song name then related keyword songs appear on recylerview or UI with no time i means too fast or at least guide me in the right direction to solve the above problems? Thanks in advance!
for fast search problems,
Binary Searchcomes up with time complexity of Log(N). but remember it requires a sorted data to be applied on.which drives you to choose a sorting algorithm. choosing a sorting algorithm depends on some factors such as:
think taking a look on Binary Search will help.