I have following application.
Product (document of _type : product) belongs to Category (document of
_type : category) . User (document of _type : user) can own these
products under any categories. 1 product can be under more than one
category. couple of ways of storing the product/category in user doc
Requirement
When consumer type keywords (they could type products or categories) in an
open text field, I need to find users who own these products/categories
(based on the keywords typed)
Model structure
Product
{
" _id" : "p100",
"_type" : "product",
"name" : "camera"
"categories" :["c100","c110"]
}
Category
{
" _id" : "c100",
"_type" : "category",
"name" : "electronics"
}
{
" _id" : "c110",
"_type" : "category",
"name" : "baby"
}
approach 1 for user doc
Storing in denormalized form product , category name itself in the
{
" _id" : "<user_id>",
"name" : "John Smith",
"product_categories" :[
{
"product" : "tv",
"category" : "electronics"
},
{
"product" : "camera",
"category" : "electronics"
}
]
}
{
" _id" : "<user_id>",
"name" : "Jane Smith",
"product_categories" :[
{
"product" : "camera",
"category" : "baby"
}
]
}
approach 2 for user doc
Storing in normalized form with product id , category id .
{
" _id" : "<user_id>",
"name" : "John Smith",
"product_categories" :[
{
"product" : "p100",
"category" : "c100"
}
]
}
{
" _id" : "<user_id>",
"name" : "Jane Smith",
"product_categories" :[
{
"product" : "p100",
"category" : "c110"
}
]
}
Approach advantages and disadvantages
Approach #1
advantage
- Possibly one query can achieve getting list of users with search text
as "camera baby"
disadvantage
- If ever we change product name "camera" or category name "baby" we
will have a hell updating all the documents with these product and category
names.
Approach #2
advantage
- If ever we change product name "camera" or category name "baby" , we
just change the name in product and category documents and all users are
still related to the new names with product id and category id in the user
documents
disadvantage
- we will have to do MORE THAN 1 query to list of users with search
text as "camera baby"
So when someone searches
-
"camera electronics" - john smith should show up in search result
-
"camera baby" - jane smith should show up in search result
-
"camera" - john smith and jane smith should show up in search
result
Million $ question
Assuming I decided to go with approach #2 . How do I achieve this query in
Elasticsearch ?
My approach
- Fire first query against elastic search on MULTIPLE TYPES ( product
and category type index) with term "camera baby" . Which will return
documents matching different types. - Group document ids based on types (application side work)
- Fire second query against elastic search on a *USER TYPE index *with
respective ids obtained from step 2.
So the question is ....
- Is it even possible to fire "camera baby" query against multiple
types under same index in elastic search (remember Im doing this since
when someone enters "camera baby" we don't know if each term is a product
or a category ? ) - Is there any way to chain query in step1 and step3 without grouping
ids on the application side ? - Is there any better way to achieve what I'm doing ( in terms
model/query/id mapping etc) above in elastic search assuming I want to
retain single place to change product /category names so that its
automatically reflected to all users who own those products in specific
category?
--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/6688550a-748c-4d7c-870e-0cf78b6ff83c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.