I'm migrating an application that was using Mysql to firebase, and I managed to migrate it almost all, just missing the part of search, I did not quite understand how it works. I'm trying to do this query below in firebase.
SELECT *, (6371 * acos( cos(radians(?)) * cos(radians(latitude)) * cos(radians(?) - radians(longitude)) + sin(radians(?)) * sin(radians(latitude)) )) AS distance FROM usuario ORDER BY distance
What I'm trying to do is pass the latitude and longitude of my User to firebase and he returns me all users ordering the nearest to the farthest.
Note: This is not the same query that I use in my application, that I just find on google and do not know if this work, but you can get an idea of what I'm trying to do.
Note ²: English is not my native language :P
The example SQL query you provided uses a capability of SQL that does not exist in Firebase: the ability to calculate an expression using the column values for each row in the table and then use the value of that expression to filter and sort the query result.
I don't see a way to use Firebase to perform the type of query you want.
You might want to take look at the Geofire library. I have not used it and its capabilities seem to be related to proximity filtering, not the sort-by-distance feature you need, but maybe you can adjust your requirements to make use of its features.
I concur with Qbix and would normally post this as a comment, but I want to make sure the Geohash comment doesn't get lost in the small-text shuffle.
GeoFire uses Geohash codes to create its keys and allow range-matching. For basic apps this may be fine, but the second you go beyond the US this creates a lot of trouble because it doesn't work well around the equator and Prime Meridian (UK). See the Wikipedia Page on Geohash for details, specifically: Edge case locations close to each other but on opposite sides of the 180 degree meridian will result in Geohash codes with no common prefix (different longitudes for near physical locations)..
Firebase is an amazing product, but not a one-size-fits-all tool. If you need good Geo-based search/matching, use a tool like ElasticSearch, MySQL, Algolia, etc. that support it directly. Reducing the number of components in cases like this doesn't decrease complexity, it increases it.
You can use Geofirestore library
https://github.com/geofirestore/geofirestore-js
here is the example used:
import * as firebase from 'firebase/app';
import 'firebase/firestore';
import { GeoCollectionReference, GeoFirestore, GeoQuery, GeoQuerySnapshot } from 'geofirestore';
// Initialize the Firebase SDK
firebase.initializeApp({
// ...
});
// Create a Firestore reference
const firestore = firebase.firestore();
// Create a GeoFirestore reference
const geofirestore: GeoFirestore = new GeoFirestore(firestore);
// Create a GeoCollection reference
const geocollection: GeoCollectionReference = geofirestore.collection('restaurants');
// Create a GeoQuery based on a location
const query: GeoQuery = geocollection.near({ center: new firebase.firestore.GeoPoint(40.7589, -73.9851), radius: 1000 });
// Get query (as Promise)
query.get().then((value: GeoQuerySnapshot) => {
console.log(value.docs); // All docs returned by GeoQuery
});
where {1000} in the query is the radius of location search
Related
I'm making an android app where user can find a book in his/her vicinity and buy it if interested. I am using firebase and geoqueries/geofire.
I want to make a SearchActivity where user can search a book by it's title in his/her vicinity.
my Firebase Database Structure looks like :
books
PushKey
g:
l:
0:
1:
name:"some book name"
If i try to query this with some book name, it works fine using :
myRef.orderByChild("name").equalTo("some book name").addChildEventListener()....//The rest of the code here...
If i try to query nearby books,then also it works fine using :
geoQuery = geoFire.queryAtLocation(myLocation, 10);
I'm stuck at combining these two.
How can i search for a specific book name only in the vicinity?
For example : I want to search a book whose name is "ABCD" and is in a radius of 10km.
OR
Search a book by name and tell which one is nearest(In case several books are uploaded with same name at different locations).
Is it possible to do so? If not, what workaround(maybe other than firebase, but has to cheap and affordable) can i opt for where i can achieve this desired result?
The Firebase Database can only query by a single property. The fact that GeoFire does something that is seemingly at odds with that (querying by longitude and latitude) is because it combines these values into a single property in a magical format called a geohash (the g property in your JSON).
Combining values into a single property is the only way to get Firebase to filter on multiple values. For example, you could prefix the g property with your book title to get some book name_geohashvalue and could then filter on that.
The two main problems with that:
This only works if you know the entire book title, you can do a prefix match on the title, as you'll already need to use the prefix match for the geohash.
This is not built in to GeoFire, so you will have to fork that library and build it yourself.
If you do try to do this yourself, and get stuck, we can try to help. But fair warning: this won't be trivial, and you'll need to understand how geohashes, geofire, and Firebase's query model work quite well. For an intro, I recommend watching the video of my talk on performing geoqueries on Firebase and Firestore.
If you want something a bit less involved, you have two main options:
Retrieve all nodes within range, and then filter for the book title client-side.
Store a separate GeoFire tree for each book title, so that you can initialize your GeoFire object based on the book title, and only get keys within range for that specific book title.
Between these two, I'd recommend starting with #1.
What I'm trying is to get the documents that are in range of a point.
Following the videos and taking a look to the geo-hash library for android, I'm able to get the bounding box and get the necessary geohashes to query in firebase.
As example:
Point -> LatLng(40.4378698,-3.8196205) (Madrid,Spain)
Radius -> 5000meters (5 km)
The boundary box I get is:
[GeoHashQuery{startValue='ezjnh', endValue='ezjns'}, GeoHashQuery{startValue='ezjjs', endValue='ezjj~'}, GeoHashQuery{startValue='ezjq0', endValue='ezjq8'}, GeoHashQuery{startValue='ezjm8', endValue='ezjmh'}]
One I have this list, I call Firebase to retrieve the documents that "match" this criteria:
fun getUpTos(queries: MutableSet<GeoHashQuery>, onSuccessListener: OnSuccessListener<QuerySnapshot>, onFailureListener: OnFailureListener) {
var reference = Firebase.firestore.collection("pois")
queries.forEach { entry ->
reference
.whereGreaterThanOrEqualTo("geohash", entry.startValue)
.whereLessThanOrEqualTo("geohash", entry.endValue)
}
reference.get()
.addOnSuccessListener(onSuccessListener)
.addOnFailureListener(onFailureListener)
}
At this moment I have around 20 Poi's in firebase to start doing the test. All Poi's are in Barcelona and 1 in Madrid.
After doing the query, I'm gettin ALL the poi's, when it was supposed to just return the Madrid poi.
How can I get only the pois that fit the query? It seems is not working properly (or I'm doing obviously something wrong)
Is possible to achieve this type of querys?
Is possible to achieve this type of querys?
Yes, it is possible.
When you are iterating through your queries MutableSet, at every iteration you are creating a new Query object. So you cannot simply call get() outside the loop only once and expect to have all those queries working. What can you do instead, is to add the get() call to every query inside the loop. The type of the object that results is Task<QuerySnapshot>. Add all those Task objects to a List<Task<QuerySnapshot>>. In the end, pass that list of tasks to Tasks's whenAllSuccess(Collection> tasks) method as explained in my answer from the following post:
Android Firestore convert array of document references to List<Pojo>
I would like to query my data set for results that occur within a date range and location range (geohash range). I am struggling to find a good solution.
I am using GeoFire and have tried various methods of segrigating my data either by date or location. It's easy enough to build a node for each day and plop geohashes inside but how can i then page through that data without downloading all 5000 results in the city of NYC for example?
If i could sort first on the geohash and then on the date this would be easily achieved but i can not do that in firebase or any NoSQL database.
Someone must have encountered this multirange query problem and i hope you can share what you learned.
Thanks
It's easy enough to build a node for each day and plop geohashes inside but how can i then page through that data without downloading all 5000 results in the city of NYC for example?
Please let me know if I'm misinterpreting your question. Would you be able construct a DatabaseReference to the node representing a specific day, creating a GeoFire object and a GeoQuery, repeating for each day?
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("path/to/dayNode");
GeoFire geoFire = new GeoFire(ref);
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(37.7832, -122.4056), 0.6); // Use this for addGeoQueryEventListener() or something else
// See https://github.com/firebase/geofire-java
This would allow you to only download the locations in the 0.6 km surrounding 37.7832, -122.4056. To prevent downloading too many results at once, start with a small radius and increase from there?
As a side note, I believe Firebase Firestore allows for multiple cursor conditions such as:
db.collection("locations")
.orderBy("locationhash")
.orderBy("date")
.startAt(locationHash, startDate)
.limit(25);
Unfortunately, I don't think you would be able to specify a radius while sorting by date and paginating unless you extended the work done in GeoFire or GeoFirestore. Hopefully someone else has a better idea.
I'm using a Firebase Firestore for android to store data. I tried to search data from documents.
My Firestore structure is:
Collection (Products) - Document (Auto Generated ID) - Field (NumberOfPeople,OfferStartDate,OfferEndDate,OfferPrice,Location)
I wrote query for search data on those fields.
CollectionReference collectionOfProducts = db.collection("Products");
collectionOfProducts
.whereEqualTo("Location", location)
.whereGreaterThanOrEqualTo("OfferPrice", offerPrice)
.whereLessThanOrEqualTo("OfferPrice", offerPrice)
.whereGreaterThanOrEqualTo("OfferStartDate", date)
.whereLessThanOrEqualTo("OfferEndDate", date)
.get()
I want search result like this: An offer which is between start date and end date, where offer price is greater than equal or less than equal on give price range. This query is not working in android studio.
How to do this in firestore firebase?
According to the official documentation regarding Cloud Firestore queries, please note that there some query limitations:
In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.
So a Query object that contains a call to both methods:
.whereGreaterThanOrEqualTo("OfferStartDate", date)
.whereLessThanOrEqualTo("OfferEndDate", date)
Is actually not possible, as "OfferStartDate" and "OfferEndDate" are different properties.
The best solution I can think of is to use only one of these method calls and do the other filtering on the client.
Another possible solution might be to use denormalization and duplicate the data
in certain intervals. In this way, you'll always know the time periods and you'll be able to create the corresponding queries.
To the best of my knowledge, Firestore only lets you use where<Greater/Less>ThanOrEqualTo() and where<Greater/Less>Than() a single field and all other filter operations on other fields can only be whereEqualTo().
Some workarounds for your specific case include -
1) Modifying your query to
collectionOfProducts
.whereGreaterThanOrEqualTo("OfferStartDate", date)
.whereEqualTo("Location", location)
.get()
And then performing the subsequent filtering on the result in your app code.
Alternately, you can perform your filter on "OfferPrice" and "Location" in your query and the remaining filters can be applied to the query result.
2) You can use firebase functions or other server code to write logic that performs customized filtering and fetch the result that way.
i was having same issue with this, but i found a work around that takes sometime to write.
lets say you want to search for a particular keyword(in this case the value of a field inside a document), and you want firebase to search multiple field instead of just looking for 1 particular field.
this is what you want to do.
const searchTerm = document.createElement('input')
db.collection('collection').where('field1', '==', `${searchTerm.value}`)
.get()
.then((snapshot) => {
if(snapshot.size === '0'){
db.collection('collection').where('field2', '==', `${searchTerm.value}`)
.get()
.then((snapshot) => {
if(snapshot.size === 0) {
db.collection......and repeat
}
})
}
})
in summary, the above code is basically telling js to search for the term again with a different field if the result of the previous query is 0. I know this solution might not be able to work if we have a large quantity of fields. But for folks out there that are working with small number fields, this solution might be able to help.
I really do hope firestore one day would allow such feature, but here is the code it worked fine for me.
the problem I have now is to allow search input to be able to search without have me to complete the word. I do currently have an idea how this would be, but just need some time to put together.
I have an application where I need to return the first user found that meets certain criteria, some of that criteria is having a certain number of objects stored.
For example, let's say I want to return the first store I can find that has at-least 3 employees with atleast two children. I know, what an odd-ball example. So I would have a query something like this:
PFUser.query()?
.whereKey("objectId", notEqualTo: PFUser.currentUser()?.objectId!)
.includeKey("stores.employees.children")
// .whereCountForkey("stores.employees", greaterThan: 2)
// .whereCountForKey("stores.employees.children", greaterThan: 1)
.getFirstObject();
Notice the commented out lines, I'm trying to find a way to do soemthing like this in a single query. I'm using parse, which I believe uses MongoDB on the back end, but I don't believe you can execute custom database queries..?
This is a mobile application for both iOS and Android, although the code shown is in SWIFT I have two variations of the project. Examples in either swift, obj-C, Java, or C# will be fine.
Also more than happy with Cloud-code solutions.
There is an example in the documentation
var Team = Parse.Object.extend("Team");
var teamQuery = new Parse.Query(Team);
teamQuery.greaterThan("winPct", 0.5);
var userQuery = new Parse.Query(Parse.User);
userQuery.matchesKeyInQuery("hometown", "city", teamQuery);
userQuery.find({
success: function(results) {
// results has the list of users with a hometown team with a winning record
}
});