-1

I am trying to implement sqlite like query in firebase database. For e.g. If there are 10 entries abc1, abc2, abc3, ..... abc10. Query with abc+"%" should display all 10 entries. I tried orderByChild("").equalTo("") as well as startAt(""), but did not get the desired result.

Tried this link, but still stuck. Kindly suggest.

KENdi
  • 7,576
  • 2
  • 16
  • 31
  • Are you making a database in table? – Infinite Loops Jul 06 '17 at 08:00
  • Duplicate of :https://stackoverflow.com/a/34537728/5110536 – Usman Rana Jul 06 '17 at 08:08
  • I tried that link too. But isn't working. I implemented. { "rules": { ".read": "true", ".write": "true", "customer":{ ".indexOn": ["key"] } } } customer1, customer2, .....customer10. query should return all customer with "customer+%" – user2385301 Jul 06 '17 at 08:14
  • As said in the linked answer and the answer linked from that, there is no equivalent to SQL's `LIKE` operator in Firebase. If you've tried something you think should work, update your question with enough information to troubleshoot. I highly recommend adding the JSON (as text, no screenshots), which you can get by clicking the "Export JSON" link in your [Firebase Database console](https://console.firebase.google.com/project/_/database/data) and a [minimal complete snippet of code that reproduces the problem](http://stackoverflow.com/help/mcve). – Frank van Puffelen Jul 06 '17 at 14:14

2 Answers2

1

startAt("abc1") with endAt("abc9") will work for the first nine.

However, string sorting wise, abc10 will throw off the sorting abc1, abc10, abc2

So... If you know the cap on how many abc's there are, you can pad with zero's to get the desired output. Here we know we will cap at 999.

abc_001
abc_002
.
.
.
abc_999

Then startAt and endAt will work correctly.

Alternately, you can store the integer values in another child node and order by those numerically.

Jay
  • 34,438
  • 18
  • 52
  • 81
1

Well after reading many questions i was disappointed to find out that firebase doesn't implement sqlite “like” query, so i decided to work around it.

I know this question is old, but in case anyone still struggling with finding an answer to this problem, I have found an alternative solution, a work around to be more precise.

when the application start, store the list in memory and keep it as a raw data to query from it everytime you need it, the list will still be synchronized with firebase..here is how i did it.

mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Loading data from firebase to the temporary listItem data
            ArrayList<ListItem> data = new ArrayList<>();
            for(DataSnapshot customList :  dataSnapshot.getChildren()){
                ListItem item = customList.getValue(ListItem.class);
                data.add(item);
            }
            listData.clear();
            listData.addAll(data);
            //listData above is my list that i use and re-use (clear and refull, rawListData is like my offline firebase database)
            rawListData = new ArrayList<>();
            rawListData.addAll(data);
            if(firstRun){
                firstRun=false;
                myAdapter = new MyAdapter(MainActivity.this, listData);
                recView.setAdapter(myAdapter);
                if(!isBind){
                    runService();
                }
            }else{
                myAdapter.notifyDataSetChanged();
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(MainActivity.this, "Database loading error", Toast.LENGTH_SHORT).show();
        }
    });

let's say I want to search in my data about a title that contains the word "blues" in it..i just call this thread > filterMusicType("blues");

    public void filterMusicType(String type){

    //Notify RecyclerView that listData was cleared.
    int currentSize = listData.size();
    listData.clear();
    myAdapter.notifyItemRangeRemoved(0, currentSize);

    //Filter data based on music type
    for(ListItem item : rawListData){
        String station = item.getTitle().toLowerCase();
        if(station.contains(type)){
            listData.add(item);
        }
    }

    //Notify RecyclerView that listData was laoded with new Data.
    myAdapter.notifyItemRangeInserted(0, listData.size());
    myAdapter.notifyDataSetChanged();
}
Daiwik Dan
  • 195
  • 10