0

I am trying to make a search result to show a list of documents from firestore collection based on the keyword found in a field from the document.

enter image description here

I saw that the firestore how-to-guides show something like this:

citiesRef.whereGreaterThanOrEqualTo("name", "San Francisco");

I tried but the result that came out isn't correct at all.

This is the class that show query results based on the keyword retrieved from the last activity using intentextra.

I am not sure how to fix it, if anyone who's familiar with firestore could help me, I would be really grateful. Thanks.

public class SearchActivity extends AppCompatActivity {

    private TextView showKeyword;
    private RecyclerView recyclerView;

    private JournalAdapter adapter;
    private List<Journal> journalList;

    private FirebaseFirestore db;
    private FirebaseAuth mAuth;
    private String userId;
    private CollectionReference userIdRef;

    private String keywordString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        showKeyword = (TextView) findViewById(R.id.keywordTextView);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerViewSearch);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        journalList = new ArrayList<>();
        adapter = new JournalAdapter(this, journalList);
        recyclerView.setAdapter(adapter);

        //firestore
        db = FirebaseFirestore.getInstance();
        mAuth = FirebaseAuth.getInstance();

        userId = mAuth.getCurrentUser().getUid();
        userIdRef = db.collection("main").document(userId).collection("journal");

        showData();
    }

    private void showData() {

        Intent intent = getIntent();
        keywordString = intent.getStringExtra("keyword");

        showKeyword.setText(keywordString);

        //problem, funny result
        userIdRef.whereGreaterThanOrEqualTo("description", keywordString)
                .orderBy("description")
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                        assert queryDocumentSnapshots != null;

                        if (!queryDocumentSnapshots.isEmpty()){
                            //if not empty
                            List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();

                            for (DocumentSnapshot documentSnapshot : list) {

                                Journal journal = documentSnapshot.toObject(Journal.class);
                                assert journal != null;
                                journal.setId(documentSnapshot.getId());
                                journalList.add(journal);
                            }
                            adapter.notifyDataSetChanged();

                        } else {
                            //if empty
                            Toast.makeText(SearchActivity.this, "No result with the word " + keywordString + " is found", Toast.LENGTH_SHORT).show();
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Toast.makeText(SearchActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

enter image description here

enter image description here

Lionlollipop
  • 113
  • 3
  • 12
  • You say *I tried but the result that came out isn't correct at all.* What is the result then? – Alex Mamo Mar 29 '20 at 15:29
  • Add the screenshot of the database. What does field 'description' contains ? – Raj Mar 29 '20 at 18:42
  • @AlexMamo The result was a list of firestore documents that doesn't contain that "keyword" string. – Lionlollipop Mar 30 '20 at 04:34
  • @Raj It contains a string. I added the database screenshot – Lionlollipop Mar 30 '20 at 04:36
  • Are you trying to filter by a field if it contains a substring? Then it is not possible I think. Check [this thread](https://stackoverflow.com/questions/46568142/google-firestore-query-on-substring-of-a-property-value-text-search) – Emil Gi Mar 30 '20 at 11:12
  • @Lionlollipop I'm not sure I understand. Please explain what are the exact result that you want to get. What is the exact term that you want to search to get those results? – Alex Mamo Mar 30 '20 at 11:23
  • @AlexMamo For example, I have a list of documents, and I want to query and show some of the documents which contain the string "keyword" in the field "description", like a search result, to detect if there are any words found in the targeted field of the documents. – Lionlollipop Mar 31 '20 at 04:36
  • So according to your screenshot, if you search for "younger", that document should be returned as a result, right? So you want to search if a field contains a keyword, correct? – Alex Mamo Mar 31 '20 at 10:24
  • @AlexMamo Yes, to search a keyword in the form of string within a field of a document. I submitted a request to Firebase support about this and they said this feature is not available yet. – Lionlollipop Apr 02 '20 at 04:22

0 Answers0