Reputation: 171
Im trying to query elasticsearch to do the following :
Data going into Elastic
Order Number Product Name
order1 Chicken
order2 Banana
order3 Chicken
order1 Cucumber
order2 Chicken
order3 Apples
order1 Flour
order2 Rice
order3 Nuts
This is the Query im using to get a Product Name
{
"query" : {
"match" : {
"ProductName" : "Chicken"
}
Result : All orders that contain Chicken
Order Number Product Name
order1 Chicken
order3 Chicken
order2 Chicken
What I would like to achieve:
If the above orders have Chicken in it then give me all the other Products that was also purchased with Chicken.
Order Number Product Name
order1 Cucumber
order2 Banana
order3 Apples
order1 Flour
order2 Rice
order3 Nuts
How do i go about constructing a query to do this ? please help i am very new to elasticsearch.
Upvotes: 1
Views: 73
Reputation: 198
This question has many different answers and it really comes down to what you want to do with this data beyond what you are asking.
Assuming however that all you want to do is get all orders that contain a particular product, I'd advise changing your indexing structure a bit.
You are indexing data in a very SQL like way. Instead you should be thinking in terms of documents or entities. As I see it an "order" should exist as a single document. It could look like this:
"order": {
"properties": {
"orderNumber": {
"type": "string"
},
"product": {
"properties": {
"name": {
"type": "string"
}
}
}
}
}
That is, a single order is a document, which can contain several products. This would allow you to get all orders which contain "Chicken" or whatever else, but would also allow you to get a list of all products by orderNumber.
Again this depends a lot of how you intend to use your data, the main thing I would keep in mind is that elasticsearch is not a relational database. You have to put a little more thought into how you store things as you can't do as complex joins when querying.
Upvotes: 1