Using NSURL APIs, how can a I determine which tags are set on a directory? The only API I'm aware of does indeed return a number associated with tags, but I'm not sure how to extract the specific tags from the returned value.
For example this will return 4 for directory B. I need to determine that both Blue and Green tags are set.
let url = URL(fileURLWithPath: "/Users/Username/Desktop/Test/B")
let resourceValues = try! url.resourceValues(forKeys:
[URLResourceKey.labelNumberKey])
print(resourceValues.labelNumber!) // 4
I've seen some use an enum to map the results back to Swift, but this doesn't seem to handle multiple tags.
enum LabelColor: Int {
case none
case gray
case green
case purple
case blue
case yellow
case red
case orange
}
So it seems an OptionSet would be the best implementation here for Swift.
If someone can point me to the algorithm used to extract tags from the total number that would be great!
I'm not aware of any way to grab the numeric values to the tags other than through a shell.
However, you can, as #Martin-R suggested, get an array of the tags attached to the file. I think this is best because user's can create custom tags, which may be hard to identify numerically.
I would suggest getting the array by using ⤵︎
var itemTags: [String] = []
do {
let resources = try url.resourceValues(forKeys: [.tagNamesKey])
if let tags = resources.tagNames {
itemTags = tags // ["Green", "Red"]
}
} catch {}
Once you have the array then you can just use some kind of enum & switch, for loop, or if/else to match it to colours you designate yourself.
Secret Sauce ⤵︎
Now the problem is that if you're sandboxed and you try this you'll probably only get one tag I imagine. It'll be the front-most tag.
This is just another one of Apple's security measures. To get around this your sandbox needs to extend to this resource.
It's a bit much to get into security scopes in this post, but here are two things you could try.
Use NSOpenPanel() to get the url or parent directory url. This will automatically give you privileges to that resource for that session. You can bookmark it, resolve it, and start accessing it.
Stop using the sandbox
Unfortunately, I don't know of other solutions that would work. I chose the first and it's the least painful at the moment.
I'm trying to create filters for a search on an Android app where a specific field in Algolia must exactly match the given String in order to come up as a hit. For example if Algolia has a field like "foo" and I only want to return hits where "foo" is equal to "bar", then I would expect that I would have to use a line of code like this:
query.setFilters("foo: \"bar\"");
Any guesses as to why this isn't working like I see in the examples or how to do so?
Ah, I thought that attributesForFaceting was done by setting what was searchable or not. It was on a different page within the dashboard than I was previously using. Thanks #pixelastic.
For example, could they type "-Adventure" if they want results without the word adventure to appear in their search results?
Sure thing! You need to enable the advancedSyntax feature and then the - in front of words will be interpreted as a NOT.
index.search('Crazy -Adventure', { advancedSyntax: true }).then(...);
Will search all objects with Crazy and without Adventure.
How does one use Firebase to do basic auto-completion/text preview?
For example, imagine a blog backed by Firebase where the blogger can tag posts with tags. As the blogger is tagging a new post, it would be helpful if they could see all currently-existing tags that matched the first few keystrokes they've entered. So if "blog," "black," "blazing saddles," and "bulldogs" were tags, if the user types "bl" they get the first three but not "bulldogs."
My initial thought was that we could set the tag with the priority of the tag, and use startAt, such that our query would look something like:
fb.child('tags').startAt('bl').limitToFirst(5).once('value', function(snap) {
console.log(snap.val())
});
But this would also return "bulldog" as one of the results (not the end of the world, but not the best either). Using startAt('bl').endAt('bl') returns no results. Is there another way to accomplish this?
(I know that one option is that this is something we could use a search server, like ElasticSearch, for -- see https://www.firebase.com/blog/2014-01-02-queries-part-two.html -- but I'd love to keep as much in Firebase as possible.)
Edit
As Kato suggested, here's a concrete example. We have 20,000 users, with their names stored as such:
/users/$userId/name
Oftentimes, users will be looking up another user by name. As a user is looking up their buddy, we'd like a drop-down to populate a list of users whose names start with the letters that the searcher has inputted. So if I typed in "Ja" I would expect to see "Jake Heller," "jake gyllenhaal," "Jack Donaghy," etc. in the drop-down.
I know this is an old topic, but it's still relevant. Based on Neil's answer above, you more easily search doing the following:
fb.child('tags').startAt(queryString).endAt(queryString + '\uf8ff').limit(5)
See Firebase Retrieving Data.
The \uf8ff character used in the query above is a very high code point
in the Unicode range. Because it is after most regular characters in
Unicode, the query matches all values that start with queryString.
As inspired by Kato's comments -- one way to approach this problem is to set the priority to the field you want to search on for your autocomplete and use startAt(), limit(), and client-side filtering to return only the results that you want. You'll want to make sure that the priority and the search term is lower-cased, since Firebase is case-sensitive.
This is a crude example to demonstrate this using the Users example I laid out in the question:
For a search for "ja", assuming all users have their priority set to the lowercased version of the user's name:
fb.child('users').
startAt('ja'). // The user-inputted search
limitToFirst(20).
once('value', function(snap) {
for(key in snap.val()){
if(snap.val()[key].indexOf('ja') === 0) {
console.log(snap.val()[key];
}
}
});
This should only return the names that actually begin with "ja" (even if Firebase actually returns names alphabetically after "ja").
I choose to use limitToFirst(20) to keep the response size small and because, realistically, you'll never need more than 20 for the autocomplete drop-down. There are probably better ways to do the filtering, but this should at least demonstrate the concept.
Hope this helps someone! And it's quite possible the Firebase guys have a better answer.
(Note that this is very limited -- if someone searches for the last name, it won't return what they're looking for. Hence the "best" answer is probably to use a search backend with something like Kato's Flashlight.)
It strikes me that there's a much simpler and more elegant way of achieving this than client side filtering or hacking Elastic.
By converting the search key into its' Unicode value and storing that as the priority, you can search by startAt() and endAt() by incrementing the value by one.
var start = "ABA";
var pad = "AAAAAAAAAA";
start += pad.substring(0, pad.length - start.length);
var blob = new Blob([start]);
var reader = new FileReader();
reader.onload = function(e) {
var typedArray = new Uint8Array(e.target.result);
var array = Array.prototype.slice.call(typedArray);
var priority = parseInt(array.join(""));
console.log("Priority of", start, "is:", priority);
}
reader.readAsArrayBuffer(blob);
You can then limit your search priority to the key "ABB" by incrementing the last charCode by one and doing the same conversion:
var limit = String.fromCharCode(start.charCodeAt(start.length -1) +1);
limit = start.substring(0, start.length -1) +limit;
"ABA..." to "ABB..." ends up with priorities of:
Start: 65666565656565650000
End: 65666665656565650000
Simples!
Based on Jake and Matt's answer, updated version for sdk 3.1. '.limit' no longer works:
firebaseDb.ref('users')
.orderByChild('name')
.startAt(query)
.endAt(`${query}\uf8ff`)
.limitToFirst(5)
.on('child_added', (child) => {
console.log(
{
id: child.key,
name: child.val().name
}
)
})
I want to add an Search Box in list Field. so that When i Enter a letter, then it will show the names starting with the letter 'A' , and so on. Iam using Vector to save the list of contacts same as the image shown :
If you want to select from the Contacts, use the ContactList.choose() method.
DO NOT try to iterate through the entire contacts your self every time. Remember there are lot of people having thousands of contacts and your code will be very unresponsive.
See: https://stackoverflow.com/a/4436816/371534
However, if you want to have 'filter as you type' kind of functionality with some other data, use the KeywordFilterField. You can get a sample code for it in the BlackBerry JDK samples.
Set a FieldChangeListener (or listen for alphanumeric key presses) to your EditField. Then refresh the list each time. Filtering on entries starting with the string contained in the EditField.
I wrote this on a pc without the Blackberry plugin installed, so couldn't test it, but it should be something like this.
String prefix = editField.getText();
Enumeration e = list.items();
while(e.hasMoreElements())
{
PIMItem item = (PIMItem) e.nextElement();
String name = item.getString(PIMItem.NAME,0);
if (name.startsWith(prefix))
{
//TODO display on screen
}
}