How to model this mysql table relationship in elastic search - elasticsearch

I have a large amount of shop items imported into elastic search and I can query them.
I am wondering how best to model the following mysql table relationship into elastic search:
Shop items can have different offers. There are different offer types. And in some shops an item may be on offer, in other shops the item may not be on offer or have a different offer type. Items don't have to have offers. I model this below:
Items table
item_id
Offers table
shop_id, item_id, offer_type, user_id
Where user_id is the id of the user who created the offer.
So as an example, item_id 1 and shop_id's 1,2 and offer_types premium and featured.
Then the offers table could look like:
shop_id, item_id, offer_type, user_id
1,1,featured,45
2,1,premium,33
2,1,featured,45
But it's not the case that every item is on offer. And even if item_id 1 is on offer in shops 1 and 2, it might not be on offer in other shops.
I want to be able to query my /items type and it will only be for one shop at a time but for that shop I want to get all the items in e.g. a certain price range and of a certain category (that i can do all ready), but I need to know for each item in the results what offer they have if any (e.g. if featured, premium or whatever offer_type).
How can I best model this behaviour in elastic search?

One approach is Nested Object relationship - Shop contains set of items with id as your shop id
For your cases
1) Get all items of a shop - GET: http://host/your_index/shops_type/shopid
This will give you all items in a shop along with offer_type. you can filter in your program logic

Related

Laravel keep track of quantity for different models

I'm building a farm management application with Laravel with the following tables
products
id
trade_name
state
category_id
equipment
id
name
type
Product is a chemical product and Equipment can either be a tractor or an implement
I want to keep track of quantity in both products and tractors
For products, quantity can be in litres or kilograms (depends on the state of the product)
For tractors I want to keep track of quantity of fuel
For that I'm thinking of creating a new stocks table with the following columns
stocks
id
stockable_id
stockable_type
quantity
The thing is I only have one warehouse which means creating a stocks tables doesn't really make sense but if I choose not to do it I'll have to add a quantity columns for both products and equipment
I'm lost with which approach I'd go with and I'd be happy to hear more from you guys

laravel define relation over multiple tables

I have a table customers with the fields id, name and so on.
One table doctors with the fields id, name.
Then there is one table subject_areas which has all subject areas which a doctor can have. The fields are id, text.
So, each doctor can have multiple subject areas. There is one pivot table doctor_subject which is a belongsToMany relation.
Here is my problem: A customer can have multiple doctors, but only for a specific subject area. I tried it with a new table customer_doctor with the fields id, customer_id and doctor_subject_id. But how do i map this in Eloquent?
Issue was in relation between tables. After chat clarification this came out as solution:
Html form is written in a way that customer first choose doctor, then depending on selection choose several of his available areas.
In that scenario customer needn't to be related to areas directly and should be related to areas only over relation with doctor.
Also as side note, if needed deeper relations, models on pivot tables could be created and used as well.

Query and comparing 2 tables

i have a table that is stored all records of products, and than i have other table that stores all records that i bought in the table products.
In one of my pages i list all my products, but i will to list only the products that were not bought from the user. How can i query the listing of products to only show products that i didnt bought? How can i make this comparation or intersection in eloquent?
In my case the producst are information from users, or more specifically answers from Inquiries, the identifyer of the user is by email, i query by group of emails, and the email is unique for each survey.
ex:
$survey = $survey->answers->groupBy('email');
As you didn't provide any details of the tables, i will make some assumptions:
The product table has an id coloumn, the second table has some form of product id, to see what has been bought.
You should take a look at this:
DB::table('bought_stuff')->whereNotIn('email', [1, 2, 3])->get();
you can read about it here:
https://laravel.com/docs/5.3/queries#where-clauses under "whereNotIn".
It is a little difficult for me to answer, as i'm not completely sure about what you mean in the last part..

Most efficient way to model data in Elasticsearch

I have an example of modelling an commence site. Say that the site has few hundreds shops and few millions products. The products per shop range: 1000-100.000 products/shop. I need to be able to aggregate the products and the shop fields. All the products and all the shops have the same schema.
Product
{
"productName"
"price"
"category"
}
Shop
{
"shopName"
"rating"
}
1) Is it more efficient to have a) 1 index/shop, b) same index and 1 type/shop or c) same index, same type and have a field to determine the shop of the product?
I read some related articles and most of them are in favour of same index and 1 type/shop. But then they say that if there is one single index which has a large number of docs it might be even slower than having multiple indices.
2) I also need to perform JOINS and aggregations between the shops and the products. For example I need to be able to retrieve all the products from the shops with rating higher than 8/10 and also get the number of products per category. Is it preferable to use a) application-side JOIN, b) parent-child relationships, c) Siren plug-in, d) something else?
I would definitely use single denormalized index/type for the use cases you've mentioned. If you will need more fields for the shop, then you will create another index for shops, while still keeping first denormalized index. Mind that you may need unique shop id alongside to shop name.

Data model design for Parse

I have currently having 4 classes in Parse.
1) Rangoli - Main item objects
2) Categories - List of all categories
3) Users - List of all users
4) ItemCategories - List of categories that each items are assigned
Below is the structure of all classes. I am facing difficulties in queries when the item has multiple categories. I am getting duplicates.
Whats the best way of designing this data model which will allow us to query by category and get all columns.
For detailed issue on the query, please refer to Relational Query in Parse
You could make category a Relation or Array.
See:
documentation
You could make category in item categories a relation, or you could add a categories column in Rangoli and scrap the ItemCategories table.
edit-
You could make Categories a column in Rangoli. Now when you query for items, you also get all their categories, You could still use your other table to get all the items in a category, and that mapping a lot easier. Now you get both use cases

Resources