public Document CreateLuceneDocFromPath(string fullName, long lastWriteTimeInMs) { // make a new, empty document Document doc = new Document(); // Add the path of the file as a field named "path". Use a field that is // indexed (i.e. searchable), but don't tokenize the field into words. doc.Add(new Field("path", fullName, Field.Store.YES, Field.Index.NOT_ANALYZED)); // Add the last modified date of the file a field named "modified". Use // a field that is indexed (i.e. searchable), but don't tokenize the field // into words. doc.Add(new Field("modified", DateTools.TimeToString(lastWriteTimeInMs, DateTools.Resolution.MINUTE), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("path2", fullName, Field.Store.YES, Field.Index.ANALYZED)); // Add the contents of the file to a field named "contents". Specify a Reader, // so that the text of the file is tokenized and indexed, but not stored. // Note that FileReader expects the file to be in the system's default encoding. // If that's not the case searching for special characters will fail. try { System.IO.StreamReader io = new System.IO.StreamReader(fullName, System.Text.Encoding.Default); doc.Add(new Field("contents", io)); } catch (System.IO.IOException e) { } // return the document return(doc); }
public virtual void TestBefore() { // create an index RAMDirectory indexStore = new RAMDirectory(); IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED, null); long now = (DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond); Document doc = new Document(); // add time that is in the past doc.Add(new Field("datefield", DateTools.TimeToString(now - 1000, DateTools.Resolution.MILLISECOND), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("body", "Today is a very sunny day in New York City", Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(doc, null); writer.Optimize(null); writer.Close(); IndexSearcher searcher = new IndexSearcher(indexStore, true, null); // filter that should preserve matches //DateFilter df1 = DateFilter.Before("datefield", now); TermRangeFilter df1 = new TermRangeFilter("datefield", DateTools.TimeToString(now - 2000, DateTools.Resolution.MILLISECOND), DateTools.TimeToString(now, DateTools.Resolution.MILLISECOND), false, true); // filter that should discard matches //DateFilter df2 = DateFilter.Before("datefield", now - 999999); TermRangeFilter df2 = new TermRangeFilter("datefield", DateTools.TimeToString(0, DateTools.Resolution.MILLISECOND), DateTools.TimeToString(now - 2000, DateTools.Resolution.MILLISECOND), true, false); // search something that doesn't exist with DateFilter Query query1 = new TermQuery(new Term("body", "NoMatchForThis")); // search for something that does exists Query query2 = new TermQuery(new Term("body", "sunny")); ScoreDoc[] result; // ensure that queries return expected results without DateFilter first result = searcher.Search(query1, null, 1000, null).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query2, null, 1000, null).ScoreDocs; Assert.AreEqual(1, result.Length); // run queries with DateFilter result = searcher.Search(query1, df1, 1000, null).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query1, df2, 1000, null).ScoreDocs; Assert.AreEqual(0, result.Length); result = searcher.Search(query2, df1, 1000, null).ScoreDocs; Assert.AreEqual(1, result.Length); result = searcher.Search(query2, df2, 1000, null).ScoreDocs; Assert.AreEqual(0, result.Length); }
public Document CreateLuceneDocFromPath(string fullName, long lastWriteTimeInMs) { Document doc = new Document(); doc.Add(new Field("path", fullName, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("modified", DateTools.TimeToString(lastWriteTimeInMs, DateTools.Resolution.MINUTE), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("path2", fullName, Field.Store.YES, Field.Index.ANALYZED)); try { doc.Add(new Field("contents", new XMLTokenStream(fullName))); } catch (System.IO.IOException e) { } return(doc); }
public Document CreateLuceneDocFromPath(string fullName, long lastWriteTimeInMs) { Document doc = new Document(); doc.Add(new Field("path", fullName, Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("modified", DateTools.TimeToString(lastWriteTimeInMs, DateTools.Resolution.MINUTE), Field.Store.YES, Field.Index.NOT_ANALYZED)); doc.Add(new Field("path2", fullName, Field.Store.YES, Field.Index.ANALYZED)); try { System.IO.StreamReader io = new System.IO.StreamReader(fullName, System.Text.Encoding.Default); doc.Add(new Field("contents", new CodeTokenizer(io))); } catch (System.IO.IOException e) { } return(doc); }
private static Document CreateDocument(System.String text, long time) { Document document = new Document(); // Add the text field. Field textField = new Field(TEXT_FIELD, text, Field.Store.YES, Field.Index.ANALYZED); document.Add(textField); // Add the date/time field. System.String dateTimeString = DateTools.TimeToString(time, DateTools.Resolution.SECOND); Field dateTimeField = new Field(DATE_TIME_FIELD, dateTimeString, Field.Store.YES, Field.Index.NOT_ANALYZED); document.Add(dateTimeField); return(document); }
/// <summary>Makes a document for a File. /// <p> /// The document has three fields: /// <ul> /// <li><code>path</code>--containing the pathname of the file, as a stored, /// untokenized field; /// <li><code>modified</code>--containing the last modified date of the file as /// a field as created by <a /// href="lucene.document.DateTools.html">DateTools</a>; and /// <li><code>contents</code>--containing the full contents of the file, as a /// Reader field; /// </summary> public static Document Document(string fullName, UInt64 lastWriteTime) { // make a new, empty document Document doc = new Document(); // Add the path of the file as a field named "path". Use a field that is // indexed (i.e. searchable), but don't tokenize the field into words. doc.Add(new Field("path", fullName, Field.Store.YES, Field.Index.ANALYZED)); // Add the last modified date of the file a field named "modified". Use // a field that is indexed (i.e. searchable), but don't tokenize the field // into words. doc.Add(new Field("modified", DateTools.TimeToString((long)lastWriteTime, DateTools.Resolution.MINUTE), Field.Store.YES, Field.Index.NOT_ANALYZED)); if (System.IO.Path.GetExtension(fullName).Equals(".bab", StringComparison.OrdinalIgnoreCase)) { try { doc.Add(new Field("contents", new XMLTokenStream(fullName))); } catch (System.IO.IOException /* e*/) { } } else { // Add the contents of the file to a field named "contents". Specify a Reader, // so that the text of the file is tokenized and indexed, but not stored. // Note that FileReader expects the file to be in the system's default encoding. // If that's not the case searching for special characters will fail. try { System.IO.StreamReader io = new System.IO.StreamReader(fullName, System.Text.Encoding.Default); doc.Add(new Field("contents", io)); } catch (System.IO.IOException e) { } } // return the document return(doc); }
/// <summary>Makes a document for a File. /// <p> /// The document has three fields: /// <ul> /// <li><code>path</code>--containing the pathname of the file, as a stored, /// untokenized field; /// <li><code>modified</code>--containing the last modified date of the file as /// a field as created by <a /// href="lucene.document.DateTools.html">DateTools</a>; and /// <li><code>contents</code>--containing the full contents of the file, as a /// Reader field; /// </summary> public static Document Document(System.IO.FileInfo f) { // make a new, empty document Document doc = new Document(); // Add the path of the file as a field named "path". Use a field that is // indexed (i.e. searchable), but don't tokenize the field into words. doc.Add(new Field("path", f.FullName, Field.Store.YES, Field.Index.NOT_ANALYZED)); // Add the last modified date of the file a field named "modified". Use // a field that is indexed (i.e. searchable), but don't tokenize the field // into words. doc.Add(new Field("modified", DateTools.TimeToString(f.LastWriteTime.Millisecond, DateTools.Resolution.MINUTE), Field.Store.YES, Field.Index.NOT_ANALYZED)); // Add the contents of the file to a field named "contents". Specify a Reader, // so that the text of the file is tokenized and indexed, but not stored. // Note that FileReader expects the file to be in the system's default encoding. // If that's not the case searching for special characters will fail. doc.Add(new Field("contents", new System.IO.StreamReader(f.FullName, System.Text.Encoding.Default))); // return the document return(doc); }
/// <summary> Sets the date resolution used by RangeQueries for a specific field. /// /// </summary> /// <param name="fieldName">field for which the date resolution is to be set /// </param> /// <param name="dateResolution">date resolution to set /// </param> public virtual void SetDateResolution(System.String fieldName, DateTools.Resolution dateResolution) { if (fieldName == null) { throw new System.ArgumentException("Field cannot be null."); } if (fieldToDateResolution == null) { // lazily initialize HashMap fieldToDateResolution = new System.Collections.Hashtable(); } fieldToDateResolution[fieldName] = dateResolution; }
/// <summary> Sets the default date resolution used by RangeQueries for fields for which no /// specific date resolutions has been set. Field specific resolutions can be set /// with {@link #SetDateResolution(String, DateTools.Resolution)}. /// /// </summary> /// <param name="dateResolution">the default date resolution to set /// </param> public virtual void SetDateResolution(DateTools.Resolution dateResolution) { this.dateResolution = dateResolution; }
// Just to generate some different Lucene Date strings public /*private*/ System.String GetLuceneDate() { return(DateTools.TimeToString((base_Renamed.Ticks / TimeSpan.TicksPerMillisecond) + random.Next() - System.Int32.MinValue, DateTools.Resolution.DAY)); }
/// <summary> Sets the date resolution used by RangeQueries for a specific field. /// /// </summary> /// <param name="fieldName">field for which the date resolution is to be set /// </param> /// <param name="dateResolution">date resolution to set /// </param> public virtual void SetDateResolution(String fieldName, DateTools.Resolution dateResolution) { if (fieldName == null) { throw new ArgumentException("Field cannot be null."); } if (fieldToDateResolution == null) { // lazily initialize HashMap fieldToDateResolution = new HashMap<String, DateTools.Resolution>(); } fieldToDateResolution.Add(fieldName, dateResolution); }
public virtual void AssertDateRangeQueryEquals(QueryParser qp, System.String field, System.String startDate, System.String endDate, System.DateTime endDateInclusive, DateTools.Resolution resolution) { AssertQueryEquals(qp, field, field + ":[" + EscapeDateString(startDate) + " TO " + EscapeDateString(endDate) + "]", "[" + GetDate(startDate, resolution) + " TO " + GetDate(endDateInclusive, resolution) + "]"); AssertQueryEquals(qp, field, field + ":{" + EscapeDateString(startDate) + " TO " + EscapeDateString(endDate) + "}", "{" + GetDate(startDate, resolution) + " TO " + GetDate(endDate, resolution) + "}"); }
/// <summary>for testing DateTools support </summary> private System.String GetDate(System.DateTime d, DateTools.Resolution resolution) { if (resolution == null) { return DateField.DateToString(d); } else { return DateTools.DateToString(d, resolution); } }
/// <summary>for testing DateTools support </summary> private System.String GetDate(System.String s, DateTools.Resolution resolution) { System.DateTime tempAux = System.DateTime.Parse(s, System.Globalization.CultureInfo.CurrentCulture); return GetDate(tempAux, resolution); }