I understand how Lucene.net can work for text indexing. Will I be able to efficiently search for documents based on a given date range? Or will Lucene.net just use text matching to match the dates?
Lucene.Net will just use text matching, so you'd need to format the dates correctly before adding to the index:
public static string Serialize(DateTime dateTime)
{
return dateTime.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture);
}
public static DateTime Deserialize(string str)
{
return DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
}
You can then, for example, perform a range based query to filter by date (e.g. 2006* to 2007* to include all dates in 2006 and 2007).
I went in to trouble when i converted date in to yyyymmddHHmmssff. When i tried sorting the data, it gave me an exception that too big to convert..something. Hence i search and found then you need to have two columns. one in yyyymmdd and the other HHmmss, and then use Sort[] and give these two columns and then use. This will solve the issue.
Related
Good day!
In my document, I have a date field, which contains ISO-8601 date, which can also be a period like "25-08-2016/P1D"
I want to search a document having exactly this date or period - so ,just having a same value of "date" field. Unfortunately, I was unable to do this. Tried different query strings, with escaping or without, like
date:"25-08-2016/P1D" - is transformed to PhraseQuery, which fails with
exception, AFAIUI since a field is just a String field
date:"25-08-2016/P1D" - same as above
date:25-08-2016/P1D -
does not fail with exception, but still PhraseQuery is created, and
nothing is being found
date:25-08-2016/P1D - parsing fails with "org.apache.lucene.queryparser.classic.TokenMgrError: Lexical error at line 1, column 150. Encountered: after : "/P1D"
What I'm doing wrong? How can I tell Lucene to search this field using simple string match, without any tokenization?
After some research I've got, that escaping query string is a wrong way - correct way to achieve this is customizing Query analyzer for necessary field ("date" in my case).
Map<String, Analyzer> analyzerPerField = new HashMap<String, Analyzer>();
analyzerPerField.put("date", new WhitespaceAnalyzer());
analyzer = new PerFieldAnalyzerWrapper(
new StandardAnalyzer(), analyzerPerField);
parser = new QueryParser("title", analyzer);
In given code, we just use WhitespaceAnalyzer (which is dividing query by whitespaces) instead of SimpleAnalyzer used by default, which is dividing text at non-letters. WhitespaceAnalyzer does not break ISO-8601 date.
For additional details about custom analyzing/tokenizing in lucene, please refer e.g. http://www.hascode.com/2014/07/lucene-by-example-specifying-analyzers-on-a-per-field-basis-and-writing-a-custom-analyzertokenizer/
Is possible change format date by specific pattern ? I need to made a function which has a two parameters. First is date and second is pattern. I need convert more date variants. Goal this function is change US and European date format.
For example i need convert
EU: dd:MM:yyyy hh:mm:ss
to
US: MM:dd:yyyy hh:mm:ss
On another page i need change
EU: dd/MM/yyyy
to
US: MM/dd/yyyy
And i have a several next variant to convert
And i want to made a similar function
Formater(euDate, pattern)
BEGIN
....
RETURN usDate
My production server is unfortunately SQL server 2005 and doesn't support function FORMAT(). And function CONVERT() doesn't support some variant of date, which i need convert. So in my current solution i parse EU date at individualy parts (#day = day(#euDate), #month, #year, ...) and join them in new string . And i compare it with input parameter in pattern and return CASE which is equal like pattern. I want to this function make general and simplier.
Thank you for Your advice.
You almost certainly can use the convert function. You can read more about all the options here.
If there is some obscure invariant you need, check out this blog by Anubhav Goyal.
Is it possible to boost recent documents in a RavenDB query?
This question is exactly what I want to do but refers to native Lucene, not RavenDB.
For example, if I have a Document like this
public class Document
{
public string Title { get; set; }
public DateTime DateCreated { get; set; }
}
How can I boost documents who's date are closer to a given date, e.g. DateTime.UtcNow?
I do not want to OrderByDecending(x => x.DateCreated) as there are other search parameters that need to affect the results.
You can boost during indexing, it's been in RavenDB for quite some time, but it's not in the documentation at all. However, there are some unit tests that illustrate here.
Those tests show a single boost value, but it can easily be calculated from other document values instead. You have the full document available to you since this is done when the index entries are written. You should be able to combine this with the technique described in the post you referenced.
Map = docs => from doc in docs
select new
{
Title = doc.Title.Boost(doc.DateCreated.Ticks / 1000000f)
};
You could also boost the entire document instead of just the Title field, which might be useful if you have other fields in your search algorithm:
Map = docs => from doc in docs
select new
{
doc.Title
}.Boost(doc.DateCreated.Ticks / 1000000f);
You may need to experiment with the right value to use for the boost amount. There are 10,000 ticks in a millisecond, so that's why i divide by such a large number.
Also, be careful that the DateTime you're working with is in UTC, or if you don't have control over where it comes from, then use a DateTimeOffset instead. Why? Because you're using a calculated duration from some reference point and you don't want the result to be ambiguous for different time zones or around daylight savings time changes.
I am using these codes for displaying time in VB.NET
it shows up in 24 hours format besides i need it in 12 hours format
System.DateTime.Now.Hour
System.DateTime.Now.Minute
System.DateTime.Now.Second
example:
14:12:42
I need it as :
02:12:42
thanks.
Use String.Format. For example:
String.Format("{0:T}", System.DateTime.Now) //02:12:42 PM
String.Format("{0:hh:mm:ss}", System.DateTime.Now) //02:12:42
String.Format("{0:hh:mm:ss tt}", System.DateTime.Now) //02:12:42 PM
Also, this website to be very helpful in summarizing the various ways you can use String.Format. Keep in mind the culture can make a difference on non-custom formats. The first example above using T (Long Time format) works on my US-based PC just fine. But if you say:
String.Format(System.Globalization.CultureInfo.InvariantCulture, _
"{0:T}", System.DateTime.Now)
You end up with 14:12:42. The latter two examples are custom formats and are not affected by culture.
When using DateTime objects you can actually use the ToString() method and set your format inside it.
string currentTime = System.DateTime.Now.ToString("hh:mm:ss");
Check this msdn article out for more clarity:
http://msdn.microsoft.com/en-us/library/zdtaw1bw.aspx
Use the appropriate format string for display.
string formatted = myDateTime.ToString("hh:mm:ss");
I have used a custom format string in this case.
1-Use regex to get first two characters of that string ie from 23:11:59 get 23
2-convert this number to integer type
3-now check it if it is not greater than 12 and if it is subtract 12 from it and by using string.replace replace the old value.
Try This...
Dim CurTime As String
CurTime = TimeOfDay.ToString("h:mm:ss tt")
I would like to control a string if is in the dd/mm/yyyy format and if the dd number is between 1 and 31 and if mm is between 1 and 12.
In vb.net you can use the IsDate() function to test the validity of a date. This will insure that the day and the month are within the valid range.
You can use the DatePart function:
DatePart("m", date)
DatePart("d", date)
Wait what? Your question is not very clear. Do you have a DateTime and need to output it in a specific format? Are you accepting a string from the user and need to make sure it fits that format? Do you get a string from somewhere else that you need to match for a specific format?
Most of all, why do you care? You shouldn't be dealing with dates as strings, except at the point of interaction with the user or other data source. Inside your program they should be a DateTime type. Assuming you're 'vb.net' tag is correct, the DateTime has handy Parse, TryParse, and ParseExact, and TryParseExact static methods you can use to accept most anything the user could throw at you.