In OData search.in, if I leave the default delimiter of ', ' or ' ', no value is returned - azure-cognitive-search

I'm testing out the search api filtering and have a relatedTags parameter that is of type Collection(Edm.String) and when I perform the following filter:
relatedTags/any(rt: search.in(rt, 'Tag 3, Tag 2'))
which is equivalent to
relatedTags/any(rt: rt eq 'Tag 3' or rt eq 'Tag 2')
Only the latter will return results where the string array, relatedTags, has either of those values.
If I set the delimiter to '|' or ',':
relatedTags/any(rt: search.in(rt, 'Tag 3|Tag 2', '|'))
will it return the same as the eq example above.
Even specifying the delimiter as ' ,' or ' ' won't return results.
relatedTags/any(rt: search.in(rt, 'Tag 3, Tag 2', ' ,'))
The search.in OData language ref page has an example that is the same as what I've written in the first example:
Rooms/any(room: room/Tags/any(tag: search.in(tag, 'wifi, tub')))

Your query is not the same as the example because the values in your comma-separated list have spaces inside them. You need to set the delimiter to just comma, not space or comma:
relatedTags/any(rt: search.in(rt, 'Tag 3,Tag 2', ','))
You will likely also need to trim whitespace around your tags in order for this to work.

Related

Sub string replacement in Snowflake SQL

My need is to replace, format a string to make it match to the key.
-- Replace symbols ( ) , with no space,
-- replace single space with underscore,
-- replace BAND with BD
e.g. x_input = Higher Education Worker Level 10, Band 2 (salaried)
x_output = HEW_HIGHER_EDUCATION_WORKER_LEVEL_10_BD_2_SALARIED
I have written the code with nested replace which gives the correct output pattern
select 'Higher Education Worker Level 10, Band 2 (salaried)' as class_0,
replace(replace(replace(upper('Higher Education Worker Level 10, Band 2 (salaried)'), '(', ''), ')', ''), ' ', '_') as class_1,
replace(class_1, ',', '') as class_2,
replace(class_2, 'Band', 'BD') as class_4
Is there a more elegant way to do this, i was reading through the snowflake regex pattern matching help, but was not able to find a cleaner way and it too nested couple of iterations.
Any hint would be appreciated.
Thanks
For the one character replacement and removal you can use translate(), which will shorten the multiple replace() by a lot.
https://docs.snowflake.com/en/sql-reference/functions/translate.html
Query with identical results from the question, but way less code:
select 'Higher Education Worker Level 10, Band 2 (salaried)' as class_0,
translate(upper(class_0), ' ()', '_') as class_1,
replace(class_1, ',', '') as class_2,
replace(class_2, 'Band', 'BD') as class_4;
In one step:
select replace(translate(upper(class_0), ' (),', '_'), 'Band', 'BD') class_4
from (
select 'Higher Education Worker Level 10, Band 2 (salaried)' class_0
)

Laravel concat query return 0 items

Here is my controller code:
if ($request->exists('tipo')) {
$valor = $request->value;
$candidates = Candidate::buscarpor($tipo, $valor)
->orderBy('id', 'desc')
->Paginate(5)
->withQueryString();
dd($candidates);
}
And this is the scope "buscarpor" inside my "Candidate" model:
public function scopeBuscarpor($query, $tipo, $valor)
{
if(($tipo) && ($valor)) {
if($tipo == 'names') {
// return $query->orWhereRaw("concat(name, ' ', last_name) like '%".$valor."%' ");
return $query->where(DB::raw("CONCAT('name', ' ', 'last_name')"), 'like', '%'.$valor.'%')
->orWhere(DB::raw("CONCAT('last_name', ' ', 'name')"), 'like', '%'.$valor.'%');
}
return $query->where($tipo, 'like', "%$valor%");
}
}
When the search is of type "names" I should query in the DB to search a candidate/person by using his first name or last name, I only have one input type text, I just writting all his names is this input type text.
The variable $valor inside of this scope has data and no problem with it.. I tested adding a name that exists in my database but it returns 0 items.
This my dd($candidates) output.
I don't know what I'm doing wrong, please guys if you have some idea about how fix this problem, I will appreciate it.. Thanks so much.
Maybe the where expressions are not correctly formed. Try writing it like this
$query->where(DB::raw("CONCAT('name', ' ', 'last_name') like '%?%'", [$valor]))
->orWhere(DB::raw("CONCAT('last_name', ' ', 'name') like '%?%'", [$valor]));
You can also use the whereRaw() syntax instead
$query->whereRaw("CONCAT('name', ' ', 'last_name') like '%?%'", [$valor])
->orWhereRaw("CONCAT('last_name', ' ', 'name') like '%?%'", [$valor]);
Or maybe the names are written with capital letters in the database? If you are using postgreSQL, you can use ilike instead of like for a case insensitive search.
If you're using MySQL, you might have to use lower() in your raw methods.
$query->whereRaw("LOWER(CONCAT('name', ' ', 'last_name')) like '%?%'", [$valor])
->orWhereRaw("LOWER(CONCAT('last_name', ' ', 'name')) like '%?%'", [$valor]);

changing type of column to array Postgresql

I am trying to convert text column to array[text] column in table i have column entry like
['Nikolai Rimsky-Korsakov', 'Jascha Heifetz', 'Arpárd Sándor']
but this is one string or text format I want to convert it into a real array of a string so that I can access a particular name in the above column.
I tried converting the type from this link by setting it to type to text[] but the column is just becoming one element of an array like this.
[ "['Nikolai Rimsky-Korsakov', 'Jascha Heifetz', 'Arpárd Sándor']" ]
But what I wanted is to type Array[text] for tat column to able to access particular names.
Use the function translate() to replace square brackets with curly ones and remove single-quotes:
translate(str, '[]''', '{}')::text[]
See the full example in Db<>fiddle.
Section 8.15.2. Array Value Input of PostgreSQL documentation describes the general look of array to be
'{ val1 delim val2 delim ... }'
So you need to trim your '[' and ']' characters and replace them with '{' and '}'.
Then you can cast to text array (text[]) and enjoy the results.
SELECT
replace(
replace(
'{'||trim(BOTH '[]' FROM test.sample)||'}',
'\',
'\\'
),
'"',
'\"'
)::text[] AS names
FROM
(
SELECT '[''Nikolai Rimsky-Korsakov'', ''Jascha Heifetz'', ''Arpárd Sándor'', ''Joe "Wingy" Manone'']'::text AS sample
) test
EDIT 2
To handle cases when there " and '' characters in your input we must escape it with \.
SELECT
replace(
replace(
'{'||trim(BOTH '[]' FROM test.sample)||'}',
'\',
'\\'
),
'"',
'\"'
)::text[] AS names
FROM
(
SELECT '[''Nikolai Rimsky-Korsakov'', ''Jascha Heifetz'', ''Arpárd Sándor'', ''Joe "Wingy" Manone'']'::text AS sample
) test
EDIT 3
To remove quotes from names:
SELECT
replace(
replace(
replace(
'{'||trim(BOTH '[]''' FROM test.sample)||'}',
'\', '\\' ),
'"', '\"'),
''', ''', ',')::text[] AS names
FROM
(
SELECT '[''Nikolai Rimsky-Korsakov'', ''Jascha Heifetz'', ''Arpárd Sándor'', ''Joe "Wingy" Manone'']'::text AS sample
) test
The problem solved by removing nested square bracket from my CSV file before populating table and that using translate function with little bit of change thank
alter artist type text[] using translate(artist, '[]"', '{}')::text[] ; ```

having issues with single quotes using like 'string token' in execute immediate statement

Here is the section of code, I am using ' ' blah ' ' to escape single quotes but I guess its not working:
declare
my_func varchar2(20) :='test_func';
begin
execute immediate 'insert into TABLE_TEST (OUTPUT) select ' || my_func || ' from dual where TABLE_TEST.FUNCTION_NAME like ' 'VALIDATION1_%' ' ';
end;
I am getting the following error:
PLS-00103: Encountered the symbol "VALIDATION1_%" when expecting one of the following:
& = - + ; < / > at in is mod remainder not rem return
returning <an exponent (**)> <> or != or ~= >= <= <> and or
like like2 like4 likec between into using || bulk member
submultiset
The symbol "* was inserted before "VALIDATION1_%" to continue.
It looks like you are trying to escape the single quote with another single quote (which is good), but there is an extra space in between the two. That has to go.
Change
' from dual where TABLE_TEST.FUNCTION_NAME like ' 'VALIDATION1_%' ' '
to
' from dual where TABLE_TEST.FUNCTION_NAME like ''VALIDATION1_%'' '
In cases like this it's much simpler to use the new q syntax for literal strings, e.g.:
execute immediate 'insert into TABLE_TEST (OUTPUT) select ' || my_func ||
q[' from dual where TABLE_TEST.FUNCTION_NAME like 'VALIDATION1_%' ]';

Matching word when not followed by number or space+number

I wanna match "on" as long as it's not followed by a number or a space+number - without using negative lookaheads (because as far as I can tell, C doesn't support this - please correct me if I'm wrong).
Thanks!
This works:
#include <sys/types.h>
#include <regex.h>
regex_t re;
/* regcomp(&re, "on([^ ]| [^[:digit:]])", REG_EXTENDED); */ // thanks sln :)
regcomp(&re, "on ?[^ 0-9]", REG_EXTENDED);
If you want to write a matcher in C but without regex, then you might want to take a look at isspace(), isdigit() and isalpha() in ctype.h
You want to match a string that has your conditions in it?
edit - didn't read ? very well
edit - Ok, here ya go buddy
/on([^\d ]|[ ](\D|$)|$)/
Test case:
use strict; use warnings;
my #samps = (
' on',
' on9',
' on ',
' on 5',
' on 7',
' on p',
' on6 ',
);
for (#samps) {
if ( /on([^\d ]|[ ](\D|$)|$)/ ) {
print "matched '$_'\n";
}
else {
print "'$_' failed\n";
}
}
Output
matched ' on'
' on9' failed
matched ' on '
' on 5' failed
matched ' on 7'
matched ' on p'
' on6 ' failed

Resources