How Could I compare two fields in an strapi entity - strapi

For example, I have an entity with two fields:
user (following, followers)
I want to search on users and find all users that following is more than followers. I use this SQL query:
select * from user where following > followers
How could I do this search in Strapi?

Related

How can I filter records of a DirectUS collection based on values on a many-to-one relationship?

I am using DirectUS version 8.3.1
I have a many-to-one relationship between user_membership and user.
The table user_membership is where we store the years of membership for a user and then we have several lines for a user, one per year of membership.
I have configured the interface with a many-to-one relationship and I have set the template {{name}} - {{email}} for the user.
I can easily add, update or remove lines into the table user_membership. The table has more than 20000 lines and I need to filter them. From DirectUS, how can I find for instance all the lines for user "John - john#smith.com" among my 20000 lines?
Is the solution to add a one-to-many relationship into table user and to go through the table user to search for all memberships a good solution? Will DirectUS add something into my original database schema if I create this user to user_membership relationship?
Many many thanks
Dominique

Getting Data together for program and campaigns from three tables

I'm facing the following problem.
I have a list of Back in Stock requests. Each request gets its own RIID and the email is hashed. I also have the product id of the product.
What I then created is a SQL view that pulls the contact data from the profile list (contact list) and the product data I need from a SUP feed. This SUP feed is not connected to the contact list via RIID or similar.
My problem then is how to get the data from all 3 lists into a mail for personalization. I can't access the data from the SQL View, can I? The Back in Stock list is not provided with email addresses and the product list is disproportionate to the contacts. Does anyone have an idea?
Here are examples:
BIS.List fields:
Email_Hash, RIID_, PRODUCTID
CONTACT.List fields:
Email, Email_Hash
Product_Feed fields:
PRODUCTID, PRODUCT_IMAGE
SQL View RESULT fields:
Email, RIID, PRODUCTID
In the Campaign Dashboard, open the Data Sources window. Add all the supplemental tables you need (remember to assign lookup fields and aliases to each table).
In your Campaign source, use the #data command (RPL) to fetch the fields and filter the results from each of the suplemental tables.
RPL Documentation: RPL_Reference_Guide.pdf

Laravel/Lumen include a column from pivot table into results of relationship eager loading

In my application a user can add other users to the "favorites" list. All information about favorite users is stored in a table with the following structure:
user_id
favorite_id
In addition, each user has its services with prices. There are users and services tables in my application that are related to each other with service_user table which has the following structure:
user_id
service_id
price
In order to get all favorite users of a particular user with their services and prices I do the following:
$user->favorites()->with('services')->get();
However, there's no price column in each service object of the resulting list of services (this column is located in pivot).
Therefore, I want to know if there's a way to get the price column as part of each service object, instead of being in pivot table. The following question already solves my problem, but I want to know if there is way to do this without map() and/or foreach() to make performance better?
Currently, I have the following solution:
$user->favorites()->with('services')->get()
->map(function ($favorite) {
foreach ($favorite['services'] as &$item) {
$item['price'] = $item->pivot->price;
}
return $favorite;
});

Elasticsearch custom score function

Lets say I have a the following tables:
products: id, name, latitude and longitude
users: id, name, latitude, longitude
interests: id, name
user_interests: user_id, interest_id
These gets inserted/updated into elasticsearch whenever they are created/edited using model observers.
Now I want to make a custom script/scoring function in elastic search which will return me a matching score when a user is searching for a product. The scoring will be based on distance between users location and product, user interests and product name match etc etc.
Being new to elastic search, whats the right approach of implementing this? Any tutorial, online resources or examples are highly appreciated.
PS: I am using PostgreSQL as a database. I can create a function there called get_match(product_id, user_id) which returns a number (0-100) based on matching criteria and do something like:
//psuedo sql
select p.name, get_match(p.id, u.id) as match
from products p, users u
order by match desc
I want to achieve a similar functionality in elasticsearch if possible.

Grab all documents whose IDs are not present in a separate table

I have a users table and a tasks table. In tasks, each document consists of a user_id field (which is an ID from the users table) and some other, irrelevant fields.
I would like to filter users after some criteria (.filter({'field': 'value'})), then get only those users that are NOT in the tasks table (that user_id field).
I started the query: r.table('users').filter({'field': 'value'}) but I don't quite have a clue on how to write that "user shouldn't be found in tasks table".
You can nest ReQL expression (including querying another table inside a method (like filter).
You want to use some indexes here to make things faster.
r.table('tasks').createIndex('user_id').run()
r.table('users').filter(...).filter(function(user) {
return r.table('users').getAll(user('id'), {index: 'user_id'}).isEmpty()
})

Resources