0

I have a django project. I implimented Firebase into the project. Firebase created and returns objects in JSON format. Now, I want to have a search system through existing users and return an object that has all of the stored username values from the list and then display all of the results.

Here is a sample of a Firebase JSON object format:

OrderedDict([  
   ('info',
   {  
      'bio':'jogn from the office',
      'company':'MySplit',
      'dob':'1993-03-23',
      'first_name':'jim',
      'gender':'M',
      'last_name':'helpert',
      'phone':'+19515394856'
   }   ),
   ('location',
   {  
      'city':'Mis Viejo',
      'state':'CA',
      'street':'27806 cheller',
      'zip_code':98892
   }   ),
   ('status',
   {  
      'active':'1',
      'group':'dev',
      'premium':'1'
   }   ),
   ('username',
   'jimhelpert'   )
])

I want to search and return an object with all the usernames that contain jim

I cant find how to make the query that would return all objects with username that contains the phrase...

here is the view.py:

        user_id = request.session['uid']
        user_profile = database.child('users').child(user_id).child('profile').get()
        print(user_profile.key())
        print(user_profile.val())
        print(user_profile)
        print(type(user_profile))
        parameters = {
            'user_profile':user_profile.val(),
        }
        return render(request, 'users/user_home.html', parameters)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Omar Jandali
  • 814
  • 3
  • 20
  • 52
  • What have you tried and what isn't working for you? – André Kool Jun 12 '18 at 07:05
  • I have been looking online to a sample or guide on how to do it and I cant even find a sample. I have no idea what it would look like. I just need a sample of what the query would look like... – Omar Jandali Jun 12 '18 at 07:29
  • You should take a look at [this answer](https://stackoverflow.com/a/28612040/4916627). It's for IOS but the same applies for other languages, there isn't a contains query in firebase realtime database. – André Kool Jun 12 '18 at 07:34
  • I read the answer you linked. So there is no contains filter on JSON objects. Is there any way that I can scan through objects in a JSON query and check of a specific key value pair contains a string... Not neccesarily and string contain... – Omar Jandali Jun 12 '18 at 07:41
  • is it possible to create any type of system that takes a JSON object with several user objects and search through them, like a search system for a social app. within the json – Omar Jandali Jun 12 '18 at 07:43

1 Answers1

0

Here is what the Firebase documentation has to say regarding search:

Cloud Firestore doesn't support native indexing or search for text fields in documents.

And it goes on to say:

Additionally, downloading an entire collection to search for fields client-side isn't practical.

You could get away with this approach if your dataset is really, really small and you know for sure that it will never grow.

However, I second the suggestion from the documentation to use a dedicated search service.

To enable full text search of your Cloud Firestore data, use a third-party search service like Algolia.

Alternatively, you might consider using an SQL database which allows this kind of querying.

Daniel Hepper
  • 28,981
  • 10
  • 72
  • 75
  • I think this is the only part of the Firebase integration that is giving me trouble. should I just not use Firebase at all or should i just use a postgresql database for this specific issue I am having. i am not sure what to do at this point – Omar Jandali Jun 12 '18 at 08:40
  • It really depends on what you are more comfortable with, Firebase + Search Service or PostgreSQL. On aspect worth considering is that Django is really tailored towards SQL databases and you will probably miss out on a lot of Django's features by using a NoSQL database like Firebase. – Daniel Hepper Jun 12 '18 at 08:45