Accessing date field in a native (Java) script


(Niccolò Becchi) #1

Hi,
I haven't been able to find any examples about how to access to a date
field inside a java native script.
I have a 'datePublished' field defined as following in the mapping:

mappings: {

  • doc: {
    • properties: {
      • ...
      • datePublished: {
        • format: dateOptionalTime
        • type: date
          }
          }
          }

}

However, inside the class:

public class ScoreByAgeScript extends AbstractDoubleSearchScript {

public ScoreByAgeScript(@Nullable Map<String,Object> params) {
    ...
}

@Override
public double runAsDouble() {
        Object datePublishedStr = source().get(fieldDate); // => Get a 

string 2013-07-07T12:00:00
Object datePublishedDoc = doc().get(fieldDate); // => Get a
list of strings [00, 03, 07t12, 2013]
}

}

I have been able only to the string text.
I could eventually parse the text to a Date Java object but I would prefer
to avoid it, avoiding any problem about string format or timezone, etc..
Is there any way to get the java date Object?
Thanks
Niccolo

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/37cfcecc-400a-4344-95fb-77ab6509520a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Binh Ly-2) #2

If you do something like this, you should get the epoch value in
milliseconds. Then you can use that value to initialize whatever object you
want:

          ScriptDocValues v = (ScriptDocValues) doc().get(dateField);
          if (v != null && !v.isEmpty()) {
            long epoch_ms = ((Longs)v).getValue();
          }

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/f746d44b-f9dd-4c65-a4af-f653fb8e86c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(Niccolò Becchi) #3

Sorry Bihn,
just seen now your suggestion but It doesn't work.
For a field* "datePublished: {format: dateOptionalTime, type: date}"* when
I perform:

ScriptDocValues v = (ScriptDocValues) doc().get(dateField);

I got an object
org.elasticsearch.index.fielddata.ScriptDocValues$Strings@5e834b36 that's
containing the following strings:

[01, 12, 15z, 2014, 30t10]

For the following date 2014-01-30T10:12:15Z stored in ES. I don't know how
to fix it.
Thanks a lot
Niccolo'

Il giorno lunedì 10 marzo 2014 18:22:23 UTC, Binh Ly ha scritto:

If you do something like this, you should get the epoch value in
milliseconds. Then you can use that value to initialize whatever object you
want:

          ScriptDocValues v = (ScriptDocValues) doc().get(dateField);
          if (v != null && !v.isEmpty()) {
            long epoch_ms = ((Longs)v).getValue();
          }

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/deb72f15-f765-4287-b073-b9ddce1ef7fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(system) #4