Home > Software design >  Unable to parse 2022-10-04T19:24:50Z format in ElasticSearch Java Implemnetation
Unable to parse 2022-10-04T19:24:50Z format in ElasticSearch Java Implemnetation

Time:10-26

SearchRequest searchRequest = Requests.searchRequest(indexName);
SearchSourceBuilder builder = new SearchSourceBuilder();
Gson gson = new Gson();
QueryBuilder querybuilder = QueryBuilders.wrapperQuery(query);
query : {
    "range": {
        "timecolumn": {
            "gte":"2022-10-07T09:45:13Z",
            "lte":"2022-10-07T09:50:50Z"
        }
    }
}

While passing the above Query I am getting Parser Exception , I cannot change the date format as data in DB is getting inserted in same format .

Need Advice on :

  1. How can we parse this kind of timestamp in ElasticSearch Java , if not
  2. How can we control pattern updation during data insertion like my column in defined as text which takes date format "2022-10-07T09:45:13Z" as text .

either I have to pass this format in ES Parser or I have to change format to 2022-10-07 09:45:13 during insertion itself .

I cannot convert for each row after inserting because we have lakhs of data

CodePudding user response:

As you are mentioning, Elasticsearch storing timecolumn as text type hence i will suggest to change mapping of timecolumn to date type and you will be able to use range query with date. Because if you store date as text and applied range then it will not return a expected result.

{
  "mappings": {
    "properties": {
      "timecolumn": {
        "type": "date"
      }
    }
  }
}

Now coming to your Java code issue, You can use below code for creating range query in Java as you are using Java client.

SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder query = QueryBuilders.rangeQuery("timecolumn").gte("2022-10-07T09:45:13Z").lte("2022-10-07T09:50:50Z");
searchSourceBuilder.query(query);
searchRequest.source(searchSourceBuilder);

Regarding your concern about reindexing data:

I cannot convert for each row after inserting because we have lakhs of data

You can use Reindex API to move data from original index to temp index and delete your original index. Then, defined the index with proper mapping and again use same Reindex API to copy data from temp index to original index with new mapping.

  • Related