/** * Use this method when you are sure that the value can't be null in ES document. * <p/> * Warning with numbers - even if mapping declares long field, value can be an Integer * instead of an expected Long. The reason is that ES delegates the deserialization of JSON * to Jackson, which doesn't know the field type declared in mapping. See * https://groups.google.com/forum/#!searchin/elasticsearch/getsource$20integer$20long/elasticsearch/jxIY22TmA8U/PyqZPPyYQ0gJ * for more details. Workaround is to cast to java.lang.Number and then to call {@link Number#longValue()} */ public <K> K getField(String key) { K value = getNullableField(key); if (value == null) { throw new IllegalStateException("Value of index field is null: " + key); } return value; }
@CheckForNull public Date getNullableFieldAsDate(String key) { Object val = getNullableField(key); if (val != null) { if (val instanceof Date) { return (Date)val; } if (val instanceof Number) { return epochSecondsToDate((Number) val); } return EsUtils.parseDateTime((String) val); } return null; }
@Test public void getField() { Map<String, Object> fields = Maps.newHashMap(); fields.put("a_string", "foo"); fields.put("a_int", 42); fields.put("a_null", null); BaseDoc doc = new BaseDoc(fields) { @Override public String getId() { return null; } @Override public String getRouting() { return null; } @Override public String getParent() { return null; } }; assertThat((String) doc.getNullableField("a_string")).isEqualTo("foo"); assertThat((int) doc.getNullableField("a_int")).isEqualTo(42); assertThat((String) doc.getNullableField("a_null")).isNull(); }
@Test public void getField_fails_if_missing_field() { Map<String, Object> fields = Collections.emptyMap(); BaseDoc doc = new BaseDoc(fields) { @Override public String getId() { return null; } @Override public String getRouting() { return null; } @Override public String getParent() { return null; } }; try { doc.getNullableField("a_string"); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Field a_string not specified in query options"); } }
/** * Use this method when you are sure that the value can't be null in ES document. * <p/> * Warning with numbers - even if mapping declares long field, value can be an Integer * instead of an expected Long. The reason is that ES delegates the deserialization of JSON * to Jackson, which doesn't know the field type declared in mapping. See * https://groups.google.com/forum/#!searchin/elasticsearch/getsource$20integer$20long/elasticsearch/jxIY22TmA8U/PyqZPPyYQ0gJ * for more details. Workaround is to cast to java.lang.Number and then to call {@link Number#longValue()} */ public <K> K getField(String key) { K value = getNullableField(key); if (value == null) { throw new IllegalStateException("Value of index field is null: " + key); } return value; }
@CheckForNull public Date getNullableFieldAsDate(String key) { Object val = getNullableField(key); if (val != null) { if (val instanceof Date) { return (Date)val; } if (val instanceof Number) { return epochSecondsToDate((Number) val); } return EsUtils.parseDateTime((String) val); } return null; }