Searching for multiple prefixes inside a column through Eloquent using RegEx - laravel

I'm trying to build a simple filtering tool, which allows users to filter profiles through different US states (Arizona, Alabama, and so on)
So I'm using the state's 2-character name in order to identify them internally.
A profile has a field called "linear_coverage", which is an imploded string of State + County where the profile is enabled.
For example, a profile could have the following "linear_coverage"
NY_Cattaraugus,NY_Chautauqua,NY_Erie,NY_Genesee,NY_Niagara,NY_Wyoming,NY_Cattaraugus,NY_Chautauqua,NY_Erie,NY_Genesee,NY_Niagara,NY_Wyoming,NY_Erie,NY_Niagara,NY_Erie,NY_Niagara,AK_Kodiak Island,AK_Skagway
You can see this person has New York (NY) and multiple counties selected, but it also has Alaska (AK).
So I've been trying to use the following:
foreach($states as $state)
$query->orWhere('linear_coverages', 'LIKE', '%' . strtoupper($state) . '_%');
//$query->orWhere('linear_coverages', 'regexp', strtoupper($state) . "_[A-z]+");
The second comment line was a test I ran, without having success.
I'm thinking, is there anyway for a RegEx to search for MULTIPLE prefixes inside a string?
So I could be search for [NY_,AK_] and this would return me the profile I just showed, and the other which meet the criteria.
Thanks a lot.
M.

Finally found out to how to achieve this
I replaced my Regexp by the following
/(NY|AZ)_[A-z]+/
By enclosing the different states and separating them with | symbol, the Regex will try to find any of these prefixes, and then the remaining string is valid.
I was able to filter down all my results properly. :D

Related

Laravel like query exact match

I'm uploading a CSV file to my database and I want to automatically add a projecttype_id to a project, given this two project types:
Roads, new construction, widening
Roads, new construction
when I tried to upload "Roads, new construction" it always gives me the first id.
Tried removing % but still the same:
$projecttypeid = ProjectType::where('name', 'LIKE', "%{$projecttype}%")->first('id');
No need for LIKE if you want an exact match. where('name', $projecttype)

How to use .filter() to return a match inside of an array (rethinkdb)

I want to find out how many of my students are being taught by a specific teacher. However some of the students have multiple teachers. The value of the teacher_name entry is represented as an array. The following query only shows those results where the match is exact, that is, it won't show me results where there are multiple teachers.
r.db('client').table('basic_info').filter({teacher_name: ["Andrew McSwain"]});
How can I iterate through the arrays to match if it contains the string I specified? Is there an API command for this or can I use good-ole javascript methods?
You probably want something like:
r.db('client').table('basic_info').filter(function(row) {
return row('teacher_name').contains('Andrew McSwain');
})

How to use the PubMed API to search for an article with an exact title?

I'm trying to use the PubMed API to search for articles with an exact title. As an example, I want to search for the title: The cost-effectiveness of mirtazapine versus paroxetine in treating people with depression in primary care.
I want up to 1000 results in JSON format, so I know that the first part of my URL should look like this:
http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmode=json&retmax=1000&term=
How do I add a title search as a GET parameter?
I've been using the Pubmed advanced search constructor, and that suggests that the query should look like The cost-effectiveness of mirtazapine versus paroxetine in treating people with depression in primary care[Title].
But if I try just adding that to the URL term=, PubMed tries to break down the title into all kinds of peculiar queries:
http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmode=json&retmax=1000&term=The%20cost-effectiveness%20of%20mirtazapine%20versus%20paroxetine%20in%20treating%20people%20with%20depression%20in%20primary%20care[Title]
How can I specify an exact title as a GET param?
Use field=title
http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmode=json&retmax=1000&term=The%20cost-effectiveness%20of%20mirtazapine%20versus%20paroxetine%20in%20treating%20people%20with%20depression%20in%20primary%20care&field=title
Check out ESearch API for more information:
http://www.ncbi.nlm.nih.gov/books/NBK25499/#_chapter4_ESearch_
Use + instead of %20 (space).
For example:
http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi?db=pubmed&retmode=json&retmax=1000&term=cost+effectiveness+of+mirtazapine[title]

OData v2 filter by property of $expanded entity

I am using $expand to enhance an OData SharePoint REST query, and I would like to $filter on one of the properties of the expanded entity. However, I can't find any documentation on the correct syntax for this. I've found a few spots that might suggest using Entity/property, but after trying that, I fail with:
Query:
_vti_bin/listdata.svc/Posts?$expand=Category&$filter=substring(\"Featured Article\",Category/Title) eq false and year(Published) lt " +year+1+ " and month(Published) lt " +month+1+ " or day(Published) lt " +day+1+ " and ApprovalStatus eq '0'&$select=Title,Published,Category,ApprovalStatus&$orderby=Published desc"
Which returns:
syntax error '\"' at position 10.
How would I filter based on the Title of the Category entity, when Category is a high level, and it's Title property is a sub-level?
The filter on navigation entities is independent of the expand.
If the navigation property is a not collection (1:1 relation)
You can filter with type/property, here is an example to search for all orders that are made by customers with some id:
http://services.odata.org/V2/Northwind/Northwind.svc/Orders?$filter=Customer/CustomerID%20eq%20%27ANATR%27
If the navigation property is a collection (1:N relation):
OData Version 2: when the expanded entity is an entity collection, you cannot search on both the expanded and the target entity. For example you cannot do that query http://services.odata.org/V4/Northwind/Northwind.svc/Customers?$expand=Orders&$filter=Orders/ShipCity%20eq%20%27Berlin%27
A work around might be to flip the query (to selecting the orders instead of customers - each order has 1 cutomer):
http://services.odata.org/V2/Northwind/Northwind.svc/Orders?$filter=ShipCity%20eq%20%27Berlin%27&$expand=Customer
but then you could get duplicate customers (your actual target entity).
OData Version 3 You might consider to use a custom query option (http://www.odata.org/documentation/odata-version-3-0/url-conventions/).
OData Version 4: you can do a filter with the any construct:
http://services.odata.org/V4/Northwind/Northwind.svc/Customers?$filter=Country%20eq%20%27Germany%27%20and%20Orders/any(o:o/ShipCity%20eq%20%27Berlin%27)
It looks like the problem is the slash-escaped double quote and that you're using substring instead of substringof.
Does it work if you use single quotes instead? Or did you want to literally match the double-quote character? (If that's the case, I think you can just leave the double quote unescaped inside the single quotes)
_vti_bin/listdata.svc/Posts?$expand=Category&$filter=substringof('Featured Article',Category/Title) eq false
or
_vti_bin/listdata.svc/Posts?$expand=Category&$filter=substringof('"Featured Article"',Category/Title) eq false
As an example on a public service, take a look at this query:
http://services.odata.org/Northwind/Northwind.svc/Products?$filter=substringof('Bev', Category/CategoryName) eq true
As far as documentation for the OData query syntax, I recommend taking a look at this page: http://www.odata.org/documentation/odata-v3-documentation/url-conventions
In the filter section there are a fair number of examples you can use for guidance.

Dynamic number of evaluations on a where (lambda)

I'm new to Entity Framework, LINQ and lambda expressions. I need to do a search over a user's table, and I need to emulate one that already exists on a desktop application. This search gives only 1 text field, and then it takes anything that you put in it and creates a "contains" query on different users' attributes like name, last name, username, etc. The most important part is that if you put two or more words in then the search makes the query search for different words.
Example:
If I search for "ju pe" I will get "Perez, Juan", this works by the identification of blank spaces.
Using regular SQL I can build a regular string containing the query, but how can I do that using lambda?
In other words, how can I get a dynamic number of "evaluations" on the .where()? Like .Where(EV1 || EV2 || EVn)
There is neat library called LinqKit which contains predicatebuilder
Sample code:
var predicate = PredicateBuilder.True <User> ();
predicate = predicate.Or(x => x.Name.Contains("ju"));
predicate = predicate.Or(x => x.Name.Contains("pe"));
// etc ...
Users.AsExpandable().Where(predicate);
You can and/or as much as you like.
You can do this using simple Linq by building a complex Where predicate, if I understand you correctly. Please let me know if I misunderstood you! Let's say you have a string searchTermInput and users, an IEnumerable<User> for some type User with a Name string property. Then you can write:
users.Where(user => searchTermInput.Split(' ')
.All(searchTerm => user.Name.Contains(searchTerm)));
In other words: for each user, check if that user's name contains every one of the search terms and filter it out if it does not.

Resources