Querying for specific strings in Firebase? - xcode

I have a database which looks something like this:
"trucks" : {
"3705ec54-8a2e-4eb1-8bb9-ab2243645ac1" : {
"email" : "sandwiches#123.com",
"name" : "Sandwich Truck",
"phone" : "123 - 456 - 1234",
"provider" : "password",
"selfDescription" : "We serve delicious sandwiches at a moderate price. Cards Accepted.",
"userType" : "truck",
"website" : "www.sandwiches.com"
},
"54fea8cd-2203-46bd-aaf8-9d823e85313d" : {
"email" : "pizza#123.com",
"name" : "Supa Pizza",
"phone" : "619 - 222 - 4444",
"provider" : "password",
"selfDescription" : "We serve incredible pizza at an incredibly unfair price.",
"userType" : "truck",
"website" : ""
},
"6c542367-507c-4d01-af2c-bf93a7efaef4" : {
"email" : "fries#123.com",
"name" : "Pete's Fries",
"phone" : "11111111111111111111",
"profilePhoto" : "",
"provider" : "password",
"selfDescription" : "We make some of the world's most delicious fries.",
"userType" : "truck",
"website" : ""
},
"7c6c4395-aec1-443c-908d-62db517def5e" : {
"email" : "chili#123.com",
"name" : "Mark's Chili",
"phone" : "1-800-CHIL-LLL",
"profilePhoto" : "",
"provider" : "password",
"selfDescription" : "We serve the most delicious chili, chili your mamma's mamma is scared to try.",
"userType" : "truck",
"website" : ""
}
}
I'd like to implement a search bar for which trucks whose names match the search terms will be returned. For example, any truck name containing the string 'fries' will have its ID committed to an array of search result IDs.
Here's what I've tried so far, but no dice. When I type in fries in my searchbar and hit the search button, it does not print "6c542367-507c-4d01-af2c-bf93a7efaef4"
let usersRef = Firebase(url: "https://•••••••••.firebaseIO.com/users")
usersRef.queryOrderedByChild("name").queryEqualToValue(searchBar.text).observeEventType(.ChildAdded, withBlock:{
snapshot in
print(snapshot.key)
})
I'm not even sure how close I am, any help would be massively appreciated.
Thanks!

You are querying using queryEqualToValue (Query.equalTo()), which will return result only if there is an exact match.
Explanation in Detail:
In the search bar if we are entering "fries" it will look for an exact match of "fries", which is not available in our document and hence the desired value "6c542367-507c-4d01-af2c-bf93a7efaef4" is not printed.
Instead if we give the value "Pete's Fries" in the search bar then we will get the result "6c542367-507c-4d01-af2c-bf93a7efaef4"
If we give partial value for our search, then we are trying to implement a search which is similar to LIKE Query in SQL. Please refer the below posts to get more info on "how to perform LIKE query in firebase"
How to perform sql "LIKE" operation on firebase?

Related

How to get original, not translated, name of the place using Google Places API?

I want to get info about some place, let's say I use this link https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJFfz5V9zi20ARwBqCYQXOIVM&fields=vicinity,name&key=MY_API_KEY, I will get this result
{
"html_attributions" : [],
"result" : {
"name" : "Try Bobry",
"vicinity" : "Dmytra Yavornytskoho Avenue, 42, Dnipro"
},
"status" : "OK"
}
but the original name of this place is Три бобри, not Try Bobry.
You say I can use language=uk parameter to get this name, but if I will use it - it will break another place, which original name is in English, for example this https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJHZkI69zi20ARfbLn6MV6S9o&fields=vicinity,name&key=MY_API_KEY&language=uk will return
{
"html_attributions" : [],
"result" : {
"name" : "Візьміть каву, щоб піти",
"vicinity" : "вулиця Барикадна, 5-7, Дніпро́"
},
"status" : "OK"
}
so it will be Візьміть каву, щоб піти instead of Take coffee to go.
All I want is to get places' names in the language of origin, not translated.

Google places API - Can I separate out the output?

Hiting the endpoint:
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=Hoboken%20NJ&fields=formatted_address,name&inputtype=textquery&key=xxxxxxxxxxxxxxxxxxx
Getting the result:
{
"candidates" : [
{
"formatted_address" : "New Jersey, USA",
"name" : "Hoboken"
}
],
"debug_log" : {
"line" : []
},
"status" : "OK"
}
What bugs me is that I can't find a way to separate out the region and country - Yes, I know I can parse the result myself. But is there an option I get shoot out to Google Places API to have the response separate out city/state(or region)/country in the returned JSON?
Something like:
{
"candidates" : [
{
"state" : "New Jersey",
"country" : "USA",
"name" : "Hoboken"
}
],
"debug_log" : {
"line" : []
},
"status" : "OK"
}
As far as I know, it isn't possible, you'll have to parse it. Places API is designed to search businesses and POIs at first place.
Google does have, however a geocoding API which seems to give out Postal Code, Country, State, Address, separetely.
There are also some free alternatives

How to insert an element into already present list in elastic search

Say I have documents stored like below.
document 1
{
id : '1',
title : "This is a test document1",
list : ["value1" , "value2"],
...
}
document 2
{
id : '2',
title : "This is a test document2",
valueList : ["value1" , "value2"],
...
}
I need to add some more elements to the valueList in the documents with a list of document ids using bulk api. The resulting should look like
document 1
{
id : '1',
title : "This is a test document1",
list : ["value1" , "value2", "value3"],
...
}
document 2
{
id : '2',
title : "This is a test document2",
valueList : ["value1" , "value2" , "value3"],
...
}
What can I do to achieve this?
I tried using the scripts but it only updates a single document.
Sorry am really new to elastic search. I could even be stupid on this question. Please forgive and make me clear with this question.
See Updating Document. It should be straightforward. You need to use _update and just to give you an idea, even though the documentation is nearly perfect, it could look like this:
POST /your_index/your_type/document1/_update
{
id : '1',
title : "This is a test document1",
list : ["value1" , "value2", "value3"]
}
This will update document1.
In case of bulk updates you should read Batch Processing and have a look at the Bulk API.
From the docs:
POST /your_index/your_type/_bulk
{ "update" : {"_id" : "document1", "_type" : "your_type", "_index" : "your_index"}}
{ "doc" : {"myfield" : "newvalue"} }
{ "update" : {"_id" : "document2", "_type" : "your_type", "_index" : "your_index"}}
{ "doc" : {"myfield" : "newvalue"} }
Please note that you can just use _update for Partial Updates.
The simplest form of the update request accepts a partial document as
the doc parameter, which just gets merged with the existing document.
Objects are merged together, existing scalar fields are overwritten,
and new fields are added.

Elastic search Update by Query to Update Complex Document

I have a use case of elastic search to update a doc.
My doc is something like this-
{
"first_name" : "firstName",
"last_name" : "lastName",
"version" : 1234,
"user_roles" : {
"version" : 12345,
"id" : 1234,
"name" : "role1"},
},
"groups" : {
"version" : 123,
"list": [
{"id":123, "name" : "ashd"},
{"id":1234, "name" : "awshd"},
]
}
}
Now depepeding on some feed I will either will be updating the parent doc or will be updating the nested doc.
I am able to find how to update the basic attributes like firstName and lastName but unable to get how to update complex/nested ones
I did something like from REST client-
"script": {
"inline": "ctx._source.user_roles = { "id" : 5678, "name" :"hcsdl"}
}
but its giving me exception-
Actual use case-
I will actually be getting a Map in java.
This key can be simple key like "first_name" or can be complex key like "user_role" and "groups"
I want to update the document using update by query on version.
The code I wrote is something like-
for (String key : document.keySet()) {
String value = defaultObjectMapper.writeValueAsString(document.get(key));
scriptBuilder.append("ctx._source.");
scriptBuilder.append(key);
scriptBuilder.append('=');
scriptBuilder.append(value);
scriptBuilder.append(";");
}
where document is the Map
Now I might get the simple fields to update or complex object.
I tried giving keys like user_roles.id and user_roles.name and also tried giving complete user_roles but nothing is working.
Can someone helpout
Try this with groovy maps instead of verbatim JSON inside your script:
"script": {
"inline": "ctx._source.user_roles = [ 'id' : 5678, 'name' : 'hcsdl']}
}

nested field query for mongodb (using ruby)

Sup, good folks of the internet.
Does anyone know how to make nested queries for mongodb? This is probably best explained by an example. To retrieve specific fields, I can use the :fields option to retrieve that field (e.g. suppose it is called "useful_field"):
collection.find({},{:fields => {"useful_field" => 1}})
But suppose that useful_field itself contains an array of many further fields, i.e
useful_field = [{"value_I_want"=>"useful","value_I_dont_want"=>"not_useful"}]
My aim is to select "value_I_want". Any thoughts?
Here is a specific entry that I am trying to deal with (a reply to a tweet):
{ "_id" : ObjectId("51b6f71b0364718d71e4bca5"),
"annotations" : { },
"resultType" : "Tweet",
"score" : 1,
"groupName" : "TweetsWithConversation",
"results" : [
{
"kind" : "Tweet",
"score" : 1,
"annotations" : { "ConversationRole" : "Ancestor" },
"value" : { "created_at" : "Fri Jun 07 19:47:51 +0000 2013",
"id" : NumberLong("343091955196104704"),
"id_str" : "343091955196104704",
"text" : "THIS_IS_WHAT_I_WANT",
etc. etc. (Apologies for the odd formatting)
I'm trying to use a method of the form that will let me do something like this:
db.collection.find({},{:fields { some_way_of_selecting(THIS_IS_WHAT_I_WANT)})
(I'm querying as part of a ruby script)
Otherwise, I'll have to go back into the dark world of regex. No-one wants that.
Try the following
db.collection.find({},{"useful_field.value_I_want": 1})
Maybe try this:
db.collection.find({"resultType" : "Tweet"}, {"results" : {$elemMatch : {"value.text" : "THIS_IS_WHAT_I_WANT"}}})
What you are trying to do is called "projection" - it's specifying what fields you want returned in the second argument to find.
In your case you simply want:
db.collection.find({}, {"results.value.text":1} )

Resources