8

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.

       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();
Dinesh_Dini
  • 419
  • 8
  • 20
  • 1
    check this: [Lucene.Net: How can I add a date filter to my search results?](http://stackoverflow.com/questions/4565303/lucene-net-how-can-i-add-a-date-filter-to-my-search-results?answertab=votes#tab-top) – Thomas C. G. de Vilhena Jul 06 '13 at 14:01

2 Answers2

8

Thanks @Thomas C. G. de Vilhena and 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);
Andrew Barrett
  • 19,721
  • 4
  • 47
  • 52
Dinesh_Dini
  • 419
  • 8
  • 20
  • why not use the SECOND resolution on the indexing? you can have multiple registrations on the same day I assume as you are filtering over the MILLISECOND resolution; both resolutions should be the same imho – Mihai Soloi Jul 08 '13 at 08:38
1

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.

Sorry I didn't provide any example code, but I am not familiar with the .NET API.

Mihai Soloi
  • 373
  • 1
  • 12