Home > database >  Firebase Database : How I can search anywhere without specifying the key
Firebase Database : How I can search anywhere without specifying the key

Time:06-19

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.

CodePudding user response:

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.

CodePudding user response:

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
            }
        });
  • Related