I am using Compass to make queries on data inside in memory data structure. It works fine for searching String and enum values, now I want to search dates.
Search criteria are annotated by #SearchRestriction annotation. Example about someDate:
#SearchRestriction(path="fooBar.someDate" type = SearchRestrictionType.EQUAL)
String someDate;
At searchable data SomeDate is annotated like the following:
#SearchableProperty
Date someDate;
SomeDate inside the searchable data is generated with new Date();) and query String is given as 20120802.
Situation on debugger:
This code generates queries like this:
someDate:20120802
Here someDate is the name of the field I am looking for and 20120802 is a date in order yyyyMMdd.
Problem:
No results is returned, when this query is run. I get an empty list. The Date in query is the same as in the Date object.
What is wrong??
Is this wrong way to search Dates with Compass? I can find only range queries about Date, but a search with exact Date or part of exact Date I cannot find.
You need to specify the format for Searchable property [Date]
#SearchableProperty(format = "yyyyMMdd")
To some extent, it relates to Grails: Lucene, Compass Query Builder and date ranges
Related
I have an entity named A, which has a createdDate with this type #Temporal(TemporalType.TIMESTAMP).
I tried to retrieve all the rows having a certain createdDate. I attached a certain date to the request and I printed that date, it looks fine like "2021-02-11 12:14:02.425", which has all the values up to millisecond.
But the sql from hibernate, the value for createdDate in the where clause is set as '11-Feb-21'. Therefore, I do not find any rows because the createdDate is saved as 2021-02-11 12:14:02.425 in the db.
public Response getByCreatedDate(Request req) {
List<AResponse> aList = aRepository.findByIdAndCreatedDate(req.getId(), req.getCreatedDate());
}
I am new for Hibernate, I tried find some useful information about it but could not. Do I have to explicitly create a new Date with those specific date and time and send it to the method 'findByIdAndCreatedDate'? If anyone has the same experience, could you give some information about it?
I got document that looks like this
#Document(collection="myDocument")
public class MyDocument {
#Id
private String id;
private List<Dates> dates;
}
public class Dates{
private String key;
private DateTime value;
}
And OtherDocument is container for DateTime values from various sources, I can't simply make fields like DateTime birthdate; inside MyDocument because I don't know what key will be, they are just some dates that describe MyDocument. Now, I need to create search engine for those values, for example, someone want's to find all MyDocuments with dates that contains:
key : "Birthdate" greater than
value : "1990-01-01 00:00:00 +00:00"
and key : "Mather's birthday" less than
value: "1975-01-01 00:00:00 +00:00"
So, Criteria (using MongoTemplate here) first may look like this
Criteria criteria = Criteria.where("myDocument.dates.value")
.exists(true)
.gt(DateTimeUtil.valueOf("1990-01-01 00:00:00 +00:00")) //just converting String to DateTime here
.and("myDocument.dates.name")
.exists(true)
.all("Birthday"));
And second one:
Criteria criteria = Criteria.where("myDocument.dates.value")
.exists(true)
.lt(DateTimeUtil.valueOf("1975-01-01 00:00:00 +00:00"))
.and("myDocument.dates.name")
.exists(true)
.all("Mather's birthday"));
The problem is, I can't put those both Criteria in one Query, it will cause error. The only soultion I found till now is to make 2 separate Query in that case and then find common part by using
resultA.retainAll(resultB)
But the point is, I don't want to, this database will store a lot of data and those requests will be very frequent. I need this to work fast, and combining 2 lists in pure Java will be slow as hell with that amount of data. Any ideas how to deal with that?
edit#
here is the error thrown when I try to combine 2 Criteria like this in one Query
caught: (java.lang.RuntimeException), msg(json can't serialize type :
class org.joda.time.DateTime) java.lang.RuntimeException: json can't
serialize type : class org.joda.time.DateTime
You can use below code. $and the query together and use $elemMatch to match the dates fields on multiple condition.
Something like
Criteria criteria1 = Criteria.where("dates").
elemMatch(
Criteria.where("value").exists(true).gt(DateTimeUtil.valueOf("1990-01-01 00:00:00 +00:00"))
.and("name").exists(true).all("Birthday")
);
Criteria criteria2 = Criteria.where("dates").
elemMatch(
Criteria.where("value").exists(true).lt(DateTimeUtil.valueOf("1975-01-01 00:00:00 +00:00"))
.and("name").exists(true).all("Mather's birthday")
);
Criteria criteria = new Criteria().andOperator(criteria1, criteria2);
Note: You may still have the problem with joda time conversion.
I have a database that has the date as String and I have to delete the rows that have the same data in the csv file as in the database. More exactly, my dates look like this 2018-03-31T23:30:24+00:00. I want that when it gets a date like this, to delete from database where data LIKE %2018-03-31%, so it will delete all the records from that day, even if the time is not the same.
I have a job where tFileInputDelimited is connected with a tSortRow and then to tFlowToIterate. After that, I have a tJava where I extract the date and then a tMysqlInput where the query has the where clause like this: WHERE purchase_date LIKE '%"+context.date+"%' . Then, with a run if connection, I have tMysqlRow in which I have the delete statement with the same where clause. After that, of course, I have the tMysqlCommit.
The context.date is made like this:
context.dataaa=(String)globalMap.get("row6.purchase_date");
context.month=context.dataaa.substring(5,7);
context.year=context.dataaa.substring(0,4);
context.day=context.dataaa.substring(8,10);
context.date=context.year+"-"+context.month+"-"+context.day;
The problem is that, it doesn't delete from database. I want it to go row by row and delete all my records that have the same day in the csv and database.
The problem was from an if statement that compared the full date separately, so I had to compare only the day not day,year and month.
In Talend, you can define the pattern of a Date column to some usage.
Here, I have a tFileInputDelimited with a column date :
Column: date
Type : Date
pattern : "yyyy-MM-dd'T'hh:mm:ss"
That let me read a file with a value like 2018-03-31T23:30:24 because here, the pattern is used to "parse" the String from the file into a Date. Now if we add a LogRow and update the schema of this component, we can define a different pattern to format the Date
Column: date
Type : Date
pattern : "yyyy-MM-dd"
Input : 2018-03-31T23:30:24
Output : 2018-03-31
Know, if you don't want to play with the Schema, you can convert/format a date into a String with TalendDate.formatDate("yyyy-MM-dd", row1.date)
When I query MongoRepository via Date field with Criteria in a Spring Boot application, the result is wrong. Here is my method:
Query query = new Query(new Criteria().andOperator(
Criteria.where("id").is(filter.getId()),
Criteria.where("datas.ts").lt(filter.getEndTime()).gte(filter.getStartTime())
));
List<PhaseData> phaseDatas = mongoOperations.find(query, PhaseData.class);
List<Data> result = new ArrayList<Data>();
for(Data pData : phaseDatas) {
result.addAll(pData.getDatas());
}
return result;
When I query with
{
"id" : "1234",
"startTime" : "2016-08-04 12:00",
"endTime" : "2016-08-04 15:00"
}
it gives me records with hour 16:54 & 21:12 too. How can I solve this issue?
Not sure if this addresses your question directly.
The DB won't return wrong result to the query. So I think it could be one of the following things:
It could be that the when you view the documents in mongodb, it displays date in iso format. So view the documents in the same format as you are creating dates for your query.
It could be timezone issue.
Mongodb dates can be considered as ISODate (MongoDB Date)
When you query, you create date objects in your timezone. So as a first debugging measure, I would see if both my DB and query timezones are the same.
Also, probably it would help if you query by creating date objects in ISODate by using SimpleDateFormat(SDF is not thread safe).
I have found that it could be confusing because the dates that you send are in a different format and the documents that you visually see in mongodb tool are displaying dates in iso format. I think that it could be the issue. The results are good, but probably you are viewing the two things differently and it causes the confusion.
I have an entity class with properties 'code','fromDate' and 'toDate' and i need to insert one new record using JPA such a way that for given code date range should not overlap.
For example
If code- ABC of date range 01/Feb/2014-10/Feb/2014 exist in DB.
I am inserting code ABC again with date range
03/Feb/2014-07/Feb/2014 should not accept - from date and to date is Within existing Date range
28/Jan/2014-02/Feb/2014 should not accept - to date is Within existing Date range
05/Feb/2014-21/Feb/2014 should not accept - From date is Within existing Date range
01/Jan/2014-28/Feb/2014 should not accept - The existing date range is within the given date range so Overlapping will happen.
Suppose the data need to be inserted is in a viewObject with similar properties.
Please help me to do the validation for date overlapping using JPA predicates
Before saving the new object you can query the DB to check if an 'overlapping' records exists.
If, a record is returned, then do not save the new object, else save;
String query = "SELECT ent FROM Entity ent WHERE ent.fromDate <= :toDate AND ent.toDate >= :fromDate WHERE ent.id = :entId";
List<Entity> overlappingRecords = JPA.em().createQuery(query).setParameter("entId", id).setParameter("fromDate", fromDate).setParameter("toDate", toDate).getResultList();
if(overlappingRecords.isEmpty())
//Over lap does not exist
else
//Over lap exists
This query assumes rejection of edges overlapping exactly.