0

I am trying to search my database and show the user the hashtag they searched for. So for e.g. if they search for #funny then that hashtag will show up in the recycler view.

Right now nothing shows up when I search for the hashtag. Can someone help me ?

private void searchHashTags(String user_word) {

    //Log.d("TAG1", "message is:" + user_word);

    DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("Video_Posts");
    Query query = usersRef.orderByChild("description").equalTo(user_word);

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (search_bar.getText().toString().equals("")) {
                recyclerHashTags.setVisibility(View.INVISIBLE);
            }

            Query query2 = usersRef.orderByChild("description").startAt(search_bar.getText().toString()).endAt(search_bar.getText().toString()+"\uf8ff");

            query2.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {

                    hashTags.clear();
                    for (DataSnapshot userSnapshot : snapshot.getChildren()) {
                        //mUsers.add(userSnapshot.getValue(User.class));

                        Post post = userSnapshot.getValue(Post.class);

                        Log.d("TAG5", user_word);

                        hashTags2.add(post.getDescription());
                        //hashTags2.add(post.getDescription());
                    }
                    adapterExplorer.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

            Log.d("Error name", error.getMessage());
        }
    });
}

database structure

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
thejock
  • 1
  • 1
  • If you're trying to search for a substring **in** the value, see https://stackoverflow.com/questions/10559191/firebase-and-indexing-search. If you're trying to find value **starting with** a substring that *is* possible. Please edit your question to simplify the code though, and reproduce the problem with hard-coded values for the database query that isn't working (so `user_word` or `search_bar`). Also see: [how to create a minimal, complete, verifiable example](http://stackoverflow.com/help/mcve). – Frank van Puffelen Oct 21 '22 at 10:42
  • @FrankvanPuffelen in the node `description` if someone enters `this is `#myfirstpost` i only want to retrieve the hashtag. No matter where it is in the string . Is that possible? – thejock Oct 22 '22 at 20:34
  • The Firebase Realtime Database does not support contains operations or other advanced text search capabilities. See the question I linked in my first comment, or more results from https://stackoverflow.com/search?q=%5Bfirebase-realtime-database%5D+full+text+search – Frank van Puffelen Oct 22 '22 at 20:38
  • Since you're using Java, then I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-filter-firestore-data-cheaper-705f5efec444) will help. – Alex Mamo Oct 26 '22 at 12:51
  • @AlexMamo is there one for firebase ? – thejock Oct 27 '22 at 19:40
  • Are you looking for something like [this](https://medium.com/firebase-tips-tricks/how-to-optimize-firebase-realtime-database-calls-to-improve-performance-cc63dad374d5)? – Alex Mamo Oct 27 '22 at 19:48

0 Answers0