# Query with condition on attribute existence. How-to manage an if xx.length or

**URL:** <https://discuss.elastic.co/t/query-with-condition-on-attribute-existence-how-to-manage-an-if-xx-length-or/14625>\
**Category:** Elasticsearch\
**Created:** [November 28, 2013, 9:42am UTC](https://discuss.elastic.co/t/query-with-condition-on-attribute-existence-how-to-manage-an-if-xx-length-or/14625 "2013-11-28T09:42:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tchinkatchuk](https://avatars.discourse-cdn.com/v4/letter/t/958977/32.png) [@Tchinkatchuk](https://discuss.elastic.co/u/Tchinkatchuk)\
**Post date:** [November 28, 2013, 9:42am UTC](https://discuss.elastic.co/t/query-with-condition-on-attribute-existence-how-to-manage-an-if-xx-length-or/14625/1 "2013-11-28T09:42:06Z")

</div>

Hi all,

I have multiple documents indexed with dynamic templates.  
So docs have differents structures (some are common, some are not)

I am trying to do a simple query with facets on source field (not\_analyzed  
fields).  
My issue is that if the field does not exists on all docs, the quey failed.  
I am sure there is a simple answer but after hours, I could not find the  
clue.

curl -XGET 'localhost:9200/\_search?pretty=true' -d '{  
"query": {  
"query\_string": {  
"query": "prof",  
"fields": [  
"slug",  
"name"  
]  
}  
},

```
"facets": {
    "marque1": {
        "terms": {
            "field": "marque"
        }
    },
    "marque2": {
        "terms": {
            "script": "_source.marque"
        }
    },
    "mime": {
        "terms": {
            "script": "_source.mime_type"
        }
    }
}

```

}'

Here, the 1st facet is ok but tokenized  
The 2nd is OK and not tokenized. I'll take it)  
The 3rd should be ok but mime\_type is not stored for every documents so I  
have this error :

{

```
 ...

"failures" : [ {

        "shard" : 0,

  "status" : 500,

  "reason" : "QueryPhaseExecutionException[[xxx][0]: query[(slug:prof | 

```

name:prof)],from[0],size[10]: Query Failed [Failed to execute main query]];  
nested: PropertyAccessException[[Error: could not access: mime\_type; in  
class: org.elasticsearch.search.lookup.SourceLookup]\n[Near : {...  
\_source.mime\_type ....}]\n ^\n[Line: 1, Column: 1]]; "

```
} ]

```

},

Could anyone help me on this ?

Thanks

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 28, 2013, 10:10am UTC](https://discuss.elastic.co/t/query-with-condition-on-attribute-existence-how-to-manage-an-if-xx-length-or/14625/2 "2013-11-28T10:10:46Z")

</div>

Probably adding a `if` statement in your script could help here?

"\_source.mime\_type !== undefined? \_source.mime\_type : """

Does it work for you?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 28 novembre 2013 at 10:42:10, Georges@Bibtol (sg@quable.fr) a écrit:

Hi all,

I have multiple documents indexed with dynamic templates.  
So docs have differents structures (some are common, some are not)

I am trying to do a simple query with facets on source field (not\_analyzed fields).  
My issue is that if the field does not exists on all docs, the quey failed.  
I am sure there is a simple answer but after hours, I could not find the clue.

curl -XGET 'localhost:9200/\_search?pretty=true' -d '{  
"query": {  
"query\_string": {  
"query": "prof",  
"fields": [  
"slug",  
"name"  
]  
}  
},

```
"facets": {
    "marque1": {
        "terms": {
            "field": "marque"
        }
    },
    "marque2": {
        "terms": {
            "script": "_source.marque"
        }
    },
    "mime": {
        "terms": {
            "script": "_source.mime_type"
        }
    }
}

```

}'

Here, the 1st facet is ok but tokenized  
The 2nd is OK and not tokenized. I'll take it)  
The 3rd should be ok but mime\_type is not stored for every documents so I have this error :  
{

```
 ...

"failures" : [ {

        "shard" : 0,

  "status" : 500,

  "reason" : "QueryPhaseExecutionException[[xxx][0]: query[(slug:prof | name:prof)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: PropertyAccessException[[Error: could not access: mime_type; in class: org.elasticsearch.search.lookup.SourceLookup]\n[Near : {... _source.mime_type ....}]\n ^\n[Line: 1, Column: 1]]; "

} ]

```

},

Could anyone help me on this ?

Thanks

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Tchinkatchuk](https://avatars.discourse-cdn.com/v4/letter/t/958977/32.png) [@Tchinkatchuk](https://discuss.elastic.co/u/Tchinkatchuk)\
**Post date:** [November 28, 2013, 10:19am UTC](https://discuss.elastic.co/t/query-with-condition-on-attribute-existence-how-to-manage-an-if-xx-length-or/14625/3 "2013-11-28T10:19:54Z")

</div>

hi David,

Thanks for the quick answer.  
This script :

```
curl -XGET 'localhost:9200/_search?pretty=true' -d '{
  "query": {
    "query_string": {
      "query": "prof",
      "fields": [
        "slug",
        "name"
      ]
    }
  },

    "facets": {
        "marque1": {
            "terms": {
                "field": "marque"
            }
        },
        "marque2": {
            "terms": {
                "script": "_source.marque"
            }
        },
        "mime_type": {
            "terms": {
                "script": "_source.mime_type !== undefined ? _source.mime_type : \"\""
            }
        }
    }
}'
```

return

```
CompileException[[Error: not a statement, or badly formed structure]\n[Near : {... _source.mime_type !== undefined ? _source.mime_typ ....}]

```

Did I understood your answer ?

Hope to see you soon in ElasticSearch french meeting

Cordialement,

Sylvain Gourvil  
Directeur Technique  
Quable SAS - [http://www.quable.fr](http://www.quable.fr)

On 28 novembre 2013 at 11:11:49, David Pilato (david@pilato.fr) wrote:

Probably adding a `if` statement in your script could help here?

"\_source.mime\_type !== undefined? \_source.mime\_type : """

Does it work for you?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 28 novembre 2013 at 10:42:10, Georges@Bibtol (sg@quable.fr) a écrit:

Hi all,

I have multiple documents indexed with dynamic templates.  
So docs have differents structures (some are common, some are not)

I am trying to do a simple query with facets on source field (not\_analyzed fields).  
My issue is that if the field does not exists on all docs, the quey failed.  
I am sure there is a simple answer but after hours, I could not find the clue.

curl -XGET 'localhost:9200/\_search?pretty=true' -d '{  
"query": {  
"query\_string": {  
"query": "prof",  
"fields": [  
"slug",  
"name"  
]  
}  
},

```
"facets": {
    "marque1": {
        "terms": {
            "field": "marque"
        }
    },
    "marque2": {
        "terms": {
            "script": "_source.marque"
        }
    },
    "mime": {
        "terms": {
            "script": "_source.mime_type"
        }
    }
}

```

}'

Here, the 1st facet is ok but tokenized  
The 2nd is OK and not tokenized. I'll take it)  
The 3rd should be ok but mime\_type is not stored for every documents so I have this error :  
{

```
 ...

"failures" : [ {

        "shard" : 0,

  "status" : 500,

  "reason" : "QueryPhaseExecutionException[[xxx][0]: query[(slug:prof | name:prof)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: PropertyAccessException[[Error: could not access: mime_type; in class: org.elasticsearch.search.lookup.SourceLookup]\n[Near : {... _source.mime_type ....}]\n ^\n[Line: 1, Column: 1]]; "

} ]

```

},

Could anyone help me on this ?

Thanks

## -- 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](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe).  
To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.529718ca.6b8b4567.22e%40GeorgesAbitbol.local](https://groups.google.com/d/msgid/elasticsearch/etPan.529718ca.6b8b4567.22e%40GeorgesAbitbol.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 28, 2013, 10:31am UTC](https://discuss.elastic.co/t/query-with-condition-on-attribute-existence-how-to-manage-an-if-xx-length-or/14625/4 "2013-11-28T10:31:45Z")

</div>

Your understanding is correct. I was just hoping it to work 🙂

Try with this: "script": "doc['test'].empty ? null : \_source.test"

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 28 novembre 2013 at 11:20:00, Quable - Sylvain Gourvil (sg@quable.fr) a écrit:

hi David,

Thanks for the quick answer.  
This script :

```
curl -XGET 'localhost:9200/_search?pretty=true' -d '{
  "query": {
    "query_string": {
      "query": "prof",
      "fields": [
        "slug",
        "name"
      ]
    }
  },

    "facets": {
        "marque1": {
            "terms": {
                "field": "marque"
            }
        },
        "marque2": {
            "terms": {
                "script": "_source.marque"
            }
        },
        "mime_type": {
            "terms": {
                "script": "_source.mime_type !== undefined ? _source.mime_type : \"\""
            }
        }
    }
}'
```

return

CompileException[[Error: not a statement, or badly formed structure]\n[Near : {... \_source.mime\_type !== undefined ? \_source.mime\_typ ....}]

Did I understood your answer ?

Hope to see you soon in ElasticSearch french meeting

Cordialement,

Sylvain Gourvil  
Directeur Technique  
Quable SAS - [http://www.quable.fr](http://www.quable.fr)

On 28 novembre 2013 at 11:11:49, David Pilato (david@pilato.fr) wrote:

Probably adding a `if` statement in your script could help here?

"\_source.mime\_type !== undefined? \_source.mime\_type : """

Does it work for you?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 28 novembre 2013 at 10:42:10, Georges@Bibtol (sg@quable.fr) a écrit:

Hi all,

I have multiple documents indexed with dynamic templates.  
So docs have differents structures (some are common, some are not)

I am trying to do a simple query with facets on source field (not\_analyzed fields).  
My issue is that if the field does not exists on all docs, the quey failed.  
I am sure there is a simple answer but after hours, I could not find the clue.

curl -XGET 'localhost:9200/\_search?pretty=true' -d '{  
"query": {  
"query\_string": {  
"query": "prof",  
"fields": [  
"slug",  
"name"  
]  
}  
},

```
"facets": {
    "marque1": {
        "terms": {
            "field": "marque"
        }
    },
    "marque2": {
        "terms": {
            "script": "_source.marque"
        }
    },
    "mime": {
        "terms": {
            "script": "_source.mime_type"
        }
    }
}

```

}'

Here, the 1st facet is ok but tokenized  
The 2nd is OK and not tokenized. I'll take it)  
The 3rd should be ok but mime\_type is not stored for every documents so I have this error :  
{

```
 ...

"failures" : [ {

        "shard" : 0,

  "status" : 500,

  "reason" : "QueryPhaseExecutionException[[xxx][0]: query[(slug:prof | name:prof)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: PropertyAccessException[[Error: could not access: mime_type; in class: org.elasticsearch.search.lookup.SourceLookup]\n[Near : {... _source.mime_type ....}]\n ^\n[Line: 1, Column: 1]]; "

} ]

```

},

Could anyone help me on this ?

Thanks

## -- 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](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

## You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group. To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe). To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.529718ca.6b8b4567.22e%40GeorgesAbitbol.local](https://groups.google.com/d/msgid/elasticsearch/etPan.529718ca.6b8b4567.22e%40GeorgesAbitbol.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.52971b91.15b5af5c.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.52971b91.15b5af5c.3e14%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Tchinkatchuk](https://avatars.discourse-cdn.com/v4/letter/t/958977/32.png) [@Tchinkatchuk](https://discuss.elastic.co/u/Tchinkatchuk)\
**Post date:** [November 28, 2013, 10:50am UTC](https://discuss.elastic.co/t/query-with-condition-on-attribute-existence-how-to-manage-an-if-xx-length-or/14625/5 "2013-11-28T10:50:16Z")

</div>

Thanks a lot !!

This works

curl -XGET 'localhost:9200/\_search?pretty=true' -d '{  
"query": {  
"query\_string": {  
"query": "air",  
"fields": [  
"slug",  
"name"  
]  
}  
},

```
"facets": {
    "marque2": {
        "terms": {
            "script": "_doc[\"marque\"].empty ? null : _source.marque"
        }
    },
    "mime_type": {
        "terms": {
            "script": "_doc[\"mime_type\"].empty ? null : _source.mime_type"
        }
    },
    "filetage": {
        "terms": {
            "script": "_doc[\"Filetage\"].empty ? null : _source.Filetage"
        }
    }
}

```

}’

But if « mime\_type » has never been dynamically created in index, it fails.

So it is possible to facet on an existing field which is not not in results but it is not possible to filter on a field that does not exists.

I’ll manage with this for the moment.

Again, thank you David.

On 28 novembre 2013 at 11:31:56, David Pilato (david@pilato.fr) wrote:

Your understanding is correct. I was just hoping it to work 🙂

Try with this: "script": "doc['test'].empty ? null : \_source.test"

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 28 novembre 2013 at 11:20:00, Quable - Sylvain Gourvil (sg@quable.fr) a écrit:

hi David,

Thanks for the quick answer.  
This script :

```
curl -XGET 'localhost:9200/_search?pretty=true' -d '{
  "query": {
    "query_string": {
      "query": "prof",
      "fields": [
        "slug",
        "name"
      ]
    }
  },

    "facets": {
        "marque1": {
            "terms": {
                "field": "marque"
            }
        },
        "marque2": {
            "terms": {
                "script": "_source.marque"
            }
        },
        "mime_type": {
            "terms": {
                "script": "_source.mime_type !== undefined ? _source.mime_type : \"\""
            }
        }
    }
}'
```

return

CompileException[[Error: not a statement, or badly formed structure]\n[Near : {... \_source.mime\_type !== undefined ? \_source.mime\_typ ....}]

Did I understood your answer ?

Hope to see you soon in ElasticSearch french meeting

Cordialement,

Sylvain Gourvil  
Directeur Technique  
Quable SAS - [http://www.quable.fr](http://www.quable.fr)

On 28 novembre 2013 at 11:11:49, David Pilato (david@pilato.fr) wrote:

Probably adding a `if` statement in your script could help here?

"\_source.mime\_type !== undefined? \_source.mime\_type : """

Does it work for you?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 28 novembre 2013 at 10:42:10, Georges@Bibtol (sg@quable.fr) a écrit:

Hi all,

I have multiple documents indexed with dynamic templates.  
So docs have differents structures (some are common, some are not)

I am trying to do a simple query with facets on source field (not\_analyzed fields).  
My issue is that if the field does not exists on all docs, the quey failed.  
I am sure there is a simple answer but after hours, I could not find the clue.

curl -XGET 'localhost:9200/\_search?pretty=true' -d '{  
"query": {  
"query\_string": {  
"query": "prof",  
"fields": [  
"slug",  
"name"  
]  
}  
},

```
"facets": {
    "marque1": {
        "terms": {
            "field": "marque"
        }
    },
    "marque2": {
        "terms": {
            "script": "_source.marque"
        }
    },
    "mime": {
        "terms": {
            "script": "_source.mime_type"
        }
    }
}

```

}'

Here, the 1st facet is ok but tokenized  
The 2nd is OK and not tokenized. I'll take it)  
The 3rd should be ok but mime\_type is not stored for every documents so I have this error :  
{

```
 ...

"failures" : [ {

        "shard" : 0,

  "status" : 500,

  "reason" : "QueryPhaseExecutionException[[xxx][0]: query[(slug:prof | name:prof)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: PropertyAccessException[[Error: could not access: mime_type; in class: org.elasticsearch.search.lookup.SourceLookup]\n[Near : {... _source.mime_type ....}]\n ^\n[Line: 1, Column: 1]]; "

} ]

```

},

Could anyone help me on this ?

Thanks

## -- 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](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

## You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group. To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe). To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

## 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](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.529718ca.6b8b4567.22e%40GeorgesAbitbol.local](https://groups.google.com/d/msgid/elasticsearch/etPan.529718ca.6b8b4567.22e%40GeorgesAbitbol.local). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group.  
To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe).  
To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.52971b91.15b5af5c.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.52971b91.15b5af5c.3e14%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.52971fe8.643c9869.22e%40GeorgesAbitbol.local](https://groups.google.com/d/msgid/elasticsearch/etPan.52971fe8.643c9869.22e%40GeorgesAbitbol.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 28, 2013, 10:52am UTC](https://discuss.elastic.co/t/query-with-condition-on-attribute-existence-how-to-manage-an-if-xx-length-or/14625/6 "2013-11-28T10:52:15Z")

</div>

Cool. For those who will come to this concern, here is a Gist which shows it: [https://gist.github.com/dadoonet/7689977](https://gist.github.com/dadoonet/7689977)

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 28 novembre 2013 at 11:50:21, Quable - Sylvain Gourvil (sg@quable.fr) a écrit:

Thanks a lot !!

This works

curl -XGET 'localhost:9200/\_search?pretty=true' -d '{  
"query": {  
"query\_string": {  
"query": "air",  
"fields": [  
"slug",  
"name"  
]  
}  
},

```
"facets": {
    "marque2": {
        "terms": {
            "script": "_doc[\"marque\"].empty ? null : _source.marque"
        }
    },
    "mime_type": {
        "terms": {
            "script": "_doc[\"mime_type\"].empty ? null : _source.mime_type"
        }
    },
    "filetage": {
        "terms": {
            "script": "_doc[\"Filetage\"].empty ? null : _source.Filetage"
        }
    }
}

```

}’

But if « mime\_type » has never been dynamically created in index, it fails.

So it is possible to facet on an existing field which is not not in results but it is not possible to filter on a field that does not exists.

I’ll manage with this for the moment.

Again, thank you David.

On 28 novembre 2013 at 11:31:56, David Pilato (david@pilato.fr) wrote:

Your understanding is correct. I was just hoping it to work 🙂

Try with this: "script": "doc['test'].empty ? null : \_source.test"

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 28 novembre 2013 at 11:20:00, Quable - Sylvain Gourvil (sg@quable.fr) a écrit:

hi David,

Thanks for the quick answer.  
This script :

```
curl -XGET 'localhost:9200/_search?pretty=true' -d '{
  "query": {
    "query_string": {
      "query": "prof",
      "fields": [
        "slug",
        "name"
      ]
    }
  },

    "facets": {
        "marque1": {
            "terms": {
                "field": "marque"
            }
        },
        "marque2": {
            "terms": {
                "script": "_source.marque"
            }
        },
        "mime_type": {
            "terms": {
                "script": "_source.mime_type !== undefined ? _source.mime_type : \"\""
            }
        }
    }
}'
```

return

CompileException[[Error: not a statement, or badly formed structure]\n[Near : {... \_source.mime\_type !== undefined ? \_source.mime\_typ ....}]

Did I understood your answer ?

Hope to see you soon in ElasticSearch french meeting

Cordialement,

Sylvain Gourvil  
Directeur Technique  
Quable SAS - [http://www.quable.fr](http://www.quable.fr)

On 28 novembre 2013 at 11:11:49, David Pilato (david@pilato.fr) wrote:

Probably adding a `if` statement in your script could help here?

"\_source.mime\_type !== undefined? \_source.mime\_type : """

Does it work for you?

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr

Le 28 novembre 2013 at 10:42:10, Georges@Bibtol (sg@quable.fr) a écrit:

Hi all,

I have multiple documents indexed with dynamic templates.  
So docs have differents structures (some are common, some are not)

I am trying to do a simple query with facets on source field (not\_analyzed fields).  
My issue is that if the field does not exists on all docs, the quey failed.  
I am sure there is a simple answer but after hours, I could not find the clue.

curl -XGET 'localhost:9200/\_search?pretty=true' -d '{  
"query": {  
"query\_string": {  
"query": "prof",  
"fields": [  
"slug",  
"name"  
]  
}  
},

```
"facets": {
    "marque1": {
        "terms": {
            "field": "marque"
        }
    },
    "marque2": {
        "terms": {
            "script": "_source.marque"
        }
    },
    "mime": {
        "terms": {
            "script": "_source.mime_type"
        }
    }
}

```

}'

Here, the 1st facet is ok but tokenized  
The 2nd is OK and not tokenized. I'll take it)  
The 3rd should be ok but mime\_type is not stored for every documents so I have this error :  
{

```
 ...

"failures" : [ {

        "shard" : 0,

  "status" : 500,

  "reason" : "QueryPhaseExecutionException[[xxx][0]: query[(slug:prof | name:prof)],from[0],size[10]: Query Failed [Failed to execute main query]]; nested: PropertyAccessException[[Error: could not access: mime_type; in class: org.elasticsearch.search.lookup.SourceLookup]\n[Near : {... _source.mime_type ....}]\n ^\n[Line: 1, Column: 1]]; "

} ]

```

},

Could anyone help me on this ?

Thanks

## -- 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](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8a20eceb-9c5b-47e4-b012-65e6fec064f3%40googlegroups.com). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

## You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group. To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe). To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.529716a6.520eedd1.3e14%40MacBook-Air-de-David.local). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

## 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](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.529718ca.6b8b4567.22e%40GeorgesAbitbol.local](https://groups.google.com/d/msgid/elasticsearch/etPan.529718ca.6b8b4567.22e%40GeorgesAbitbol.local). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

## You received this message because you are subscribed to a topic in the Google Groups "elasticsearch" group. To unsubscribe from this topic, visit [https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/kvIm3Na7up8/unsubscribe). To unsubscribe from this group and all its topics, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com). To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.52971b91.15b5af5c.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.52971b91.15b5af5c.3e14%40MacBook-Air-de-David.local). For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.52971fe8.643c9869.22e%40GeorgesAbitbol.local](https://groups.google.com/d/msgid/elasticsearch/etPan.52971fe8.643c9869.22e%40GeorgesAbitbol.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.5297205f.3f6ab60f.3e14%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.5297205f.3f6ab60f.3e14%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:04am UTC](https://discuss.elastic.co/t/query-with-condition-on-attribute-existence-how-to-manage-an-if-xx-length-or/14625/7 "2017-07-06T02:04:20Z")

</div>


