-
Notifications
You must be signed in to change notification settings - Fork 0
/
search1.aspx.cs
336 lines (316 loc) · 15.7 KB
/
search1.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using System.Xml.Linq;
using StockFoo.Entity;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using System.IO;
using StockFoo.Analyzer;
namespace StockFoo.Web
{
public partial class search1 : System.Web.UI.Page
{
protected string SearchWords = "";
protected string TotalCount = "";
protected string SearchTime = "";
protected string PageInfo="";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request["w"] != null && Request["w"].ToString().Length > 0)
{
int PageIndex = 1;
if (!int.TryParse(Request.QueryString["pi"], out PageIndex)) PageIndex = 1;
SelectWordResult(Server.HtmlDecode(Request["w"].ToString()), PageIndex);
}
else
{
Response.Redirect("default.aspx");
}
}
}
private void SelectWordResult(string searchStr, int pageIndex)
{
try
{
SearchWords = searchStr;
DateTime dt1 = DateTime.Now;
//Lucene.Net.Analysis.Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_CURRENT);
StockFooAnalyzer analyzer = new StockFooAnalyzer(System.Configuration.ConfigurationManager.AppSettings["AnalyzerPath"].ToString());
FSDirectory dy = FSDirectory.Open(new DirectoryInfo(Server.MapPath("manage/IndexDirectory")));
IndexSearcher searcher = new IndexSearcher(dy, false);
//多个索引文件查询
//MultiReader reader = new MultiReader(new IndexReader[] { IndexReader.Open(@"c:\index"), IndexReader.Open(@"\\server\index") });
//IndexSearcher searcher = new IndexSearcher(reader);
//Hits hits = searcher.Search(query);
//或
//IndexSearcher[] searchers = new IndexSearcher[2];
//searchers[0] = new IndexSearcher(Server.MapPath("manage/IndexDirectory01"));
//searchers[1] = new IndexSearcher(Server.MapPath("manage/IndexDirectory02"));
//MultiSearcher multiSearcher = new MultiSearcher(searchers);
//可以使用 ParallelMultiSearcher 进行多线程并行搜索。
MultiFieldQueryParser parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_CURRENT, new string[] { "title","body"}, analyzer);
Query query = parser.Parse(searchStr);
//单个条件排序
//Sort sort = new Sort();
//SortField f = new SortField("publish_time", SortField.STRING, true);//按照publish_time字段排序,true表示降序
//sort.SetSort(f);
//多个条件排序
//Sort sort = new Sort();
//SortField f1 = new SortField("title", SortField.SCORE, false);
//SortField f2 = new SortField("publish_time", SortField.STRING, true);
//sort.SetSort(new SortField[] { f1, f2 });
Hits hits = searcher.Search(query);
//TopDocs docs = searcher.Search(query, null, 10000);
//进行多条件搜索
//Query query1 = new TermQuery(new Term(FieldValue, "name1")); // 词语搜索
//Query query2 = new WildcardQuery(new Term(FieldName, "name*")); // 通配符
//Query query3 = new PrefixQuery(new Term(FieldName, "name1")); // 字段搜索 Field:Keyword,自动在结尾添加 *
//Query query4 = new RangeQuery(new Term(FieldNumber, NumberTools.LongToString(11L)), new Term(FieldNumber, NumberTools.LongToString(13L)), true); // 范围搜索
//Filter filter = new DateFilter(FieldDate, DateTime.Parse("2005-10-1"), DateTime.Parse("2005-10-30"));
//Query query5 = new FilteredQuery(query, filter); // 带过滤条件的搜索
//BooleanQuery query = new BooleanQuery();
//query.Add(query1, BooleanClause.Occur.MUST);
//query.Add(query2, BooleanClause.Occur.MUST);
//IndexSearcher searcher= new IndexSearcher(reader);
//Hits hits = searcher.Search(query);
int totalCount = 0;
if (hits != null && hits.Length() > 0)
{
ArrayList arr01 = new ArrayList();
ArrayList arr02 = new ArrayList();
for (int i = 0; i < hits.Length(); i++)
{
Document doc = hits.Doc(i);
string url = doc.Get("url").ToString();
string title = doc.Get("title").ToString();
string body = doc.Get("body").ToString();
string publishtime = doc.Get("publish_time").ToString();
string site = doc.Get("site").ToString();
if (searchStr.IndexOf(" ") > -1)
{
bool isexisit = false;
string[] word = searchStr.Split(' ');
for (int j = 0; j < word.Length; j++)
{
if (word[j].Trim().Equals(""))
{
continue;
}
if (title.IndexOf(word[j].Trim()) > -1)
{
isexisit = true;
break;
}
}
if (isexisit)
{
arr01.Add(doc);
}
else
{
arr02.Add(doc);
}
}
else
{
if (title.IndexOf(searchStr) > -1)
{
arr01.Add(doc);
}
else
{
arr02.Add(doc);
}
}
}
arr01.Sort(new DateTimeCompare());
arr02.Sort(new DateTimeCompare());
ArrayList arrAll = new ArrayList();
foreach (Document doo in arr01)
{
arrAll.Add(doo);
}
foreach (Document doo in arr02)
{
arrAll.Add(doo);
}
StringBuilder sb = new StringBuilder();
int begincount = 0;
if (pageIndex > 1)
begincount = (pageIndex - 1) * 10;
int endcount = pageIndex * 10;
//if (endcount > hits.Length() || endcount < 10)
// endcount = hits.Length();
if (endcount > arrAll.Count || endcount < 10)
endcount = arrAll.Count;
for (int i = begincount; i < endcount; i++)
{
//ScoreDoc doc = docs.scoreDocs[i];
//string url = searcher.Doc(doc.doc).Get("url").ToString();
//Document doc = hits.Doc(i);
Document doc = (Document)arrAll[i];
string url = doc.Get("url").ToString();
string title = doc.Get("title").ToString();
string body = doc.Get("body").ToString();
body = body.Length > 100 ? body.Substring(0, 100) + "..." : body;
if (searchStr.IndexOf(" ") > -1)
{
title = GetRedWord(title, searchStr);
body = GetRedWord(body, searchStr);
}
else
{
title = title.Replace(searchStr, "<font color=red>" + searchStr + "</font>");
body = body.Replace(searchStr, "<font color=red>" + searchStr + "</font>");
}
string publishtime = doc.Get("publish_time").ToString();
string site = doc.Get("site").ToString();
sb.Append(" <div style=\"margin-top: 20px; margin-bottom: 20px;width:666px;\">");
sb.Append(" <div>");
sb.Append(" <a target=\"_blank\" href=\"" + url + "\"><font color=blue>" +title+ "</font></a>");// <font color=#666699>" + doc.score.ToString("0.00")+"</font>
sb.Append(" </div>");
sb.Append(" <div style=\"font-size: 13px;\">");
sb.Append(" <font size=2 >" + body + "</font>");
sb.Append(" </div>");
sb.Append(" <div>");
string datetime = "";
try
{
datetime = Convert.ToDateTime(publishtime).ToString("yyyy-MM-dd HH:mm");
}
catch
{
datetime = publishtime;
}
sb.Append(" <font color=#006600>" + (url.Length > 66 ? url.Substring(0, 66) : url) + "</font> <font size=2 color=#006600>" + datetime + "</font> <font size=2 color=#666699>[" + site + "]</font>");
sb.Append(" </div>");
sb.Append(" </div>");
}
labResultList.Text = sb.ToString();
//totalCount = hits.Length();
totalCount = arrAll.Count;
TotalCount = totalCount.ToString();
int totalPage = totalCount % 10 == 0 ? totalCount / 10 : totalCount / 10 + 1;
if (totalPage > 1)
{
StringBuilder sbp = new StringBuilder();
string parame = "";
if (pageIndex > 1)
{
parame = "search1.aspx?pi=" + (pageIndex - 1).ToString() + "&w=" + System.Web.HttpUtility.UrlEncode(searchStr);
sbp.Append("<a href=\"" + parame + "\">上一页</a> ");
}
int beginindex = 1;
int endindex = totalPage;
if (totalPage > 20 && pageIndex > 11 && totalPage - pageIndex > 8)
{
beginindex = pageIndex - 10;
}
if (totalPage > 20 && pageIndex > 11 && totalPage - pageIndex < 9)
{
beginindex = pageIndex - (20 - (totalPage - pageIndex) - 1);
}
if (endindex > 10)
{
endindex = pageIndex + 9;
if (endindex > totalPage)
{
endindex = totalPage;
}
}
for (int i = beginindex; i <= endindex; i++)
{
if ((i) == pageIndex)
{
sbp.Append(" " + pageIndex);
}
else
{
parame = "search1.aspx?pi=" + (i).ToString() + "&w=" + System.Web.HttpUtility.UrlEncode(searchStr);
sbp.Append("<a href=\"" + parame + "\">[" + (i).ToString() + "]</a>");
}
}
if (pageIndex < totalPage)
{
parame = "search1.aspx?pi=" + (pageIndex + 1).ToString() + "&w=" + System.Web.HttpUtility.UrlEncode(searchStr);
sbp.Append(" <a href=\"" + parame + "\">下一页</a>");
}
PageInfo = sbp.ToString();
}
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt2 - dt1;
double timeuse = Convert.ToInt32(ts.TotalMilliseconds) * 0.001;
SearchTime = timeuse.ToString("0.001");
searcher.Close();
}
else
{
SearchWords = searchStr;
TotalCount = "";
SearchTime = "";
PageInfo = "";
labResultList.Text = "<br/>抱歉,未找到:“" + SearchWords + "”相关内容。<br/><br/><b>建议您:</b> <br/>1、看看输入的文字是否有误<br/>2、去掉可能不必要的字词,如“的”、“什么”等<br/>3、<a href=\"http://www.haopingba.com/Page1Result.aspx?web=aall&searchword=" + searchStr + "\" target=\"_blank\">去此处查询</a>";
}
}
catch (Exception ex)
{
SearchWords = searchStr;
TotalCount ="";
SearchTime ="";
PageInfo="";
labResultList.Text = "<br/>检索异常:" + ex.Message + "<br/><br/><b>建议您:</b> <br/>1、看看输入的文字是否有误<br/>2、去掉可能不必要的字词,如“的”、“什么”等<br/>3、<a href=\"http://www.haopingba.com/Page1Result.aspx?web=aall&searchword=" + searchStr + "\" target=\"_blank\">去此处查询</a>";
}
}
private string GetRedWord(string titleorbody, string searchStr)
{
string[] word = searchStr.Split(' ');
for (int j = 0; j < word.Length; j++)
{
if (word[j].Trim().Equals(""))
{
continue;
}
if (titleorbody.IndexOf(word[j].Trim()) > -1)
{
titleorbody=titleorbody.Replace(word[j].Trim(), "<font color=red>" + word[j].Trim() + "</font>");
}
}
return titleorbody;
}
}
public class DateTimeCompare :IComparer
{
int IComparer.Compare(object x, object y)
{
try
{
Document xdoc = (Document)x;
DateTime xpublishtime=Convert.ToDateTime(xdoc.Get("publish_time"));
Document ydoc = (Document)y;
DateTime ypublishtime = Convert.ToDateTime(ydoc.Get("publish_time"));
return ypublishtime.CompareTo(xpublishtime);
}
catch
{
throw;
}
}
}
}