|
|
Hi :)
I'm starting to use Solr and I'm facing a little problem with dates. My documents have a date property which is of type 'yyyyMMdd'.
To index these dates, I use the following code:
String dateString = "20101230"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = sdf.parse(dateString); doc.addField("date", date);
In the index, the date "20101230" is saved as "2010-12-29T23:00:00Z" ( because of GMT).
Now I would like to query documents which have their date property equals to "20101230" but I don't know how to handle this.
I tried the following code :
String dateString = "20101230"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = sdf.parse(dateString); SimpleDateFormat gmtSdf = new SimpleDateFormat("yyyy-MM-dd'T'HH\\:mm\\:ss'Z'"); String gmtString = gmtSdf.format(Date);
The problem is that gmtString is equals to "2010-12-30T00\:00\:00Z". There is a difference between the index value and the parameter value of my query : /.
I see that there might be something to do with the timezones during the date to string and string to date conversion but I can't find it.
Thanks,
Gary
+
G.Long 2012-05-02, 15:18
-
Re: question about dates
Jack Krupansky 2012-05-02, 15:55
The trailing "Z" is required in your input data to be indexed, but the Z is not actually stored. Your query must have the trailing "Z" though, unless you are doing a wildcard or prefix query.
-- Jack Krupansky
-----Original Message----- From: G.Long Sent: Wednesday, May 02, 2012 11:18 AM To: [EMAIL PROTECTED] Subject: question about dates
Hi :)
I'm starting to use Solr and I'm facing a little problem with dates. My documents have a date property which is of type 'yyyyMMdd'.
To index these dates, I use the following code:
String dateString = "20101230"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = sdf.parse(dateString); doc.addField("date", date);
In the index, the date "20101230" is saved as "2010-12-29T23:00:00Z" ( because of GMT).
Now I would like to query documents which have their date property equals to "20101230" but I don't know how to handle this.
I tried the following code :
String dateString = "20101230"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = sdf.parse(dateString); SimpleDateFormat gmtSdf = new SimpleDateFormat("yyyy-MM-dd'T'HH\\:mm\\:ss'Z'"); String gmtString = gmtSdf.format(Date);
The problem is that gmtString is equals to "2010-12-30T00\:00\:00Z". There is a difference between the index value and the parameter value of my query : /.
I see that there might be something to do with the timezones during the date to string and string to date conversion but I can't find it.
Thanks,
Gary
+
Jack Krupansky 2012-05-02, 15:55
-
Re: question about dates
Jack Krupansky 2012-05-02, 15:59
Oops... I meant to say that Solr doesn't *index* the trailing Z, but it is "stored" (the stored value, not the indexed value.) The query must match the indexed value, not the stored value.
-- Jack Krupansky
-----Original Message----- From: Jack Krupansky Sent: Wednesday, May 02, 2012 11:55 AM To: [EMAIL PROTECTED] Subject: Re: question about dates
The trailing "Z" is required in your input data to be indexed, but the Z is not actually stored. Your query must have the trailing "Z" though, unless you are doing a wildcard or prefix query.
-- Jack Krupansky
-----Original Message----- From: G.Long Sent: Wednesday, May 02, 2012 11:18 AM To: [EMAIL PROTECTED] Subject: question about dates
Hi :)
I'm starting to use Solr and I'm facing a little problem with dates. My documents have a date property which is of type 'yyyyMMdd'.
To index these dates, I use the following code:
String dateString = "20101230"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = sdf.parse(dateString); doc.addField("date", date);
In the index, the date "20101230" is saved as "2010-12-29T23:00:00Z" ( because of GMT).
Now I would like to query documents which have their date property equals to "20101230" but I don't know how to handle this.
I tried the following code :
String dateString = "20101230"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = sdf.parse(dateString); SimpleDateFormat gmtSdf = new SimpleDateFormat("yyyy-MM-dd'T'HH\\:mm\\:ss'Z'"); String gmtString = gmtSdf.format(Date);
The problem is that gmtString is equals to "2010-12-30T00\:00\:00Z". There is a difference between the index value and the parameter value of my query : /.
I see that there might be something to do with the timezones during the date to string and string to date conversion but I can't find it.
Thanks,
Gary
+
Jack Krupansky 2012-05-02, 15:59
-
Re: question about dates
Jack Krupansky 2012-05-02, 16:10
That wasn't right either... the query must have the trailing Z, which Solr will strip off to match the indexed value which doesn't have the Z. So, my corrected original statement is:
The trailing "Z" is required in your input data to be indexed, but the Z is not actually indexed by Solr (it is stripped), although the stored value of the field, if any, would have the original value with the Z. Your query must have the trailing "Z" though (which Solr will strip off), unless you are doing a wildcard or prefix query.
Sorry about that.
-- Jack Krupansky
-----Original Message----- From: Jack Krupansky Sent: Wednesday, May 02, 2012 11:59 AM To: [EMAIL PROTECTED] Subject: Re: question about dates
Oops... I meant to say that Solr doesn't *index* the trailing Z, but it is "stored" (the stored value, not the indexed value.) The query must match the indexed value, not the stored value.
-- Jack Krupansky
-----Original Message----- From: Jack Krupansky Sent: Wednesday, May 02, 2012 11:55 AM To: [EMAIL PROTECTED] Subject: Re: question about dates
The trailing "Z" is required in your input data to be indexed, but the Z is not actually stored. Your query must have the trailing "Z" though, unless you are doing a wildcard or prefix query.
-- Jack Krupansky
-----Original Message----- From: G.Long Sent: Wednesday, May 02, 2012 11:18 AM To: [EMAIL PROTECTED] Subject: question about dates
Hi :)
I'm starting to use Solr and I'm facing a little problem with dates. My documents have a date property which is of type 'yyyyMMdd'.
To index these dates, I use the following code:
String dateString = "20101230"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = sdf.parse(dateString); doc.addField("date", date);
In the index, the date "20101230" is saved as "2010-12-29T23:00:00Z" ( because of GMT).
Now I would like to query documents which have their date property equals to "20101230" but I don't know how to handle this.
I tried the following code :
String dateString = "20101230"; SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date date = sdf.parse(dateString); SimpleDateFormat gmtSdf = new SimpleDateFormat("yyyy-MM-dd'T'HH\\:mm\\:ss'Z'"); String gmtString = gmtSdf.format(Date);
The problem is that gmtString is equals to "2010-12-30T00\:00\:00Z". There is a difference between the index value and the parameter value of my query : /.
I see that there might be something to do with the timezones during the date to string and string to date conversion but I can't find it.
Thanks,
Gary
+
Jack Krupansky 2012-05-02, 16:10
-
Re: question about dates
Chris Hostetter 2012-05-02, 19:26
: String dateString = "20101230"; : SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); : Date date = sdf.parse(dateString); : doc.addField("date", date); : : In the index, the date "20101230" is saved as "2010-12-29T23:00:00Z" ( because : of GMT).
"because of GMT" is missleading and vague ... what you get in your index is a value of "2010-12-29T23:00:00Z" because that is the canonical string representation of the date object you have passed to doc.addField -- the date object you have passed in represents that time, because you constructed a SimpleDateFormat object w/o specifying which TimeZone that SDF object should assume is in use when it parses it's string input. So when you give it the input "20101230" it treats that is Dec 30, 2010, 00:00:00.000 in whatever teh local timezone of your client is.
If you want it to treat that input string as a date expression in GMT, then you need to configure the parser to use GMT (SimpleDateFormat.setTimeZone)
: I tried the following code : : : String dateString = "20101230"; : SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); : Date date = sdf.parse(dateString); : SimpleDateFormat gmtSdf = new : SimpleDateFormat("yyyy-MM-dd'T'HH\\:mm\\:ss'Z'"); : String gmtString = gmtSdf.format(Date); : : The problem is that gmtString is equals to "2010-12-30T00\:00\:00Z". There is
again, that is not a "gmtString" .. in this case, both of the SDF objects you are using have not been configured with an explicit TimeZone, so they use whatever hte platform default is where this code is run -- so the variable you are calling "gmtString" is actaully a string representation of Date object formated in your local TimeZone.
Bottom line...
* when parsing a string into a Date, you really need to know (and be explicit to the parser) about what timezone is represented in that string (unless the formated of hte string includes the TimeZone)
* when building a query string to pass to solr, then the DateFormat you use to formate a Date object must format it using GMT -- there is a DateUtil class included in solrj to make this easier.
If you really don't care at all about TimeZones, then just use GMT everywhere .. but if you actually care about what time of day something happened, and want to be able to query for events with hour/min/sec/etc.. granularity, then you need to be precise about the TimeZone in every Formatter you use. -Hoss
+
Chris Hostetter 2012-05-02, 19:26
-
Re: question about dates
G.Long 2012-05-03, 12:43
Thank you for the tips :)
Gary
Le 02/05/2012 21:26, Chris Hostetter a �crit : > : String dateString = "20101230"; > : SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); > : Date date = sdf.parse(dateString); > : doc.addField("date", date); > : > : In the index, the date "20101230" is saved as "2010-12-29T23:00:00Z" ( because > : of GMT). > > "because of GMT" is missleading and vague ... what you get in your index > is a value of "2010-12-29T23:00:00Z" because that is the canonical > string representation of the date object you have passed to doc.addField > -- the date object you have passed in represents that time, because you > constructed a SimpleDateFormat object w/o specifying which TimeZone that > SDF object should assume is in use when it parses it's string input. So > when you give it the input "20101230" it treats that is Dec 30, 2010, > 00:00:00.000 in whatever teh local timezone of your client is. > > If you want it to treat that input string as a date expression in GMT, > then you need to configure the parser to use GMT > (SimpleDateFormat.setTimeZone) > > : I tried the following code : > : > : String dateString = "20101230"; > : SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); > : Date date = sdf.parse(dateString); > : SimpleDateFormat gmtSdf = new > : SimpleDateFormat("yyyy-MM-dd'T'HH\\:mm\\:ss'Z'"); > : String gmtString = gmtSdf.format(Date); > : > : The problem is that gmtString is equals to "2010-12-30T00\:00\:00Z". There is > > again, that is not a "gmtString" .. in this case, both of the SDF objects > you are using have not been configured with an explicit TimeZone, so they > use whatever hte platform default is where this code is run -- so the > variable you are calling "gmtString" is actaully a string representation > of Date object formated in your local TimeZone. > > Bottom line... > > * when parsing a string into a Date, you really need to know (and be > explicit to the parser) about what timezone is represented in that string > (unless the formated of hte string includes the TimeZone) > > * when building a query string to pass to solr, then the DateFormat > you use to formate a Date object must format it using GMT -- there is a > DateUtil class included in solrj to make this easier. > > If you really don't care at all about TimeZones, then just use GMT > everywhere .. but if you actually care about what time of day something > happened, and want to be able to query for events with hour/min/sec/etc.. > granularity, then you need to be precise about the TimeZone in every > Formatter you use. > > > -Hoss
+
G.Long 2012-05-03, 12:43
|
|