# Empty field in native script

**URL:** <https://discuss.elastic.co/t/empty-field-in-native-script/16560>\
**Category:** Elasticsearch\
**Created:** [March 24, 2014, 9:36am UTC](https://discuss.elastic.co/t/empty-field-in-native-script/16560 "2014-03-24T09:36:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Meidan](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@Meidan](https://discuss.elastic.co/u/Meidan)\
**Post date:** [March 24, 2014, 9:36am UTC](https://discuss.elastic.co/t/empty-field-in-native-script/16560/1 "2014-03-24T09:36:54Z")

</div>

I have created native script that checks if a specific field (mapped to  
type long) in the document is empty, using three different options below,  
the two first options sometime return false.  
Is there a documentation on the semantics of each option?

From what I see, the first two options use  
PackedArrayIndexFieldData.loadDirect(AtomicReaderContext context) which  
gets the terms from a Lucene reader (lucene.index.SegmentReader) and since  
they're not null, the field value isn't considered empty.

I'm using ElasticSearch 0.90.5 and java 1.7u17.

public class MapScript extends AbstractSearchScript {  
@Override  
public Object run() {  
if (!doc().containsKey("field2")) //sometime false  
return new Object();

```
    if (doc().get("field2") == ScriptDocValues.EMPTY) //sometime false
        return new Object();

    ScriptDocValues value = (ScriptDocValues) doc().get("field2");
    if (value.isEmpty()) //always true
        return new Object();
        
    return new Object();
}

```

}

Here’s the data for this script:  
curl -XPOST localhost:9200/documents/ -d '{ "mappings" : { "type1" :  
{ "properties" : { "field1" : { "type" : "long" }  
} }}}'  
curl -XPUT localhost:9200/documents/type1/1 -d '{ "field1" : 7 }'  
curl -XPUT localhost:9200/documents/type1/\_mapping -d '{ "type1" :  
{ "properties" : { "field2" : {"type" : "long"}  
} }}'  
curl -XPUT localhost:9200/documents/type1/2 -d '{ "field1" : 77 }'  
curl -XPUT localhost:9200/documents/type1/3 -d '{ "field1" : 3, "field2" :  
33 }'

--  
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/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Itamar\_Syn\_Hershko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/itamar_syn_hershko/32/725_2.png) [@Itamar\_Syn\_Hershko](https://discuss.elastic.co/u/Itamar_Syn_Hershko)\
**Post date:** [March 24, 2014, 9:41am UTC](https://discuss.elastic.co/t/empty-field-in-native-script/16560/2 "2014-03-24T09:41:23Z")

</div>

The internals aren't really documented.

But what are you trying to do? are you familiar with Missing Filter?

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

--

Itamar Syn-Hershko  
[http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
Freelance Developer & Consultant  
Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)

On Mon, Mar 24, 2014 at 11:36 AM, Meidan [meidan.alon@gmail.com](mailto:meidan.alon@gmail.com) wrote:

> I have created native script that checks if a specific field (mapped to  
> type long) in the document is empty, using three different options below,  
> the two first options sometime return false.  
> Is there a documentation on the semantics of each option?
> 
> From what I see, the first two options use  
> PackedArrayIndexFieldData.loadDirect(AtomicReaderContext context) which  
> gets the terms from a Lucene reader (lucene.index.SegmentReader) and since  
> they're not null, the field value isn't considered empty.
> 
> I'm using Elasticsearch 0.90.5 and java 1.7u17.
> 
> public class MapScript extends AbstractSearchScript {  
> @Override  
> public Object run() {  
> if (!doc().containsKey("field2")) //sometime false  
> return new Object();
> 
> ```
> if (doc().get("field2") == ScriptDocValues.EMPTY) //sometime false
> return new Object();
> 
> ScriptDocValues value = (ScriptDocValues) doc().get("field2");
> if (value.isEmpty()) //always true
> return new Object();
> 
> return new Object();
> }
> 
> ```
> 
> }
> 
> Here's the data for this script:  
> curl -XPOST localhost:9200/documents/ -d '{ "mappings" : { "type1" :  
> { "properties" : { "field1" : { "type" : "long" }  
> } }}}'  
> curl -XPUT localhost:9200/documents/type1/1 -d '{ "field1" : 7 }'  
> curl -XPUT localhost:9200/documents/type1/\_mapping -d '{ "type1" :  
> { "properties" : { "field2" : {"type" : "long"}  
> } }}'  
> curl -XPUT localhost:9200/documents/type1/2 -d '{ "field1" : 77 }'  
> curl -XPUT localhost:9200/documents/type1/3 -d '{ "field1" : 3, "field2" :  
> 33 }'
> 
> --  
> 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/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAHTr4ZsjJW5x1C-BwatL1ETOn%2Bf5HJ8%3DBM1MM6kt2AKQnQSj8w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZsjJW5x1C-BwatL1ETOn%2Bf5HJ8%3DBM1MM6kt2AKQnQSj8w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Meidan](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@Meidan](https://discuss.elastic.co/u/Meidan)\
**Post date:** [March 24, 2014, 9:45am UTC](https://discuss.elastic.co/t/empty-field-in-native-script/16560/3 "2014-03-24T09:45:04Z")

</div>

Sure, I want the exact same behavior as the missing filter, but in a native  
script.  
This is just a part of a much larger script I have.

On Mon, Mar 24, 2014 at 11:41 AM, Itamar Syn-Hershko [itamar@code972.com](mailto:itamar@code972.com)wrote:

> The internals aren't really documented.
> 
> But what are you trying to do? are you familiar with Missing Filter?  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-missing-filter.html)
> 
> --
> 
> Itamar Syn-Hershko  
> [http://code972.com](http://code972.com) | @synhershko [https://twitter.com/synhershko](https://twitter.com/synhershko)  
> Freelance Developer & Consultant  
> Author of RavenDB in Action [http://manning.com/synhershko/](http://manning.com/synhershko/)
> 
> On Mon, Mar 24, 2014 at 11:36 AM, Meidan [meidan.alon@gmail.com](mailto:meidan.alon@gmail.com) wrote:
> 
> > I have created native script that checks if a specific field (mapped to  
> > type long) in the document is empty, using three different options below,  
> > the two first options sometime return false.  
> > Is there a documentation on the semantics of each option?
> > 
> > From what I see, the first two options use  
> > PackedArrayIndexFieldData.loadDirect(AtomicReaderContext context) which  
> > gets the terms from a Lucene reader (lucene.index.SegmentReader) and since  
> > they're not null, the field value isn't considered empty.
> > 
> > I'm using Elasticsearch 0.90.5 and java 1.7u17.
> > 
> > public class MapScript extends AbstractSearchScript {  
> > @Override  
> > public Object run() {  
> > if (!doc().containsKey("field2")) //sometime false  
> > return new Object();
> > 
> > ```
> > if (doc().get("field2") == ScriptDocValues.EMPTY) //sometime false
> > return new Object();
> > 
> > ScriptDocValues value = (ScriptDocValues) doc().get("field2");
> > if (value.isEmpty()) //always true
> > return new Object();
> > 
> > return new Object();
> > }
> > 
> > ```
> > 
> > }
> > 
> > Here's the data for this script:  
> > curl -XPOST localhost:9200/documents/ -d '{ "mappings" : { "type1" :  
> > { "properties" : { "field1" : { "type" : "long" }  
> > } }}}'  
> > curl -XPUT localhost:9200/documents/type1/1 -d '{ "field1" : 7 }'  
> > curl -XPUT localhost:9200/documents/type1/\_mapping -d '{ "type1" :  
> > { "properties" : { "field2" : {"type" : "long"}  
> > } }}'  
> > curl -XPUT localhost:9200/documents/type1/2 -d '{ "field1" : 77 }'  
> > curl -XPUT localhost:9200/documents/type1/3 -d '{ "field1" : 3, "field2"  
> > : 33 }'
> > 
> > --  
> > 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/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5f77fa65-9f40-4bb2-98ac-9834e7362eb0%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> 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/gSlS02Dw2ss/unsubscribe](https://groups.google.com/d/topic/elasticsearch/gSlS02Dw2ss/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/CAHTr4ZsjJW5x1C-BwatL1ETOn%2Bf5HJ8%3DBM1MM6kt2AKQnQSj8w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZsjJW5x1C-BwatL1ETOn%2Bf5HJ8%3DBM1MM6kt2AKQnQSj8w%40mail.gmail.com)[https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZsjJW5x1C-BwatL1ETOn%2Bf5HJ8%3DBM1MM6kt2AKQnQSj8w%40mail.gmail.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/CAHTr4ZsjJW5x1C-BwatL1ETOn%2Bf5HJ8%3DBM1MM6kt2AKQnQSj8w%40mail.gmail.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Meidan.

--  
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/CAHHFEKny4AkKqAjmg97sc%3DLBxdFwk5N3jbviSTkHLOV%3DLE1Emw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHHFEKny4AkKqAjmg97sc%3DLBxdFwk5N3jbviSTkHLOV%3DLE1Emw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Binh\_Ly\_2](https://avatars.discourse-cdn.com/v4/letter/b/d07c76/32.png) [@Binh\_Ly\_2](https://discuss.elastic.co/u/Binh_Ly_2)\
**Post date:** [March 24, 2014, 3:58pm UTC](https://discuss.elastic.co/t/empty-field-in-native-script/16560/4 "2014-03-24T15:58:58Z")

</div>

Can you try something like this for each field you want to test?

```
        if (doc().containsKey(field)) {
              ScriptDocValues fieldValue = (ScriptDocValues) 

```

doc().get(field);  
if (fieldValue != null && !fieldValue.isEmpty()) {  
//not empty!  
}  
}

--  
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/26c1f284-8a62-4449-b88f-f50e54fbbe57%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/26c1f284-8a62-4449-b88f-f50e54fbbe57%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Meidan](https://avatars.discourse-cdn.com/v4/letter/m/848f3c/32.png) [@Meidan](https://discuss.elastic.co/u/Meidan)\
**Post date:** [March 24, 2014, 4:24pm UTC](https://discuss.elastic.co/t/empty-field-in-native-script/16560/5 "2014-03-24T16:24:53Z")

</div>

Binh, I did try that.  
As I wrote in my original message, I just want to know the differences  
between the 3 variants.

On Mon, Mar 24, 2014 at 5:58 PM, Binh Ly [binhly\_es@yahoo.com](mailto:binhly_es@yahoo.com) wrote:

> Can you try something like this for each field you want to test?
> 
> ```
> if (doc().containsKey(field)) {
> ScriptDocValues fieldValue = (ScriptDocValues)
> 
> ```
> 
> doc().get(field);  
> if (fieldValue != null && !fieldValue.isEmpty()) {  
> //not empty!  
> }  
> }
> 
> --  
> 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/gSlS02Dw2ss/unsubscribe](https://groups.google.com/d/topic/elasticsearch/gSlS02Dw2ss/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/26c1f284-8a62-4449-b88f-f50e54fbbe57%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/26c1f284-8a62-4449-b88f-f50e54fbbe57%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/26c1f284-8a62-4449-b88f-f50e54fbbe57%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/26c1f284-8a62-4449-b88f-f50e54fbbe57%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .
> 
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Meidan.

--  
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/CAHHFEKkT\_uKQgzT\_8eHb7cBwVBPEtp6Rc2\_1iWcgk4D7Xm9h\_A%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAHHFEKkT_uKQgzT_8eHb7cBwVBPEtp6Rc2_1iWcgk4D7Xm9h_A%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:41am UTC](https://discuss.elastic.co/t/empty-field-in-native-script/16560/6 "2017-07-06T01:41:05Z")

</div>


