-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.aspx.cs
136 lines (114 loc) · 4.95 KB
/
Main.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Main : ProFazekPageBase
{
string directoryName = "LuceneArticleDB";
string luceneDBPath = "";
protected void Page_Load(object sender, EventArgs e)
{
string appDataPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_Data");
luceneDBPath = Path.Combine(appDataPath, directoryName);
if (!IsPostBack)
{
DateTime startDate = new DateTime(2008, 1, 1, 0, 0, 0);
FromTextBox.Text = startDate.ToShortDateString();
FromTextBox_CalendarExtender.SelectedDate = startDate;
DateTime now = DateTime.Now;
ToTextBox.Text = now.ToShortDateString();
ToTextBox_CalendarExtender.SelectedDate = now;
ArticleService a = new ArticleService();
DataList1.DataSource = a.GetArticles();
DataList1.DataBind();
}
}
protected void SearchButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(SearchTextBox.Text))
{
Lucene.Net.Store.RAMDirectory ramDir = new Lucene.Net.Store.RAMDirectory(luceneDBPath);
String srch = SearchTextBox.Text;
Lucene.Net.Search.IndexSearcher idx = new Lucene.Net.Search.IndexSearcher(ramDir);
Lucene.Net.QueryParsers.QueryParser qp = new Lucene.Net.QueryParsers.QueryParser("_searchtxt", new Lucene.Net.Analysis.Standard.StandardAnalyzer());
qp.SetDefaultOperator(Lucene.Net.QueryParsers.QueryParser.Operator.AND);
Lucene.Net.Search.BooleanQuery.SetMaxClauseCount(100);
Lucene.Net.Search.Hits hits = idx.Search(qp.Parse(srch));
List<int> aIds = new List<int>();
for (int i = 0; i < hits.Length(); i++)
{
Lucene.Net.Documents.Document doc = hits.Doc(i);
int aid = 0;
if (int.TryParse(doc.Get("id"), out aid))
{
aIds.Add(aid);
}
}
using (DataClassesDataContext dtx = new DataClassesDataContext())
{
var arts = from a in dtx.Articles
where aIds.Contains(a.Id)
select a;
DataList1.DataSource = arts;
DataList1.DataBind();
}
idx.Close();
GC.Collect();
}
}
protected void IndexButton_Click(object sender, EventArgs e)
{
Directory.CreateDirectory(luceneDBPath);
Lucene.Net.Store.FSDirectory dir = Lucene.Net.Store.FSDirectory.GetDirectory(luceneDBPath, true);
Lucene.Net.Index.IndexWriter idx = new Lucene.Net.Index.IndexWriter(dir, new Lucene.Net.Analysis.Standard.StandardAnalyzer(), true);
using (DataClassesDataContext dtx = new DataClassesDataContext())
{
int FailCount = 0;
foreach (var a in dtx.Articles)
{
String searchText = String.Join(" ", new String[] { a.Title, a.Subtitle, a.Lead, a.Body,
a.Created.ToLongDateString(), a.Created.ToLongTimeString(),
a.Created.ToShortDateString(), a.Created.ToShortTimeString()});
Lucene.Net.Documents.Document doc = new Lucene.Net.Documents.Document();
doc.Add(new Lucene.Net.Documents.Field("id", a.Id.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.UN_TOKENIZED));
doc.Add(new Lucene.Net.Documents.Field("_searchtxt", searchText, Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.TOKENIZED));
try
{
idx.AddDocument(doc);
}
catch
{
FailCount++;
}
}
}
idx.Optimize();
idx.Close();
dir.Close();
GC.Collect();
}
protected void UpdateButton_Click(object sender, EventArgs e)
{
// TODO (if the website will have edit interface we should update the search DB too)
}
protected void FilterButton_Click(object sender, EventArgs e)
{
DateTime fromDate;
bool isValidFromDate = DateTime.TryParse(FromTextBox.Text, out fromDate);
DateTime toDate;
bool isValidToDate = DateTime.TryParse(ToTextBox.Text, out toDate);
using (DataClassesDataContext dtx = new DataClassesDataContext())
{
var arts = from a in dtx.Articles
where ((!isValidFromDate || a.Created >= fromDate) &&
(!isValidToDate || a.Created <= toDate))
orderby a.Created descending
select a;
DataList1.DataSource = arts;
DataList1.DataBind();
}
}
}