filter items by relational count in directus - directus

I'm playing around with Directus and wanted to filter my items by counting O2M relations.
I tried using has, but as described in docs it counts "at least n items", but I want to retrieve all items with a relation count of 0. How can I achieve this?
Having two tables:
My current request is like this:
https://directus.localhost/_/items/products?meta=total_count,result_count&limit=100&offset=0&fields=id,suppliers.*&filter[suppliers][has]=0
But I get all products, since all do have at least 0 suppliers. How to filter exactly zero suppliers?
Thanks for any advice!

Related

SQL query for the number of cases when a value of column1 (non-unique) can't be found within any record where column2 meets a basic criteria

I am doing a beginners' SQL tutorial and I started to wonder whether a simple SQL query on this table: http://www.sqlcourse2.com/items_ordered.html could tell the number of items (also 1) which have only been purchased more items at a time, so there is no record which contains the quantity column with a value of 1 AND the item. I am really beginner at this so please try to keep it simple.
Thank you in advance!
Welcome to the fascinating world of SQL.
Well - I'm not giving you the answer, but a hint (after all, it's a training and your own thinking and finding the solution would be the best way for you to learn something new).
The way you formulate your question is somewhat puzzling.
When I combine what you ask with what is possible with SQL, the question that would make sense to me would be that you need to list (or count, I did not understand that very well) the items (or the complete rows in the table with matching item, that was not clear either), that were never sold with a quantity of 1.
If that's what you need, you will need a subselect to get all distinct items that were sold with a quantity of 1, and select the rows from your base table whose item value is not in the list you get from the subselect.
Do you need more hints?
Marco

Solr/Lucene result field term count

I am using solr to do a search. As result I get back a set of fields. One of the fields is "domains". The domain field is a many to many relationship in my database, so my docs contain an array of "domains" the are linked to.
What I want to do is, for each domain in the resultset, count how many times this "domain term" is found in the global result set.
How should I do this ?
You need to look at the Field collapsing feature.

How do I make a shopping list from multiple recipes in an SQL database?

I'm working on using a recipe database in SQLw, like the one in this question (which has helped a lot already) Structuring a recipe database , to combine the ingredients of several user selected recipes to a shopping list.
Also, the items on this shopping list are to be divided in two categories (eg: "groceries" and "check pantry")
Example case:
User can select 7 recipes to make a weekly mealplan in a form (almost got this part)
The given output is a shopping list of all the ingredients marked as "groceries" and a "check stock" list of all the ingredients marked as "pantry".
Any help at all would be much appreciated!
I had just posted a full solution, but given the subject here looks like it may be homework, I'm just going to point you in the right direction. If this isn't homework, leave a comment and I'll put the full solution back.
Since you have multiple recipes, a normal selection based on joins would give you back multiple rows per ingredient. You want some way to roll up all of the rows for a given ingredient into a single row and show a total of the quantity that you need.

Solr facet counts are not correct, how to deduplicate

We are using two solrs to index the files. Sometimes one article is indexed in both solrs because we do update. It cause a problem that the facet counts are not correct due to these duplicated articles. How can I de-duplicate the counts?
My advise would be not to keep duplicated articles. So you need a method to identify this duplicates articles and deleted it form one SOLR.
If you don't want to delete duplicate articles you still need to keep track of them.
Knowing which articles from SOLR1 are duplicates in SOLR2 will help you de-duplicate the counts like this:
create an extra field in SOLR1 named :
IsDuplicateField = true, if article is duplicated in SOLR2
= false, otherwise
when you do the query to SOLR1 add: IsDuplicatedField=true to facets.
when retrieving result just decrease the total number of facet counts with total number of IsDuplicateField from SOLR1.
In this situation the facet IsDuplicateField will retrieve all the articles that are duplicated and match your query.
Good luck !

Designing database

I am finding it difficult to decide on an efficient design of the database. My application would get a number of ingredients(table) from the user and check with the database to find the recipe that could be prepared from the list of ingredients that the user provides.
My initial design is
Useringredients(ing_id,ing_name..);
the recipe database would be
recipe(rec_id,rec_text,...);
items_needed(rec_id,item_id,...);
items(item_id,item_name);
Is this a good way ? If so how will i be able to query to retrieve the recipes from the list of user ingredients.
Help would be very much appreciated.
This design could work. You have one table recording recipes, one recording items and one recording the many-to-many relationship between the two (though I would work on your naming conventions to keep things consistent).
To get any recipes that contain at least one item in your list, you could use the following:
Select rec.rec_id,
Count(itn.item_id) as [NumMatches]
From recipe as rec
Join items_needed as itn on itn.rec_id = rec.rec_id
Where itn.item_id in (comma-delimited-list-of-itemIDs)
Group By rec.rec_ID
Having Count(itn.item_id) > 0
Order By Count(itn.item_id) desc
This returns any recipes that contain at least some of the items that are selected, sorted with the first recipes having the highest number of matches.
The following query should give you a list of unique recipes using any one of the ingredients the user searches for
select distinct rec_id,rec_text,ii.item_name
from recipe rr
join items_needed itn on itn.rec_id=rr.rec_id
join items ii on ii.item_id=itn.item_id
join userIngredients ui on ui.ing_id=ii.item_id
Yaakov's query looks like it will handle the situation where you want all ingredients. You might be able to replace (comma-delimited-list-of-itemIDS) with (select ing_id from userIngredients)