concatenate attributes in search expression - amazon-web-services

I am trying to build Filter Expression in query for searching data in dynamodb.
var params = {
TableName: "ContactsTable",
ExpressionAttributeNames: {
"#lastName": "LastName",
"#firstName": "FirstName",
"#contactType": "ContactType"
},
FilterExpression: "contains(#lastName, :searchedName) or contains(#firstName, :searchedName)",
ExpressionAttributeValues: {
":companyContactType": event.query.companyContactType,
":searchedName": event.query.searchedValue
},
KeyConditionExpression: "#contactType = :companyContactType"
};
Users generally search for LastName, FirstName (they append comma to LastName as a common search pattern). However data is stored in separate attributes named LastName and FirstName so that they can search by that as well.
Is there a way by which I can dynamically concatenate these two fields something like contains(#lastName<append comma>#firstName, :searchedName)?

You should remove comma from user input, split words and, for each word, check if it is contained in both (first name and last name) and 'or' everything together, or even use begins_with instead of contains.
Ex. For "john smith" will result in
contains(#lastName, "john") or
contains(#lastName, "smith" ) or
contains(#firstName, "john") or
contains(#firstName, "smith")
Also contains() is case sensitive from what i know,so you might want to insert first name and last name as lowercase as well as lowercase user's search term.

Related

Select a certain map element in a list using PartiQL (DynamoDB)

Although I understood how to select a list item in a map using document paths (eg Devices.FireStick.DateWatched[0]),I cannot figure out how to do it the other way round.
Say, I have an attribute containing a list of maps:
"Text": [
{
"Right": "Line one, right text",
"Left": "Line one, left text"
},
{
"Right": "Line two, right text",
"Left": "Line two, left text"
}]
and I want to get the left text of line one in my PartiQL select. My naive approach:
select Text[0].Right from mytable where ID='123'
did not result in success.
You need to double-quote DynamoDB reserved words, such as "right".
Here's a working PartiQL query:
SELECT Text[0]."Right" FROM mytable WHERE id = '1'
Also, note that if your table name includes a hyphen then quote that too, for example:
SELECT Text[0]."Right" FROM "mytable-xyz" WHERE id = '1'

Regex for Parsing JSON

I have a column of data I'm reading in Tableau directly from Redshift. This column contains a JSON object. It looks like this:
{"Age": 58, "City": "Wisconsin Rapids", "Race": "Other", "State": "Wisconsin", "Gender": "Female", "Country": "United States"}
I wish to extract this data by generating a column with a calculated field for each data point of interest using Tableau's REGEXP_EXTRACT function. I.e. an Age column, a City column etc.
How do I write a line of regular expressions to get the value of 58 for Age, Wisoncsin Rapids for City, etc.
Thanks!
You can use this regex :
"Age"\s?+:\s?+"?([[:alnum:]\s]+)"?
to extract its value here Age for example
if you want other key for example State use State instead of Age
"State"\s?+:\s?+"?([[:alnum:]\s]+)"?
you'll find the value of the key in the first group
See here https://regex101.com/r/KA7PSl/2

filterWithSelect in ember model Tables, remove regex

Using Ember Models Table
If I have set a field in my database to include: male and female
and I have this in my columns:
{
"propertyName": "sex",
"title": "Sex",
"filterWithSelect": true,
"sortFilterOptions": true
},
This works great if I select 'female', but 'male' includes both sexes. Is there any way to restrict it to the entire string, instead of it doing a substring?
Ember-models-table's defaultFilter does not do regex, only substring search.
As per the documentation, you can supply your own filterFunction for each column taking the cell value to check, the filter string and optionally the whole record as arguments, e.g.
{
"propertyName": "sex",
"filterWithSelect": true,
"filterFunction": (value, filterString) => value === filterString,
},

Kettle database lookup case insensitive

I've a table "City" with more than 100k records.
The field "name" contains strings like "Roma", "La Valletta".
I receive a file with the city name, all in upper case as in "ROMA".
I need to get the id of the record that contains "Roma" when I search for "ROMA".
In SQL, I must do something like:
select id from city where upper(name) = upper(%name%)
How can I do this in kettle?
Note: if the city is not found, I use an Insert/update field to create it, so I must avoid duplicates generated by case-sensitive names.
You can make use of the String Operations steps in Pentaho Kettle. Set the Lower/Upper option to Y
Pass the city (name) from the City table to the String operations steps which will do the Upper case of your data stream i.e. city name. Join/lookup with the received file and get the required id.
More on String Operations step in pentaho wiki.
You can use a 'Database join' step. Here you can write the sql:
select id from city where upper(name) = upper(?)
and specify the city field name from the text file as parameter. With 'Number of rows to return' and 'Outer join?' you can control the join behaviour.
This solution doesn't work well with a large number of rows, as it will execute one query per row. In those cases Rishu's solution is better.
This is how I did:
First "Modified JavaScript value" step for create a query:
var queryDest="select coalesce( (select id as idcity from city where upper(name) = upper('"+replace(mycity,"'","\'\'")+"') and upper(cap) = upper('"+mycap+"') ), 0) as idcitydest";
Then I use this string as a query in a Dynamic SQL row.
After that,
IF idcitydest == 0 then
insert new city;
else
use the found record
This system make a query for file's row but it use few memory cache

DynamoDB scan on field which is array of structures

I have a DynamoDB table where one of the fields is an array of structures, each structure has several fields, let's say name and phone number. I want to make a (python, but it does not really matter) scan query for entries where one of the array entry's fields is the name that I need. Is that possible ? It is similar to the question
dynamodb - scan items by value inside array
but in my case condition has to be on one of the fields of the structure:
FilterExpression: "contains(#user, :v)",
ExpressionAttributeNames: { "#users": "user" },
ExpressionAttributeValues: { ":v": "John" }
Thanks.