0

I am trying to build a searching function on my app, and the user can search from two different categories: The Title and Author of a book. I am using Firebase Firestore for my app, and I am using Android Studio for my project.

FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference booksRef = db.collection("Books");


booksRef
                        .whereEqualTo("Title", usersearch)
                        .get()
                        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                if (task.isSuccessful()) {
                                    for (QueryDocumentSnapshot document : task.getResult()) {
                                        //Success Code
                                    }

                                } else {
                                   //Failure Code
                                }

                            }
                        });

As you can see, I am only using .whereEqualTo("Title", usersearch), but I want it to also test if the usersearch is equal to any of the Authors in the Database. However, if I put .whereEqualTo("Author", usersearch) Nothing will be found because there is nothing with the same author and title. I have also tried using || but that didn't work either. Thank you in Advance!

Olivia
  • 1
  • 4

1 Answers1

0

I think that the best option for you in this case is to use the wherein operation. You can find at this other question an example of this, but basically should to create an array with the words you want to search, use that variable within the whereIn and you can replace the whereEqualTo for the whereIn as below:

.whereIn("attributes", filters.toList())
                        .get()
                        .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                if (task.isSuccessful()) {
                                    for (QueryDocumentSnapshot document : task.getResult()) {
                                        //Success Code
                                    }

                                } else {
                                   //Failure Code
                                }

                            }
                        });
Samuel Romero
  • 1,233
  • 7
  • 12
  • What kind of reference does the .whereIn need to be on? When I attached it to a collection reference it gave me an error. – Olivia Sep 08 '20 at 13:07
  • What is the error you are getting? Could you please show the code you are using? – Samuel Romero Sep 08 '20 at 19:52
  • It says "Can't resolve symbol where In" when I connect it to booksRef – Olivia Sep 09 '20 at 00:07
  • Since I do not know how you are calling the whereIn method, it is not possible for me to see the error. But please be sure you are using the right symbol name (not misspelled or miss capitalized errors). Another possible options is the method is called with the wrong number or types of arguments. In addition, be sure that you are importing "com.google.cloud.firestore.Query" in your code. – Samuel Romero Sep 09 '20 at 21:29
  • Finally, you can double check the full example here https://github.com/googleapis/java-firestore/blob/107aa05c6b7b8c4f452a51ad61a728e060d9e66e/samples/snippets/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java#L558-L560 – Samuel Romero Sep 09 '20 at 21:29