0

I'm trying to turn this code into a multiple search, ie not just a Users city, but include state and country.

I have the 3 search input windows in the xml, the getters and setters ready, but I haven't seen any code to accomplish want I'd like to do: it will only find a user by city.

private fun firebaseUserSearch(searchText: String) {

    Toast.makeText(this@MainActivity, "Started Search", Toast.LENGTH_LONG).show()

    val firebaseSearchQuery = mUserDatabase!!.orderByChild("name").startAt(searchText).endAt(searchText + "\uf8ff")


    val options = FirebaseRecyclerOptions.Builder<Users>()
            .setQuery(
                    firebaseSearchQuery, Users::class.java!!)
            .build()

    

So with this code I can only search one child city and I get the user that lives in that city. I'd like to search city, state, country. Thank you in advance for your help.

Here's an image of the UI that will explain what I'm trying to do

Community
  • 1
  • 1
JSL
  • 3
  • 4
  • You can not add more than on e order by child in single query you can sort it after data receive by if else conditions – Hello world May 16 '19 at 07:10
  • So you basically want to search on three properties, right? Please responde with @AlexMamo – Alex Mamo May 16 '19 at 08:35
  • @AlexMamo Yes Alex. I just posted an image of the UI in the question(link) to answer your question. Yes, I want to search three properties. Thank you for asking. – JSL May 16 '19 at 17:10
  • @JSL Have you solved the issue regarding the search of three properties? – Alex Mamo May 20 '19 at 08:10
  • @AlexMamo thank you so much for asking. No I haven't solved it. I've been scouring the internet. I haven't seen a forum answer or video that explains it anywhere. The code that "Hello world" replied gave me a red-error 'log'. and as far as "sorting" it out later, there's nothing (forum or video) on that (if - else sorting) either. Very frustrating. I would appreciate the help. Thanks again. – JSL May 20 '19 at 20:08

2 Answers2

1

You can not add more than on e order by child in single query you can sort it after data receive by if else conditions

Multiple orderBy are not allowed in Firebase

Try this

userDatabase.orderByChild("townCity").startAt(searchText).addValueEventListener( new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
 log.i("data", dataSnapshot.toString());
}

@Override
public void onCancelled(DatabaseError databaseError){

}
});
Hello world
  • 80
  • 1
  • 10
0

Unfortunately, you cannot filter the results based on three propertis with the Firebase realtime database without making some changes in the structure of your database. The realtime database doesn't have the capability to perform filtering on multiple properties (using "multiple WHERE clauses" as many people can say in SQL terms). However, there is a workaround that can help you filter results on two or more properties using a composite property, as explained in my answer from the following post:

So in your case, the combination of the properties that you are looking for, is a string value that has the city/town, state/pro and country in the composition.

Anyhow, in Cloud Firestore chaining multiple where() calls is permitted. So if you consider at some point to try using Cloud Firestore, beside this feature, you'll also find many more.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • On how to use a third-party search service like Algolia, please see my answer from the **[post](https://stackoverflow.com/questions/49596610/is-it-possible-to-use-algolia-query-in-firestorerecycleroptions/49607796)**. – Alex Mamo May 24 '19 at 20:56
  • I've clicked it. When I do it says>>"Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." Thank you for your time and info. I realize what I need is a more in-depth search function, so I'm checking out tutorials on Elastic search/ SQL. I haven't seen a way around it for what I'm trying to accomplish. Much appreciated. – JSL May 29 '19 at 17:24