# How to save elasticsearch date type in spring data?

**URL:** <https://discuss.elastic.co/t/how-to-save-elasticsearch-date-type-in-spring-data/240249>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [July 8, 2020, 2:22am UTC](https://discuss.elastic.co/t/how-to-save-elasticsearch-date-type-in-spring-data/240249 "2020-07-08T02:22:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kase](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@kase](https://discuss.elastic.co/u/kase)\
**Post date:** [July 8, 2020, 2:22am UTC](https://discuss.elastic.co/t/how-to-save-elasticsearch-date-type-in-spring-data/240249/1 "2020-07-08T02:22:41Z")

</div>

Hi there,  
I want to use spring data to get elasticsearch documents, but encountered some problems with get data.  
Here is my code.  
Entity:

```auto
    @Id
    private String id;

    @Field(name = "@timestamp", type = FieldType.Date)
    private Date timestamp;

    @Field(name = "netflow.first_switched", type = FieldType.Date)
    private Date firstSwitched;

    @Field(name = "netflow.last_switched", type = FieldType.Date)
    private Date lastSwitched;

```

My data in elasticsearch:  
 ![註解 2020-07-08 101000](https://us1.discourse-cdn.com/elastic/original/3X/d/4/d4b64a70ad7ae2997c9d81971f9a5b315791d342.png)

I want to test spring data can get document or not(implement ElasticsearchRepository), so I hard code ID like:  
`Optional<OriginalData> a = originalDataService.findById("nfQ8KHMBB49SfiIiH2rK");`

And then I got an exception:  
`org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-07-07T07:44:54.878Z'; nested exception is java.lang.IllegalArgumentException`

so I modify my Entity code to

```auto
    @Id
    private String id;

// @Field(name = "@timestamp", type = FieldType.Date)
// private Date timestamp;

    @Field(name = "netflow.first_switched", type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    private LocalDateTime firstSwitched;

    @Field(name = "netflow.last_switched", type = FieldType.Date, format = DateFormat.custom, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
    private LocalDateTime lastSwitched;

```

still wrong, but got another exception:  
`org.springframework.data.elasticsearch.core.convert.ConversionException: could not create object of class java.time.LocalDateTime`

My questions are:

1. How to use spring data get elasticsearch document with field containing data type?
2. I want to get field "@timestamp", can I get this?

Does anyone can help me, please?  
Kase

---

<div class="post-metadata">

**Author:** ![kase](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@kase](https://discuss.elastic.co/u/kase)\
**Post date:** [July 10, 2020, 7:04am UTC](https://discuss.elastic.co/t/how-to-save-elasticsearch-date-type-in-spring-data/240249/2 "2020-07-10T07:04:06Z")

</div>

Didn't solve this problem..., I use [Java High Level REST Client](https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html) instead of old way.  
And faced another problem, I want to ask how to use rest client to get documents with matching multiple fields?

I have tried

```auto
public SearchHits findSymmetry(String srcAddr, String dstAddr, Long srcPort, Long dstPort, String ipProtocol, long tos, String id) {
    MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.termsQuery("flow.src_addr", srcAddr));
    searchRequest.source(searchSourceBuilder);
    multiSearchRequest.add(searchRequest);
    searchSourceBuilder.query(QueryBuilders.termsQuery("flow.dst_addr", dstAddr));
    searchRequest.source(searchSourceBuilder);
    multiSearchRequest.add(searchRequest);
    try {
        MultiSearchResponse response = client.msearch(multiSearchRequest,RequestOptions.DEFAULT);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

```

When I build my project, IDE shown me error

```auto
Error:(87, 48) java: reference to termsQuery is ambiguous
  both method termsQuery(java.lang.String,double...) in org.elasticsearch.index.query.QueryBuilders and method termsQuery(java.lang.String,java.lang.Object...) in org.elasticsearch.index.query.QueryBuilders match
Error:(90, 48) java: reference to termsQuery is ambiguous
  both method termsQuery(java.lang.String,double...) in org.elasticsearch.index.query.QueryBuilders and method termsQuery(java.lang.String,java.lang.Object...) in org.elasticsearch.index.query.QueryBuilders match
Error:(96, 48) java: reference to termsQuery is ambiguous
  both method termsQuery(java.lang.String,double...) in org.elasticsearch.index.query.QueryBuilders and method termsQuery(java.lang.String,java.lang.Object...) in org.elasticsearch.index.query.QueryBuilders match

```

Is that mean I could only search limit two fields?  
Does anyone can help me, please?  
Kase

---

<div class="post-metadata">

**Author:** ![kase](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@kase](https://discuss.elastic.co/u/kase)\
**Post date:** [July 10, 2020, 8:08am UTC](https://discuss.elastic.co/t/how-to-save-elasticsearch-date-type-in-spring-data/240249/3 "2020-07-10T08:08:03Z")

</div>

I found [answer](https://stackoverflow.com/questions/45528572/java-method-call-is-ambiguous).

```auto
    public SearchHits findSymmetry(String srcAddr, String dstAddr, long srcPort, long dstPort, String ipProtocol, long tos, String id) {
        BoolQueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.termsQuery("flow.src_addr", srcAddr))
                .must(QueryBuilders.termsQuery("flow.dst_addr", dstAddr))
                .must(longTermsQuery("flow.src_port", srcPort))
                .must(longTermsQuery("flow.dst_port", dstPort))
                .must(QueryBuilders.termsQuery("flow.ip_protocol", ipProtocol))
                .must(longTermsQuery("flow.tos", tos));
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(query);
        searchRequest.source(sourceBuilder);
        try {
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            return searchResponse.getHits();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static TermsQueryBuilder longTermsQuery(String name, long... values) {
        return new TermsQueryBuilder(name, values);
    }

```

Thank you for following people!

---

<div class="post-metadata">

**Author:** ![Vinayak\_Sapre](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vinayak_sapre/32/45939_2.png) [@Vinayak\_Sapre](https://discuss.elastic.co/u/Vinayak_Sapre)\
**Post date:** [July 10, 2020, 8:11am UTC](https://discuss.elastic.co/t/how-to-save-elasticsearch-date-type-in-spring-data/240249/4 "2020-07-10T08:11:28Z")

</div>

@kase  
MultiSearchRequest is used batch multiple independent queries. You want to match document where both src\_addr and dst\_addr match to respective values.

Not tested but following should give you the idea.

```auto
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(
                QueryBuilders.boolQuery()
                .filter(QueryBuilders.termsQuery("flow.src_addr", srcAddr))
                .filter(QueryBuilders.termsQuery("flow.dst_addr", destAddr)));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

```

this is equivalent of ( (flow.src\_addr = srcAddr) AND (flow.dst\_addr = destAddr) ). If you need OR instead of AND, replace "filter" by "should"

---

<div class="post-metadata">

**Author:** ![kase](https://avatars.discourse-cdn.com/v4/letter/k/a5b964/32.png) [@kase](https://discuss.elastic.co/u/kase)\
**Post date:** [July 10, 2020, 10:30am UTC](https://discuss.elastic.co/t/how-to-save-elasticsearch-date-type-in-spring-data/240249/5 "2020-07-10T10:30:54Z")

</div>

Hi @Vinayak_Sapre, thanks for your answering, problem has solved!

---

<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:** [August 7, 2020, 10:30am UTC](https://discuss.elastic.co/t/how-to-save-elasticsearch-date-type-in-spring-data/240249/6 "2020-08-07T10:30:58Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
