Elasticsearch (round-robin sorting) - sorting

Consider the follow list of products, at the moment these are sorted by name, this gives an unfair result which could lead to the following output (Giving 1 company the top of results, based on their 'fortunate' productnaming only)
Company x Product 1
Company x Product 2
Company x Product 3
Company y Product a
Company z Product a
Company y Product b
Company z Product b
Company y Product c
Company z Product c
How can I sort this using elasticsearch so that it comes out in "round robin" order, that is, select each unique item once before repeats. So the above list would come out like this:
Company x Product 1
Company y Product a
Company z Product a
Company x Product 2
Company y Product b
Company z Product b
Company x Product 3
Company y Product c
Company z Product c
I have found this question asked for LINQ : LINQ order by "round robin"

Unless you find some kind of script solution, your best bet is to perform several elasticsearch searches and merge the results manually. You can at least perform all the searches in a single request using the Multi Search API.

Related

Calculate percentage when an item can be assigned to multiple categories

this might be a stupid question but I'm blanking at the moment and can't find the answer in google.
I have the following example table
customer
bought
A
food
B
food,drink
C
drink
D
drink
now how do I calculate the percentage of customers that bought food/drink over total customers? what would be the best way to calculate this?
solution
% food = #customers who bought food / #total unique customers = 2 / 4 = 50%
% drink = #customers who bought drink / #total unique customers = 3 / 4 = 75%
the problem here is the total % exceeds 100%
solution - count customer B twice, once for drink and once for food
% food = #customers who bought food / #total customers = 2 / 5 = 40%
% drink = #customers who bought drink / #total customers = 3 / 5 = 60%
the total is 100% but is this the correct way to calc % in this case?
The issue is obviously that one customer can buy both food and drink and I'm not sure how to handle this case. Any help would be appreciated. Thank you!
UPDATE:
thanks for the answer. It makes sense to remove altogether the customers who bought both products. But now I'm wondering what happens if there are more than 2 categories?
Example as follows, now we have an additional product (ice cream) in the mix
customer
bought
A
food
B
food,drink
C
drink
D
drink
E
drink,ice cream
F
ice cream
G
food,drink,ice cream
I guess with the same logic we can remove customer G since they bought all the products? And how should we handle customer E and B?
I think that what you are looking for is simply to eliminate the number of customers that purchased both products.
If you are looking for unique combinations of customers who purchased only a specific product then:
3 unique customers bought a single unique item
1/3 food => 33%
2/3 drink => 66%
The ones who bought both products, don't matter in these calculations since they add the same percentage to both cases.
EDIT:
I used excel to help me out. I hope that is fine
I added your data into an excel sheet and created a crosstable pivot.
I've set the Customers as columns and the Products as rows, in order to see what each individual customer has purchased via the values field Count of Product
I've changed the count of Product into the formula for % of Total
in order to show me for each product, the percentage split between the customers, so for example if customer B bought both drink and food he will be shown as 50% in both corresponding rows. If he bought all 3 then 33%
The grand total column, will contains the final percentage for each product row item. Excel calculates it the same way as states in some comments, it counts the products total instead of the customers total, which makes sense since that is your data set being consumed, and not the customers themselves.
If we swap the rows and columns around and do the same calculations, we see individual percentages for each customer (how much % of product they each individually purchased) and we can sum them up for each product. The result is the same

What is the right eloquent relationship to use on sharing same product but different distributor?

There is a portal where manufacturer supplies to distributors and orders from dealers are verified by their own distributor. Assuming that "Distributor X" has 40 stock balance for "Shampoo Blue". Whenever dealer order SB, DX will just need to verify the payment. Once the payment verified, it will deduct stock balance from DX and manufacturer will pack and ship out.
I can't figure out how to deduct stock balance from it.
Dealar - Place order
Distributor - Verify order and deduct stock balance.
Manufacturer - Pack and ship.
I tried using Many to Many, Has One Through and also Poly Many to Many.
companies
- id integer
- name string
products
- id integer
- name string
- price decimal
orders
- id integer
- company_id integer
- timestamps datetime
- verified boolean
order_items
- id integer
- product_id integer
- order_id integer
inventories
- ??
- ??
- balance integer
etc
What is the schema to create an Inventory model and what is the relationships(if the relationships exists in Laravel) I should use or need something else to perform SQL queries to deduct balance of the particular product?
Many companies may have different products balance, whenever the dealer order it will deduct from the company balance.
Many to Many is the right relation to apply.
If your product can be from more than 1 distributor (1 to N) and one distributor can have more than one product (1 to N) then in conclusion you'll have N to N (Many to Many) relation.

rasa-core Map entities to other entites

I am building a simple food ordering bot. In this I have a take_order intent in which two entities will be extracted food_item and quantity, both of these entities have list types in slots, for example if a user message like this comes:
I would like to have [one] (quantity) [chicken burger] (food_item) and [two] (quantity) [fries] (food_item)
slot for this example will be: slot{“quantity”: [“one”, “two”], “food_item”: [“chicken burger”, “fries”]}
in the action user_take_order I will be multiplying quantity of each item to its price and giving total bill to the user.
But I have a problem, in a complex case when user do not provide a quantity for the food_item, I will be assuming the default quantity to one but the problem occurs when user orders three items and do not provide quantity for only the second item, for example:
I would like to have [one] (quantity) [chicken burger] (food_item), [fries] (food_item) and [two] (quantity) [soft drinks] (food_item)
in this example no quantity is provided for the fries and slots filled: slot{“quantity”: [“one”, “two”], “food_item”: [“chicken burger”, “fries”, “cold drink”]}
in the action user_take_order I would like to do this:
1 x price_of_chicken_burger
1 x price_of_fries
2 x price_of_cold_drink
but the problem is that in quantity slot I have only quantities of chicken burger and cold drink and I do not have a clue that user did not mention the quantity of fries( I want to set quantity of fries as 1 “default case”)
have I choose the wrong types for slots quantity and food_item?
slots:
food_item:
type: list
quantity:
type: list
One of the possible solutions is to extract quantity and food item as one entity:
I would like to have [one chicken burger] (quantity_food_item), [fries] (quantity_food_item) and [two soft drinks] (quantity_food_item)
then differentiate them inside an action.

How to get multiple to One Relation in Oracle Sql Query

I have product code defined in a table A where i will list 3 products with quantity. If customer purchase those 3 items I will be offering 1 product free.
Product Code
Item A
Item B
Item C
For these items I will be offering "Item D" as free.
How do I write a query to check, whether all the 3 items are available in current table?
Unfortunately you aren’t giving us enough information to go ahead and help you. From what you said this is what I am thinking. You have a table such as this:
You want a select that checks if all 3 items are available in the current table:
SELECT CASE WHEN COUNT (PRODUCT_CODE) = 3 THEN
'YOU GET FREE ITEM D'
ELSE
'YOU DONT GET ANYTHING'
END AS ANSWER
FROM TABLE_A
WHERE (PRODUCT_CODE = PRODUCT_CODE_1 AND QUANTITY > 0)
OR (PRODUCT_CODE = PRODUCT_CODE_2 AND QUANTITY > 0)
OR (PRODUCT_CODE = PRODUCT_CODE_3 AND QUANTITY > 0)
So basically the where clause is check if those 3 items exists and there quantity is greater than zero. Then I wrote a Case statement (you can use decode if you prefer) to check if the count is 3 then give me my free item else don’t give me.

SQL to Relational Algebra

How do I go about writing the relational algebra for this SQL query?
Select patient.name,
patient.ward,
medicine.name,
prescription.quantity,
prescription.frequency
From patient, medicine, prescription
Where prescription.frequency = "3perday"
AND prescription.end-date="08-06-2010"
AND canceled = "Y"
Relations...
prescription
prescription-ref
patient-ref
medicine-ref
quantity
frequency
end-date
cancelled (Y/N))
medicine
medicine-ref
name
patient
Patient-ref
name
ward
I will just point you out the operators you should use
Projection (π)
π(a1,...,an): The result is defined as the set that is obtained when all tuples in R are restricted to the set {a1,...,an}.
For example π(name) on your patient table would be the same as SELECT name FROM patient
Selection (σ)
σ(condition): Selects all those tuples in R for which condition holds.
For example σ(frequency = "1perweek") on your prescription table would be the same as SELECT * FROM prescription WHERE frequency = "1perweek"
Cross product(X)
R X S: The result is the cross product between R and S.
For example patient X prescription would be SELECT * FROM patient,prescription
You can combine these operands to solve your exercise. Try posting your attempt if you have any issues.
Note: I did not include the natural join as there are no joins. The cross product should be enough for this exercise.
An example would be something like the following. This is only if you accidentally left out the joins between patient, medicine, and prescription. If not, you will be looking for cross product (which seems like a bad idea in this case...) as mentioned by Lombo. I gave example joins that may fit your tables marked as "???". If you could include the layout of your tables that would be helpful.
I also assume that canceled comes from prescription since it is not prefixed.
Edit: If you need it in standard RA form, it's pretty easy to get from a diagram.
alt text http://img532.imageshack.us/img532/8589/diagram1b.jpg

Resources