44 lines
2.1 KiB
C#
44 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Lucene.Net.Analysis;
|
|
using Lucene.Net.Documents;
|
|
using Lucene.Net.QueryParsers;
|
|
using Lucene.Net.Search;
|
|
|
|
namespace LibationSearchEngine
|
|
{
|
|
// field names are case specific and, due to StandardAnalyzer, content is case INspecific
|
|
public static class LuceneExtensions
|
|
{
|
|
public static void AddRaw(this Document document, string name, string value)
|
|
=> document.Add(new Field(name, value, Field.Store.YES, Field.Index.NOT_ANALYZED));
|
|
|
|
public static void AddAnalyzed(this Document document, string name, string value)
|
|
{
|
|
if (value != null)
|
|
document.Add(new Field(name.ToLowerInvariant(), value, Field.Store.YES, Field.Index.ANALYZED));
|
|
}
|
|
|
|
public static void AddNotAnalyzed(this Document document, string name, string value)
|
|
=> document.Add(new Field(name.ToLowerInvariant(), value, Field.Store.YES, Field.Index.NOT_ANALYZED));
|
|
|
|
public static void AddBool(this Document document, string name, bool value)
|
|
=> document.Add(new Field(name.ToLowerInvariant(), value.ToString(), Field.Store.YES, Field.Index.ANALYZED_NO_NORMS));
|
|
|
|
public static Query GetQuery(this Analyzer analyzer, string defaultField, string searchString)
|
|
=> new QueryParser(SearchEngine.Version, defaultField.ToLowerInvariant(), analyzer).Parse(searchString);
|
|
|
|
// put all numbers, including dates, into this format:
|
|
// ########.##
|
|
internal const int PAD_DIGITS = 8;
|
|
internal const string DECIMAL_PRECISION = ".00";
|
|
internal static string ToLuceneString(this int i) => ((double)i).ToLuceneString();
|
|
internal static string ToLuceneString(this float f) => ((double)f).ToLuceneString();
|
|
internal static string ToLuceneString(this DateTime dt)
|
|
=> dt.ToString("yyyyMMdd") + DECIMAL_PRECISION;
|
|
internal static string ToLuceneString(this double d)
|
|
=> d.ToString("0" + DECIMAL_PRECISION).PadLeft(PAD_DIGITS + DECIMAL_PRECISION.Length, '0');
|
|
}
|
|
}
|