C# 如何索引&;在Lucene.NET中搜索Datetime字段?

C# 如何索引&;在Lucene.NET中搜索Datetime字段?,c#,lucene,lucene.net,C#,Lucene,Lucene.net,我不知道如何索引和搜索已注册的_日期(它包含sql格式的datetime)。我需要在年或天之间进行搜索。我使用布尔查询进行搜索。下面的代码用于数字字段和普通字段索引 IndexWriter indexWriter = new IndexWriter(dir, new StandardAnalyzer(),Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED); DataSet ds = new DataSet()

我不知道如何索引和搜索已注册的_日期(它包含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();

如果将索引存储为标准字符串,例如,如果将
2013-07-05 20:00:00
转换为
20130705200000
,则可以使用Lucene搜索范围


很抱歉,我没有提供任何示例代码,但我不熟悉.NET API。

谢谢@Thomas C.G.de Vilhena和Mihai Soloi。在你的帮助下,我找到了解决办法

对于索引:

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));
用于搜索:

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);

检查:为什么不在索引上使用第二个分辨率?你可以在同一天有多个注册,我假设你过滤毫秒分辨率;两个决议应该是相同的