Querying ravendb index using named field and collection - lucene

I have the following index in ravendb, Changes_Which is a named field and this maps to a sub collection
public My_Index()
{
Map = revisions => from revision in revisions
from change in revision.Changes
where (revision.AuditedType == "typeA")
select
new
{
revision.ChangeTimestamp,
Changes_Which = change.Which
};
}
the json for a revision looks like:
{
"AuditedType" : "typeA",
"Changes": [
{
"SubWhich": null,
"Which": "Regulation",
"Original": "Unknown",
"New": "Yes"
},
{
"SubWhich": null,
"Which": "Other",
"Original": "Unknown",
"New": "Yes"
},
{
"SubWhich": null,
"Which": "Regulation",
"Original": "Unknown",
"New": "Yes"
}
]
"ChangeTimestamp": "2011-05-03"
}
I am trying to filter the results from the index using the Changes_Which field:
var q = session.Advanced.LuceneQuery<revision>("My_Index/Index")
.WaitForNonStaleResultsAsOfLastWrite()
.AddOrder(paging.SortColumn, paging.SortOrder == "desc")
.Skip(((paging.CurrentPage - 1) * paging.RecordsPerPage))
.Take(paging.RecordsPerPage);
if (fromDate > DateTime.MinValue && toDate > DateTime.MinValue)
{
q = q.WhereGreaterThanOrEqual("ChangeTimestamp", fromDate)
.AndAlso()
.WhereLessThanOrEqual("ChangeTimestamp", toDate.AddDays(1).AddMinutes(-1));
}
if (removeNoChange)
{
q = q.AndAlso().Not.WhereEquals("Changes_Which", "Regulation");
}
This line:
q = q.AndAlso().Not.WhereEquals("Changes_Which", "Regulation");
does work, but only removes the first match on "Which": "Regulation", the third one in the collection remains.
Any suggestions appreciated.

The reason for this is that you are outputting different index entries for the same document.
When searching in RavenDB, you are searching on documents, and in your case, you filtered out the first index entry on that document, but there are other index entries that match, so it is returned.
You are probably better off with:
Map = revisions => from revision in revisions
where (revision.AuditedType == "typeA")
select
new
{
revision.ChangeTimestamp,
Changes_Which = revision.Changes.Select(chnage => change.Which)
};
This will output just a single entry per document (which is recommended) and will result in filtering of all documents that were changed because of Regulation

Related

Node.JS Firebird nested Request in forEach

I'm fairly new to Node.Js and JS in general. I was trying to create a SQL query for a table. After that, I create a separate query to get one/multiple rows from another table that have the same ID (in here LGID) as the row from the first query. Like a JOIN command but I need the results from the second query as a separate object.
I am using the "node-firebird" client.
Everything works, except "the entire code from the second query"
My expected Output would be something like this:
{
"KDNR": "1",
"NUMBER": "+49123456789",
"NAME1": "John",
"NAME2": "Doe,
"STRASSE": "Musterstrasse 38",
"PLZ": "12345",
"ORT": "Musterstadt",
"TEL": "+49123456787",
"TNK": {
{"NAME": "TANK1"},
{"NAME": "TANK2"},
}
}
db.query('SELECT * FROM LG WHERE NAME1 = ?', [req.userData.orga], (err, lgResult) => {
if (err)
throw err;
lgResult.forEach((row) => {
row.KDNR = ab2str(row.KDNR);
row.NUMBER = ab2str(row.NUMBER);
row.NAME1 = ab2str(row.NAME1);
row.NAME2 = ab2str(row.NAME2);
row.STRASSE = ab2str(row.STRASSE);
row.PLZ = ab2str(row.PLZ);
row.ORT = ab2str(row.ORT);
row.TEL = ab2str(row.TEL);
console.log('test');
db.query('SELECT * FROM TNK WHERE LGID = ?', [1], (errN, tnkResult) => {
if (errN)
return res.status(500).json({ error: 'Error queryisssng DB' });
console.log('another test');
row.TNK.push(tnkResult);
});
return res.status(200).json(lgResult);
});
I can see the test multiple times in the console, but not the another test.
I hope this is enough code for you to help.

How to retrieve all values for a particular key in karate

How to retrieve partNumbers from below response. In below response
"10000061","10000062","10000063"
are dynamic in nature. I have to match these partNumbers with data table partnumbers.( In a response there could be more than 10 part numbers(based on input) and i have to validate them.)
{ "added": true, "lineItems": { "1111111": { "itemCore": { "partNumber":
"10000061" } }, "222222": { "itemCore": { "partNumber": "10000061" } },
"3333333": { "itemCore": { "partNumber": "10000063" } } } }
Tried below
def partNum= get[0] response..itemCore.partNumber[*] but getting empty array.
def partNum= get[0] response..itemCore.partNumber but getting empty value.
My below second approach also giving me empty value.
* def keys = function(obj){ return response.lineItems.keySet() }
* json dynamicValue= keys(response)
* print 'dynamic value '+dynamicValue
* def first = dynamicValue[0]
* print response.lineItems.dynamicValue[0].itemCore.partNumber
* print response.lineItems.first.itemCore.partNumber
For retrieving data for a particular key, you can use deep scan operator in jsonPath,
* def partNumbers = karate.jsonPath(response,"$..partNumber")
Here's another solution, using karate.forEach() which can also operate on a map, not just a list:
* def keys = []
* eval karate.forEach(response.lineItems, function(k){ keys.add(k) })
* print keys

Azure Stream Analytics: Get Array Elements by name

I was wondering if it is possible for me to get the elements of the array by the name of property than the position. For example, this is my incoming data:
{
"salesdata": {
"productsbyzone": {
"zones": [{
"eastzone": "shirts, trousers"
},
{
"westzone": "slacks"
},
{
"northzone": "gowns"
},
{
"southzone": "maxis"
}
]
}
}
}
I intend to move this to a SQL database and I have columns within the database for each zone. The problem is that the order of different zones changes within each json. I was successfully using the following query until I realized that the position of the zones changes within each json:
WITH
salesData AS
(
SELECT
(c.salesdata.productsbyzone.zone,0) as eastzone,
(c.salesdata.productsbyzone.zone,1) as westzone,
(c.salesdata.productsbyzone.zone,2) as northzone,
(c.salesdata.productsbyzone.zone,3) as sourthzone,
FROM [sales-data] as c
)
SELECT
eastzone.eastzone as PRODUCTS_EAST,
westzone.westzone as PRODUCTS_WEST,
northzone.northzone as PRODUCTS_NORTH,
southzone.southzone as PRODUCTS_SOUTH
INTO PRODUCTSDATABASE
FROM salesData
Need a way to reference these fields by the name rather than by the position.
I recommend a solution: Use the JavaScript UDF in the azure stream job to complete the columns sort.
Please refer to my sample:
Input data(upset the order):
{
"salesdata": {
"productsbyzone": {
"zones": [{
"westzone": "slacks"
},
{
"eastzone": "shirts, trousers"
},
{
"northzone": "gowns"
},
{
"southzone": "maxis"
}
]
}
}
}
js udf code:
function test(arg) {
var z = arg;
var obj = {
eastzone: "",
westzone: "",
northzone: "",
southzone: ""
}
for(var i=0;i<z.length;i++){
switch(Object.keys(z[i])[0]){
case "eastzone":
obj.eastzone = z[i]["eastzone"];
continue;
case "westzone":
obj.westzone = z[i]["westzone"];
continue;
case "northzone":
obj.northzone = z[i]["northzone"];
continue;
case "southzone":
obj.southzone = z[i]["southzone"];
continue;
}
}
return obj;
}
You can define the order you want in the obj parameter
SQL:
WITH
c AS
(
SELECT
udf.test(jsoninput.salesdata.productsbyzone.zones) as result
from jsoninput
),
b AS
(
SELECT
c.result.eastzone as east,c.result.westzone as west,c.result.northzone as north,c.result.southzone as south
from c
)
SELECT
b.east,b.west,b.north,b.south
INTO
jaycosmos
FROM
b
Output:
Hope it helps you.
You can use GetArrayElement to return array element then access to each property. Please refer the below query
WITH
salesData AS
(
SELECT
GetArrayElement(zones,0) as z
FROM [sales-data] as s
)
SELECT
z.eastzone
z.westzone
z.northzone
z.southzone
FROM PRODUCTSDATABASE
FROM salesData

Combine search criteria from different column

I have a Datatable with 3 columns, and one search box.
Any keyword in the search box should match the beginning of either Column 1, 2 or 3.
Example:
Given keyword "apple", these 3 rows will match:
"Apple pie" - "dessert" - "handmade"
"New pear dessert" - "dessert" - "apple tree"
"New apple dessert" - "apple dessert" - "handmade"
This row should not match:
"Old apple dessert" - "dessert" - "handmade apple product"
Code:
For exact match on beginning of any of 3 Column:
searchBox.on('keyup', $.proxy(function (e) {
// how to merge the search result?
this._table.column(0).search('^' + searchBox.val(), true,
false).draw();
this._table.column(1).search('^' + searchBox.val(), true,
false).draw();
this._table.column(2).search('^' + searchBox.val(), true,
false).draw();
}, this));
I found a solution.
According to the Datatable, we need a custom filter to achieve OR-condition on multiple columns: https://datatables.net/forums/discussion/11728/filtering-datatable-columns-using-or-logic
$.fn.dataTable.ext.search.push(function (settings, data, dataIndex) {
var keyword = searchBox.val();
keyword = keyword.toUpperCase();
var productDesc = data[1].toUpperCase();
var productExch = data[3].toUpperCase();
var productSymbol = data[5].toUpperCase();
if (productDesc.startsWith(keyword) || productExch.startsWith(keyword) || productSymbol.startsWith(keyword)) {
return true;
}
return false;
});
searchBox.on('keyup', $.proxy(function (e) {
this._table.draw(); // this._table.search(searchBox.val()).draw();
}, this));

Store Address book contact in sqlite titanium

So i am building an application that takes contact information from the address book and stores it into a Titanium Model for use later on in the user journey.
All other information is storing and returning correctly however the image of the contact always comes back blank for some reason.
The code for storing the address book information is as follows
if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_AUTHORIZED){
var people = Titanium.Contacts.getAllPeople();
var totalContacts = people.length;
var addressbook = [];
Alloy.Collections.contactsModel.numberOfContacts();
Ti.API.info(numberOfContacts);
if(totalContacts > 0){
var phoneContacts = [];
for (var index = 0; index < totalContacts; index++){
var person = people[index];
phoneContacts.push({
name:person.fullName,
phoneNumber:person.phone,
profileImage:person.image,
contactID:person.identifier
});
}
Alloy.Collections.contactsModel.reset(phoneContacts);
Alloy.Collections.contactsModel.each(function(_m) {
_m.save();
});
}
} else if (Ti.Contacts.contactsAuthorization == Ti.Contacts.AUTHORIZATION_UNKNOWN){
Ti.Contacts.requestAuthorization(function(e){
//Authorization is unknown so requesting for authorization
if (e.success) {
} else {
}
});
} else {
}
}
The model definition is as follows
exports.definition = {
config: {
columns: {
"friendID": "INTEGER PRIMARY KEY AUTOINCREMENT",
"contactID": "string",
"name": "string",
"phoneNumber": "string",
"emailAddress": "string",
"profileImage": "blob"
},
adapter: {
type: "sql",
collection_name: "contactsModel",
idAttribute:"friendID"
}
},
extendModel: function(Model) {
_.extend(Model.prototype, {
// extended functions and properties go here
});
return Model;
},
extendCollection: function(Collection) {
_.extend(Collection.prototype, {
collection.trigger('sync');
},
}
}
*/
});
return Collection;
}
};
The image was working fine when collecting it from the address book and putting it into a list view. However when it's saved and then i attempt to retrieve it and put it into a list view where the problem occurs.
Thanks guys.
Something to consider is not to put all the information in the database. The reason is that when it changes on the device, those changes will not reflect in the records you've stored.
Here's how I approach it:
var db = Ti.Database.open( 'person' );
db.execute( "CREATE TABLE IF NOT EXISTS people( " +
"id INTEGER PRIMARY KEY, identifier TEXT, pid INTEGER);" );
var person = db.execute( 'SELECT id, identifier, pid FROM people' );
while( person.isValidRow( ) ) {
var contact,
contact_id;
if( OS_IOS ) {
contact_id = person.fieldByName( 'identifier' );
}
if( OS_ANDROID ) {
contact_id = person.fieldByName( 'pid' );
}
contact = Ti.Contacts.getPersonByIdentifier( contact_id );
var p_image = contact.image || "/images/user";
...
}
Essentially, I store the contact's ID in the database and then use that identifier to retrieve the user's info.
Note that the user's record does not always have an image, so it is a good idea to provide a default.