0

In the following code, 'question' is a string variable created by a users voice input. this question is compared with document IDs in my firebase cloud firestore.

public void setAnswer(String question){
        FirebaseFirestore JLdatabase = FirebaseFirestore.getInstance();

        DocumentReference docRef = JLdatabase.collection("Products").document(question);
        docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot document = task.getResult();
                    if (document.exists()) {
                        textViewAnswer.setText(document.getString("location"));
                    } else {
                        textViewAnswer.setText("I can't help you with this yet");
                    }
                } else {
                    textViewAnswer.setText("Oeps. Something went wrong");
                }
            }
        });


If question is exactly equal to the document ID, it works, but most of the time something slightly different will be said. Now I want to check if there is a word/word combination in the question that's equal to one of my documents ID. Is this possible in Java? I couldn't find something like this on the web.

If there is a better way to solve a problem like this. I would like to here!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

2 Answers2

0

what I would do is :

  1. Create a field name that stores question that follows your document ID
  2. Store the question as an array. (meaning each word is its own array)
  3. Perform a firebase query with the function array-contains
  4. Remember how I told you to store your question as an array (e.g ["How", "are", "you])? Well because firebase do not have the LIKE operation as to SQL
  5. After performing your query, you can get the document ID and pass it into the document reference
Shreamy
  • 341
  • 2
  • 16
0

If a question is exactly equal to the document ID, it works

It works because you are pointing to the document that has that exact id.

Now I want to check if there is a word/word combination in the question that's equal to one of my documents ID.

Unfortunately, there is no way you can create a query to search for a substring in the document id. The only option that you have is to create a property of type String that can hold the question and perform a query using one of my solutions that can be found in my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193