2013-07-06 13 views
6

私はどのようにインデックスを作成し、Registred_Dateを検索するのかわかりません(これはsql形式のdatetimeを含んでいます)。私は年または日の間に検索する必要があります。私は検索のためにブールクエリを使用しています。通常のフィールドインデクシング。Lucene.NETの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(); 
+1

チェック.com/questions/4565303/lucene-net-how-can-i-add-a-date-filter-to-my-search-results?answertab = votes#tab-top) –

答えて

7

ありがとう@トーマス・C・G・ド・ヴィレナとミハイ・ソロイ。あなたの助けを借りて解決策を見つけました。索引付けのため

:検索するための

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

:[Lucene.Net::どのように私は私の検索結果に日付フィルタを追加することができますか?](のhttp:// stackoverflowのこの

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

索引付け? MILLISECONDの解像度でフィルタリングしているときに同じ日に複数の登録を行うことができます。両方の解像度は同じでなければなりません –

+0

私は気づいていない、感謝@Mihaiソロイ私の時間を節約します。 –

1

あなたが範囲に亘って検索するLuceneのRangeQueryを使用することができます201307052000002013-07-05 20:00:00から変換しまう場合には、例えば、標準の文字列として、あなたのインデックスを格納たい場合。

申し訳ありませんが、サンプルコードは提供していませんでしたが、.NET APIに精通していません。

関連する問題