c# - 如何索引和搜索 Lucene.NET 中的日期时间字段?

标签 c# lucene lucene.net

我不知道如何索引和搜索 Registred_Date(它包含 sql 格式的日期时间)。我需要在年或天之间进行搜索。我在其中使用 bool 查询进行搜索。下面的代码用于数字字段和普通字段索引。

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

最佳答案

感谢@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);

关于c# - 如何索引和搜索 Lucene.NET 中的日期时间字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17503119/

相关文章:

c# - 为什么我们不能在 .Net 中创建静态类的实例

c# - 使用 C# wpf 更改周末日期

java - 安装 Lucene

c# - Lucene.Net 无法在空字符串列表上使用 RavenDB 'IN' 运算符解析查询

lucene.net - 如何使用新的Sitecore.Search API进行排序/过滤

c# - 在 Windows 商店应用程序中下载文件

c# - ASP.NET 5 MVC 6 通用存储库模式

json - Elasticsearch中的AND查询

solr - 如果WildcardQuery不影响文档的评分,为什么它会不断返回0.5?

search - 使用 Lucene.NET 在通配符搜索中转义特殊字符