How-to index arrays (tags) in CouchDB using couchdb-lucene - arrays

The setup:
I have a project that is using CouchDB. The documents will have a field called "tags". This "tags" field is an array of strings (e.g., "tags":["tag1","tag2","etc"]). I am using couchdb-lucene as my search provider.
The question:
What function can be used to get couchdb-lucene to index the elements of "tags"?
If you have an idea but no test environment, type it out, I'll try it and give the result here.

Well it was quite easy after I figured it out. Please realize that the $ character has no significance to the code, my fields in this case just begin with $. Posted the answer for anyone with this question in the future.
function(doc) {
var result = new Document();
for(var i in doc.$tags) {
result.add(doc.$tags[i]);
}
return result;
}

Perhaps the syntax has changed, but you can construct a view to search by any item in a document's array:
function(doc) {
for (var i=0; i<doc.page.length; i++) {
emit(doc.page[i].url, doc._id);
}
}

Related

DotNet Core Azure Search SDK - filtering results

We are trying to implement Filter functionality into Azure (Cognitive) Search. I was hoping to find some nice SDK methods that hide all the ugly parts, but so far the only example I found looks like this (source):
SearchParameters parameters = new SearchParameters()
{
Filter = String.Format("groupIds/any(p:search.in(p, '{0}'))", string.Join(",", groups.Select(g => g.ToString()))),
Select = new[] { "application essays" }
};
I was wondering, whether I am missing some docs. Or maybe it is on the roadmap?
Check out our new Azure.Search.Documents SDK we released last month. It does have OData filter helps as you can find here:
int stars = 4;
SearchOptions options = new SearchOptions
{
// Filter to only Rating greater than or equal our preference
Filter = SearchFilter.Create($"Rating ge {stars}"),
Size = 5, // Take only 5 results
OrderBy = { "Rating desc" } // Sort by Rating from high to low
};
It'll escape string parameters correctly. The OData $filter syntax still requires raw input, but the type helpers in the formattable string should make your situation easier: you don't have to worry about escaping values yourself.

How to ensure unique key while using Parse database

I need unique records my Parse object but due to the 'saveinbackground' a simple find() on the client didn't do the job. Even adding and setting a boolean like bSavingInbackGround and skip an additional save if true wouldn't prevent my app from creating duplicates.
Ensure unique keys is obvious very helpfull in many (multi user) situations.
Parse CloudCode would be the right way but I didn't find a proper solution.
After doing some trail and error testing I finally got it to work using Cloud Code. Hope it helps someone else.
My 'table' is myObjectClass and the field that needs to be unique is 'myKey'.
Add this to main.js an upload to Parse Server Cloud Code.
Change myObjectClass and 'myKey' to suit your needs:
Parse.Cloud.beforeSave("myObjectClass", function(request, response) {
var myObject = request.object;
if (myObject.isNew()){ // only check new records, else its a update
var query = new Parse.Query("myObjectClass");
query.equalTo("MyKey",myObject.get("myKey"));
query.count({
success:function(number){ //record found, don't save
//sResult = number;
if (number > 0 ){
response.error("Record already exists");
} else {
response.success();
}
},
error:function(error){ // no record found -> save
response.success();
}
})
} else {
response.success();
}
});
Your approach is the correct approach, but from a performance point of view, I think using query.first() is faster than query.count() since query.first() will stop as soon as it finds a matching record, whereas query.count() will have to go through the whole class records to return matching the number of matching records, this can be costly if you have a huge class.

Query arrays values with Firebase

Is there any way to on Firebae to filter data in an array?
I have this model on my Firebase:
-KABIGeWnBMUKjLTcvp8
deviceToken:"7DE60240CB4B712F05A009F32358610C1327917E7E68409..."
favorites
0:"Masha"
1:"moksha"
name:"juan"
And the problem is that I can't find any method to get all "users" that contain a certain value on the "favorites" array field.
Nope, that's not an option See firebase equivalent to sql where in ().
Instead: invert your data structure to make this query possible:
items_by_favorites
"Masha"
"-KABIGeWnBMUKjLTcvp8"
"moksha"
"-KABIGeWnBMUKjLTcvp8"
Now you can look up the item keys for Masha with a simple read: ref.child('items_by_favorites/Masha') and then load each item:
ref.child('items_by_favorites/Masha').on('value', function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key();
ref.child('items').child(key).once('value', function(itemSnapshot) {
console.log(itemSnapshot.val());
});
});
})
First of all your question is answered deep in the guide for retrieving data, which is where I got this answer. It's under complex queries, then range queries, should you want more info.
var ref = new Firebase("https://dinosaur-facts.firebaseio.com/dinosaurs");
ref.orderByChild("height").equalTo(25).on("child_added", function(snapshot) {
console.log(snapshot.key());
});
The basic idea is that you need to first order the reference by a common child value, and then call .equalTo() to end up with a query that yields what you want.
Also you can call order by child like
ref.orderByChild("height/sublevel")
To drill deeper in the tree.
FirebaseFirestore.instance.collection('your collection name').where('favorite', arrayContains: 'Masha').snapshot();

Angularjs: How to filter selected fields from an object array

Lets say I have an object array like this
peoples = [{name:"Joe",age:21,sex:'M'},
{name:"Smith",age:18,sex:'M'},{name:"Sally",age:25,sex:'F'}];
Some of these fields are irrelevant so I need to remove attribute age and sex from all of above objects.
This is the desired output.
[{name:"Joe"},{name:"Smith"},{name:"Sally"}];
Update:
It would be nice if I can use angularjs inbuilt service like $filter.
Well I don't see the need to remove the unnecessary objects since you only access the fields you want. Otherwise you can create a new array from the one you have created chucking out what you don't want.
var peoples = [{name:"Joe",age:21,sex:'M'},{name:"Smith",age:18,sex:'M'},{name:"Sally",age:25,sex:'F'}];
var names = [];
peoples.forEach(function(entry) {
names.push({name:entry.name});
});
console.log("New Names:",names); //[{name:"Joe"},{name:"Smith"},{name:"Sally"}];
Just do:
for( var i = 0; i < peoples.length; ++i ) {
delete peoples[i].age;
delete peoples[i].sex;
}
delete is the keyword created for this

Using CouchDB-lucene how can I index an array of objects (not values)

Hello everyone and thanks in advance for any ideas, suggestions or answers.
First, the environment: I am using CouchDB (currently developing on 1.0.2) and couchdb-lucene 0.7. Obviously, I am using couchdb-lucene ("c-l" hereafter) to provide full-text searching within couchdb.
Second, let me provide everyone with an example couchdb document:
{
"_id": "5580c781345e4c65b0e75a220232acf5",
"_rev": "2-bf2921c3173163a18dc1797d9a0c8364",
"$type": "resource",
"$versionids": [
"5580c781345e4c65b0e75a220232acf5-0",
"5580c781345e4c65b0e75a220232acf5-1"
],
"$usagerights": [
{
"group-administrators": 31
},
{
"group-users": 3
}
],
"$currentversionid": "5580c781345e4c65b0e75a220232acf5-1",
"$tags": [
"Tag1",
"Tag2"
],
"$created": "/Date(1314973405895-0500)/",
"$creator": "administrator",
"$modified": "/Date(1314973405895-0500)/",
"$modifier": "administrator",
"$checkedoutat": "/Date(1314975155766-0500)/",
"$checkedoutto": "administrator",
"$lastcommit": "/Date(1314973405895-0500)/",
"$lastcommitter": "administrator",
"$title": "Test resource"
}
Third, let me explain what I want to do. I am trying to figure out how to index the '$usagerights' property. I am using the word index very loosely because I really do not care about being able to search it, I simply want to 'store' it so that it is returned with the search results. Anyway, the property is an array of json objects. Now, these json objects that compose the array will always have a single json property.
Based on my understanding of couchdb-lucene, I need to reduce this array to a comma separated string. I would expect something like "group-administrators:31,group-users:3" to be a final output.
Thus, my question is essentially: How can I reduce the $usagerights json array above to a comma separated string of key:value pairs within the couchdb design document as used by couchdb-lucene?
A previous question I posted regarding indexing of tagging in a similar situation, provided for reference: How-to index arrays (tags) in CouchDB using couchdb-lucene
Finally, if you need any additional details, please just post a comment and I will provide it.
Maybe I am missing something, but the only difference I see from your previous question, is that you should iterate on the objects. Then the code should be:
function(doc) {
var result = new Document(), usage, right;
for(var i in doc.$usagerights) {
usage = doc.$usagerights[i];
for(right in usage) {
result.add(right + ":" + usage[right]);
}
}
return result;
}
There's no requirement to convert to a comma-separated list of values (I'd be intrigued to know where you picked up that idea).
If you simply want the $usagerights item returned with your results, do this;
ret.add(JSON.stringify(doc.$usagerights),
{"index":"no", "store":"yes", "field":"usagerights"});
Lucene stores strings, not JSON, so you'll need to JSON.parse the string on query.

Resources