-2

I want to search data from firebase. whenever a user type anything in the searchview it will search the related data. For example if user input the name of company it should show data related to that company, OR if user input price it should show data related to that specific price. My Recyclerview is working fine. I'm getting data but it only search data according to company name.

Here is My Code

private void getData(String query) {

    Query firebaseSearchQuery = myRef.orderByChild("companyName").startAt(query).endAt("\uf8ff");

    FirebaseRecyclerOptions<Package> options = new FirebaseRecyclerOptions
            .Builder<Package>().setQuery(firebaseSearchQuery, Package.class).build();

    final FirebaseRecyclerAdapter<Package, PackagesViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<Package, PackagesViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull PackagesViewHolder packagesViewHolder, int i, @NonNull Package aPackage){
                    packagesViewHolder.setDetails(getApplicationContext(), packagesViewHolder, i, aPackage.getPackageName()/*, aPackage.getPackageType()*/, aPackage.getCompanyName(), aPackage.getData()
                            , aPackage.getOnNetMinutes(), aPackage.getOffNetMinutes(), aPackage.getSms()
                            , aPackage.getPrice(), aPackage.getValidity()
                            , aPackage.getSubscriptionCode(), aPackage.getUnsubCode()
                            , aPackage.getRemainingDataCode(), aPackage.getInfo()
                            /*, aPackage.getTermsAndConditions()*/);

                }

                @NonNull
                @Override
                public PackagesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_layout, parent, false);
                    PackagesViewHolder packagesViewHolder = new PackagesViewHolder(view, SearchActivity.this);
                    return packagesViewHolder;
                }
            };
    recyclerView.setAdapter(firebaseRecyclerAdapter);
    firebaseRecyclerAdapter.startListening();
}

**Here is the structure of my firebase database**

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

2 Answers2

1

There is no built-in operator on Firebase to search across all properties of a node. If your app requires such functionality, you might want to consider using a different/additional database for that, such as using Algolia for the searches and Firebase for the realtime sync.

Alternatively, you can build your own search structure inside of Firebase, as I've shown in my answer here: How to query based on multiple conditions in Firebase?

Also see:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
0

You need to add an extra attribute to the database for example:

price-name : "18 comp" 

And then do :

Query firebaseQuery = myRef.orderByChild("price-name").startAt(query).endAt("\uf8ff");

There is no or clause in firebase Realtime Database. The above is an alternative to be able to achieve that.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134