[英]How to Index & search the Datetime field in Lucene.NET?
I don't know how to index and Search the Registred_Date(It contains sql format datetime).I need to search between years or days.where i'm using boolean query for the search.The code below is used for numeric field and ordinary field indexing. 我不知道如何索引和搜索Registred_Date(它包含sql格式的datetime)。我需要在几年或几天之间搜索。我正在使用布尔查询进行搜索。下面的代码用于数字字段和普通现场索引。
IndexWriter indexWriter = new IndexWriter(dir, new StandardAnalyzer(),Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
DataSet ds = new DataSet();
//ds contains table
if (ds.Tables[0] != null)
{
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
//Create the Document object
Document doc = new Document();
foreach (DataColumn dc in dt.Columns)
{
string check = dc.ToString();
if (check.Equals("Experience"))
{
int n=Convert.ToInt32(dr[dc.ColumnName]);
NumericField numericField = new NumericField(dc.ColumnName, Field.Store.YES, true);
numericField.SetIntValue(n);
doc.Add(numericField);
}
else if(check.Equals("Registred_Date"))
{
}
else
{
doc.Add(new Field(dc.ColumnName, dr[dc.ColumnName].ToString(), Field.Store.YES, Field.Index.ANALYZED));
}
//Populate the document with the column name and value from our query
}
// Write the Document to the catalog
indexWriter.AddDocument(doc);
}
}
}
// Close the writer
indexWriter.Close();
Thanks @Thomas CG de Vilhena and Mihai Soloi. 谢谢@Thomas CG de Vilhena和Mihai Soloi。 I found the solution with your help. 我在你的帮助下找到了解决方案。
For indexing: 索引:
DateTime d1 = Convert.ToDateTime(dr[dc.ColumnName]);
doc.Add(new Field("Registered_Date", DateTools.DateToString(d1, DateTools.Resolution.SECOND), Field.Store.YES, Field.Index.ANALYZED));
For searching: 搜索:
DateTime d1 = DateTime.Now.AddDays(-15);
var dateValue = DateTools.DateToString(d1, DateTools.Resolution.MILLISECOND);
var filter = FieldCacheRangeFilter.NewStringRange("Registered_Date",lowerVal: dateValue, includeLower: true,upperVal: null, includeUpper: false);
If you would store your index as a standard string, for example if you would transform from 2013-07-05 20:00:00
into 20130705200000
you can use the Lucene RangeQuery to search over the ranges. 如果要将索引存储为标准字符串,例如,如果要从2013-07-05 20:00:00
转换为20130705200000
,则可以使用Lucene RangeQuery搜索范围。
Sorry I didn't provide any example code, but I am not familiar with the .NET API. 对不起,我没有提供任何示例代码,但我不熟悉.NET API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.