0

I've the following structure :

I would like to implement the search feature, I want to search anywhere without specifying the key (bags, brandName color...), I want to make a query that search for all cars that have a value that matching with the search keyword.

Example :

If I write "Red" in the search bar, I would like to get all cars that have red color, in the same time if I search for 850 I want to get all cars that have 850 as a price.

In brief, I don't want to use orderByChild, because I don't want to search by specific key. I hope my question is clear.

private void getAllCarsBy(String filter) {
        Query query = mDatabase.child("cars").startAt(filter); //not working

        FirebaseRecyclerOptions<Car> options = new FirebaseRecyclerOptions.Builder<Car>()
                .setLifecycleOwner(this)
                .setQuery(query, Car.class)
                .build();
        setAdapter(options);
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                loadingProgressBar.setVisibility(View.GONE);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                ToastUtils.showLong(error.getMessage());
                loadingProgressBar.setVisibility(View.GONE);
            }
        });
    }

Massive thanks for your help in advance.

  • 1
    If you consider at some point in time try using [Cloud Firestore](https://firebase.google.com/docs/firestore/), here you can find a [resource](https://medium.com/firebase-tips-tricks/how-to-filter-firestore-data-cheaper-705f5efec444) that can help you achieve that, – Alex Mamo Jun 19 '22 at 08:30
  • Yes you can, but you cannot use `FirebaseRecylerAdapter`. You need to use the simple one. – Ticherhaz FreePalestine Jun 19 '22 at 14:40
  • You can refer to this link. https://stackoverflow.com/a/68248939/9346054 hope this helps – Ticherhaz FreePalestine Jun 19 '22 at 18:32

2 Answers2

3

There is no way to search across all property names in the Firebase Realtime Database. If you need such search functionality, consider using a dedicated solution for searching - such as Algolia, ElasticSearch or many others.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • There are also two very useful extensions for searching with [Algolia](https://firebase.google.com/products/extensions/algolia-firestore-algolia-search) or [ElasticSearch](https://firebase.google.com/products/extensions/elastic-firestore-elastic-app-search). – Alex Mamo Jun 19 '22 at 08:28
  • 1
    They are quite useful indeed Alex, but unfortunately at the moment only for Firestore while OP is using the Realtime Database. – Frank van Puffelen Jun 19 '22 at 11:56
0

Try This One

mDatabase.orderByChild("cars").startAt(filter).addValueEventListner(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        //TODO
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        //TODO
    }
});
Kaan
  • 5,434
  • 3
  • 19
  • 41
Ryadh Hanafi
  • 231
  • 2
  • 4