Terms facet

Hi,

I have the following document structure:
"simple_array":{
"dynamic":"true",
"properties":{
"field_name":{
"type":"string",
"index":"not_analyzed"
},
"last_field":{
"type":"boolean"
}
}
}

Now I want to get terms facet for simple_array.field_name and it would be
easy:
{
"facets":{
"test_facet":{
"terms":{
"field":"simple_array.field_name",
}
}
}
}

But now I want to get counted only that 'field_name' fields that have
'last_field' set fo 'true'. I know that it is simple using nested type with
facet filter. But I have a situation where simple_array is only one of
document fields and I have to use in request filters for other document
fields - here nested type for 'simple_array' won't work.

Thanks
Best regards.

As I can see, the best way would be:
{
"facets":{
"test_facet":{
"terms":{
"field":"simple_array.field_name",
"script":"_source.simple_array[i].last_field"
}
}
}
}

But how to get 'i' for given term ? :slight_smile: _source.simple_array.indexOf(term)
does not work because simple_array keeps documents containing 'term'...

I would really appreciate your help here :slight_smile:

Best regards.
Marcin.

2012/4/12 Marcin Dojwa m.dojwa@livechatinc.com

Hi,

I have the following document structure:
"simple_array":{
"dynamic":"true",
"properties":{
"field_name":{
"type":"string",
"index":"not_analyzed"
},
"last_field":{
"type":"boolean"
}
}
}

Now I want to get terms facet for simple_array.field_name and it would be
easy:
{
"facets":{
"test_facet":{
"terms":{
"field":"simple_array.field_name",
}
}
}
}

But now I want to get counted only that 'field_name' fields that have
'last_field' set fo 'true'. I know that it is simple using nested type with
facet filter. But I have a situation where simple_array is only one of
document fields and I have to use in request filters for other document
fields - here nested type for 'simple_array' won't work.

Thanks
Best regards.

If all your field names are unique within sample_array and you store
source, you could do something like this:

"facets": {
    "test_facet":{
        "terms":{
            "field":"simple_array.field_name",
            "script" : "foreach(item : _source.simple_array) { 

if(item.field_name.equals(term)) return item.last_field }"
}
}
}

However, when I find that I need to do something as complicated as this I,
typically, consider it as an indication that I should refactor my schema.

On Thursday, April 12, 2012 5:37:41 PM UTC-4, Marcin Dojwa wrote:

As I can see, the best way would be:
{
"facets":{
"test_facet":{
"terms":{
"field":"simple_array.field_name",
"script":"_source.simple_array[i].last_field"
}
}
}
}

But how to get 'i' for given term ? :slight_smile: _source.simple_array.indexOf(term)
does not work because simple_array keeps documents containing 'term'...

I would really appreciate your help here :slight_smile:

Best regards.
Marcin.

2012/4/12 Marcin Dojwa m.dojwa@livechatinc.com

Hi,

I have the following document structure:
"simple_array":{
"dynamic":"true",
"properties":{
"field_name":{
"type":"string",
"index":"not_analyzed"
},
"last_field":{
"type":"boolean"
}
}
}

Now I want to get terms facet for simple_array.field_name and it would be
easy:
{
"facets":{
"test_facet":{
"terms":{
"field":"simple_array.field_name",
}
}
}
}

But now I want to get counted only that 'field_name' fields that have
'last_field' set fo 'true'. I know that it is simple using nested type with
facet filter. But I have a situation where simple_array is only one of
document fields and I have to use in request filters for other document
fields - here nested type for 'simple_array' won't work.

Thanks
Best regards.

Thanks :slight_smile: it's good to know that scripts like this are allowed. But you are
right, I will modify document structure in that case.

Thanks for help
Marcin

W dniu piątek, 13 kwietnia 2012 użytkownik Igor Motov napisał:

If all your field names are unique within sample_array and you store
source, you could do something like this:

"facets": {
    "test_facet":{
        "terms":{
            "field":"simple_array.field_name",
            "script" : "foreach(item : _source.simple_array) {

if(item.field_name.equals(term)) return item.last_field }"
}
}
}

However, when I find that I need to do something as complicated as this I,
typically, consider it as an indication that I should refactor my schema.

On Thursday, April 12, 2012 5:37:41 PM UTC-4, Marcin Dojwa wrote:

As I can see, the best way would be:
{
"facets":{
"test_facet":{
"terms":{
"field":"simple_array.field_na**me",
"script":"source.simple**array[i].last_field"
}
}
}
}

But how to get 'i' for given term ? :slight_smile: _source.simple_array.**indexOf(term)
does not work because simple_array keeps documents containing 'term'...

I would really appreciate your help here :slight_smile:

Best regards.
Marcin.

2012/4/12 Marcin Dojwa <m.dojwa@livechatinc.com <javascript:_e({},
'cvml', 'm.dojwa@livechatinc.com');>>

Hi,

I have the following document structure:
"simple_array":{
"dynamic":"true",
"properties":{
"field_name":{
"type":"string",
"index":"not_analyzed"
},
"last_field":{
"type":"boolean"
}
}
}

Now I want to get terms facet for simple_array.field_name and it would
be easy:
{
"facets":{
"test_facet":{
"terms":{
"field":"simple_array.field_**name",
}
}
}
}

But now I want to get counted only that 'field_name' fields that have
'last_field' set fo 'true'. I know that it is simple using nested type with
facet filter. But I have a situation where simple_array is only one of
document fields and I have to use in request filters for other document
fields - here nested type for 'simple_array' won't work.

Thanks
Best regards.