Flask-Restless case-insensitive query - flask-restless

How to make case-insensitive search query using Flask-Restless?
Example:
api/user?q={"filters":[{"name":"lastname","op":"like","val":"%davidson%"}]}
I would like the previous code to return "davidson", "Davidson", "DAVIDSON"...
But it only returns "davidson".

You can use the ilike operator like so:
api/user?q={"filters":[{"name":"lastname","op":"ilike","val":"davidson"}]}
This is not really documented in the Flask-Restless docs, but the operator is documented in SQLAlchemy which is the OR mapper Flask-Restless uses under the hood. See the relevant docs here or this answer.

Related

Minus flag on list filter

Working on some code and saw this:
<#list entity.fields(f- !f.isPrimaryKey) as field>
I know it is some kind of filter but I don't understand the f- flag. Looking on the documentation I did not see anything regarding that.
Ignoring the typo, you can use lambda
The usage of lambdas is restricted to the parameters of certain built-ins, like: filter, map, take_while, drop_while.
For example as #ddekany comment
<#list entity.fields?filter(f -> !f.primaryKey) as field>
Where you are filtering fields list with non primary keys

Pythons Elasticsearch-DSL filter for exactly one match from list of values

I saw some realted posts but none of them match my exact issue.
Using Python 2.7 with Elasticsearch-dsl (6.3, that is also my Elasticsearch version).
I want to do something like,
s = Search(using=elastic_conn, index='my_index').filter("match", service_name=['exmp_name1', 'exmp_name2'])
This syntax doesn't work though.
I wish to get back all documents with service_name == 'exmp_name1' OR service_name == 'exmp_name2'
I prefer to use the filter context rather then query context as from my understanding it's faster and scoring really isn't important to me, just an absolute match (or mismatch).
How can I achieve this behavior?
Thanks
Ok. All I needed is to filter by terms rather then match.
The terms syntax supports several values.
Working code:
s = Search(using=elastic_conn, index='audit').filter("terms", service_name=['exmp_name1', 'exmp_name2'])

Google Cloud, compute.instances.aggregatedList with filter fails

The google cloud API for compute.instances.aggregatedList includes filter argument.
https://cloud.google.com/compute/docs/reference/rest/beta/instances/aggregatedList
I use (status eq "RUNNING") as a filter to view all my running instances.
I would like to have a more elaborate criteria, such as one that uses labels and or other terms, however even the Google documentation terms (that use OR operator) returns an error, For example - even Google documentation example:
(cpuPlatform = "Intel Skylake") OR (cpuPlatform = "Intel Broadwell")
fails with error 400:
"message": "Invalid value for field 'filter': ' (cpuPlatform = \"Intel
Skylake\") OR (cpuPlatform = \"Intel Broadwell\")'. Invalid list
filter expression."
it looks as if the '=' signs are not accepted, and AND/OR operators are not accepted.
What is the correct format for this API.
I had the same issue when I used "=" instead of "eq" in google-api-python-client. I required to use labels to filter the aggregated instances list. At first I used
filter="labels.projectid=test-project"
which returned 400 error in aggregated list but was successful if it was queried for instances of specific zone.
I achieved the list successfully when I used filter as
filter="labels.projectid eq \"test-project\""
or
filter = "labels.projectid eq test-project"
I even tested it using REST-Client provided by google and it worked.
Reference: https://issuetracker.google.com/80238913
Even 3 years later Google haven't fixed the bug: OR and AND operators are not supported even it is advertised:
https://cloud.google.com/compute/docs/reference/rest/v1/instances/aggregatedList
Google API is famous for inconsistency and false promises. Just relax and do 2 queries to emulate OR.
For AND operator just omit AND and quote comparison expressions into parentheses:
(name eq 'stage-.*') (labels.env ne "SKIP")
Note I use eq / ne with regex instead of operators =, !=, :.
I ran into a similar error message calling a GCP API. I finally got it to work by making the filter look like this:
fmt.Sprintf("selfLink = \"%s\"", networkLink)
Compute instances list api filter param should work with
(labels.<label_name_1>=<label_value_1>) OR (labels.<label_name_2>=<label_value_2>)
I have found that this string works as a filter within Python:
test_filter = '((labels.test="test-value") AND (labels.test-second="test-second-value")) OR ((labels.test="test-other-value"))'
This filter worked for me:
name eq my-service-v.*
Will return groups like my-service-v112 etc' (even though the name field is nested inside).

How to fetch multiple cq pages using Xpath based on page's property

I have two cq page and I want to retrive these two using jcr:title property using XPATH. The below query is working fine for single.
/jcr:root/content/test//element(*, cq:Page)[((jcr:like(jcr:content/#jcr:title, dell)))]
But I want to excuate it for multiple items. I have tried with following option but it is not working
/jcr:root/content/test//element(*, cq:Page)[((jcr:like(jcr:content/#jcr:title, [dell,samusng])))]
Could anyone help me to write xpath query?
As rakhi4110 already mentioned in the comment, you can combine multiple where clauses with an or just like in SQL. Though I think you either want exact matches or use jcr:containts instead of jcr:like.
Exact match:
/jcr:root/content/test//element(*, cq:Page) [jcr:content/#jcr:title='dell' or jcr:content/#jcr:title='samsung']
Contains:
/jcr:root/content/test//element(*, cq:Page) [jcr:contains(jcr:content/#jcr:title, 'dell') or jcr:contains(jcr:content/#jcr:title, 'samsung')]
Or if you really want to use jcr:like which, as in SQL, uses % for wildcard:
/jcr:root/content/test//element(*, cq:Page) [jcr:like(jcr:content/#jcr:title, '%dell%') or jcr:like(jcr:content/#jcr:title, '%samsung%')]

Case insensitive Contains with Dynamic Linq

When using Contains with Dynamic Linq on Linq-to-objects, the search is case sensitive. I would like to be able to search case insensitive (like Linq-to-sql, cause SQL server does this by default).
Something like:
this.someQuery = this.someQuery.Where(field + ".Contains(#0, true)", strValue);
where true means: caseinsensitive = true, like one of the extensions of System.String.Contains provides. Though i cannot use extensions to System.String with dynamic Linq by default.
Can you just .ToLower() both sides of the comparison? Something like this:
this.someQuery = this.someQuery.Where(field.ToLower().Contains(strValue.ToLower()));
Or did I misunderstand what you're looking for?
When using "toLower", make sure you include the () "ToLower()"
If you want to use placeholders then your code should look like
this.someQuery = this.someQuery.Where("field" + ".ToLower().Contains(#0.ToLower())", strValue);

Resources