Multi Fields terms facets


(lowang) #1

hi,

i'm pretty newbie just playing around with elastic search. For testing
puproses I added following documents to the index:

[
{"title":"iPHONE","properties":[{"product_property_id":
40,"value":"white"}, {"product_property_id":41,"value":"135"] },
{"title":"PROTEKTOR IPhone","properties":[{"product_property_id":
40,"value":"grey"}] },
{"title":"iPHONE MIC","properties":[{"product_property_id":
40,"value":"black"}] }
]

in example above "product_property_id":40 means its color,
"product_property_id":41 is weight and now I want to fetch all pairs
of possible properties and how many results applying them will fetch,
for example:

// pseudocode
{ {"product_property_id":40,"value":"black"} => 1,
{"product_property_id":40,"value":"grey"} => 1, {"product_property_id":
40,"value":"white"} =>1, {"product_property_id":41,"value":"135"] =>
1 }

how can I accomplish that ?


(Clinton Gormley) #2

Hiya

i'm pretty newbie just playing around with elastic search. For testing
puproses I added following documents to the index:

[
{"title":"iPHONE","properties":[{"product_property_id":
40,"value":"white"}, {"product_property_id":41,"value":"135"] },
{"title":"PROTEKTOR IPhone","properties":[{"product_property_id":
40,"value":"grey"}] },
{"title":"iPHONE MIC","properties":[{"product_property_id":
40,"value":"black"}] }
]

I'm not sure that you can do what you want with your data structure.

I think you would need to restructure it to look like this:

[
{"title":"iPHONE","properties":{"40": "white", "41": "135}},
{"title":"PROTEKTOR IPhone","properties":{"40": "grey"}},
{"title":"iPHONE MIC","properties":{"40": "black"}}
]

Then you could do, eg:

curl -XGET 'http://127.0.0.1:9200/_all/_search' -d '
{
"query" : {
"match_all" : {}
},
"facets" : {
"property_41" : {
"terms" : {
"field" : "properties.41"
}
},
"property_40" : {
"terms" : {
"field" : "properties.40"
}
}
},
"size" : 0
}
'

clint


(lowang) #3

Thanks Clint! that worked like a charm

but that leads me to another question, is there a way to fetch all
properties.keys ?
I think I'll need that to define all facets when building query that
you write above.
Document structure is as you suggested:

[
{"title":"iPHONE","properties":{"40": "white", "41": "135}},
{"title":"PROTEKTOR IPhone","properties":{"40": "grey"}},
{"title":"iPHONE MIC","properties":{"40": "black"}}
]

=Przemek

On Nov 30, 12:04 pm, Clinton Gormley clin...@iannounce.co.uk wrote:

Hiya

i'm pretty newbie just playing around with elastic search. For testing
puproses I added following documents to the index:

[
{"title":"iPHONE","properties":[{"product_property_id":
40,"value":"white"}, {"product_property_id":41,"value":"135"] },
{"title":"PROTEKTOR IPhone","properties":[{"product_property_id":
40,"value":"grey"}] },
{"title":"iPHONE MIC","properties":[{"product_property_id":
40,"value":"black"}] }
]

I'm not sure that you can do what you want with your data structure.

I think you would need to restructure it to look like this:

[
{"title":"iPHONE","properties":{"40": "white", "41": "135}},
{"title":"PROTEKTOR IPhone","properties":{"40": "grey"}},
{"title":"iPHONE MIC","properties":{"40": "black"}}
]

Then you could do, eg:

curl -XGET 'http://127.0.0.1:9200/_all/_search' -d '
{
"query" : {
"match_all" : {}
},
"facets" : {
"property_41" : {
"terms" : {
"field" : "properties.41"
}
},
"property_40" : {
"terms" : {
"field" : "properties.40"
}
}
},
"size" : 0}

'

clint


(Lukáš Vlček) #4

Hi,

You can fetch field values if the field is stored.

But I think you probably want to avoid extra query for stored fields and
then running a new query with facets built on top of results from previous
query, right? Please correct me anybody if I am wrong but it is not possible
to have ES generate all facets for field values dynamically now. I remember
discussing similar functionality on #IRC some time ago and if my memory
serves me good such functionality should be available in some future
release. But it is not there yet so now you have to use an extra query
"technique" (which may not be optimal). Also if you know that number lot of
property keys is not high you can always create facets for all possible key
values (definitely not nice solution but can work good in some cases).

Regards,
Lukas

On Wed, Dec 1, 2010 at 9:56 PM, Przemyslaw Wroblewski <
przemyslaw.wroblewski@gmail.com> wrote:

Thanks Clint! that worked like a charm

but that leads me to another question, is there a way to fetch all
properties.keys ?
I think I'll need that to define all facets when building query that
you write above.
Document structure is as you suggested:

[
{"title":"iPHONE","properties":{"40": "white", "41": "135}},
{"title":"PROTEKTOR IPhone","properties":{"40": "grey"}},
{"title":"iPHONE MIC","properties":{"40": "black"}}
]

=Przemek

On Nov 30, 12:04 pm, Clinton Gormley clin...@iannounce.co.uk wrote:

Hiya

i'm pretty newbie just playing around with elastic search. For testing
puproses I added following documents to the index:

[
{"title":"iPHONE","properties":[{"product_property_id":
40,"value":"white"}, {"product_property_id":41,"value":"135"] },
{"title":"PROTEKTOR IPhone","properties":[{"product_property_id":
40,"value":"grey"}] },
{"title":"iPHONE MIC","properties":[{"product_property_id":
40,"value":"black"}] }
]

I'm not sure that you can do what you want with your data structure.

I think you would need to restructure it to look like this:

[
{"title":"iPHONE","properties":{"40": "white", "41": "135}},
{"title":"PROTEKTOR IPhone","properties":{"40": "grey"}},
{"title":"iPHONE MIC","properties":{"40": "black"}}
]

Then you could do, eg:

curl -XGET 'http://127.0.0.1:9200/_all/_search' -d '
{
"query" : {
"match_all" : {}
},
"facets" : {
"property_41" : {
"terms" : {
"field" : "properties.41"
}
},
"property_40" : {
"terms" : {
"field" : "properties.40"
}
}
},
"size" : 0}

'

clint


(system) #5