My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#region Disclaimer/Info

///////////////////////////////////////////////////////////////////////////////////////////////////
// Subtext WebLog
//
// Subtext is an open source weblog system that is a fork of the .TEXT
// weblog system.
//
// For updated news and information please visit http://subtextproject.com/
// Subtext is hosted at Google Code at http://code.google.com/p/subtext/
// The development mailing list is at subtext@googlegroups.com
//
// This project is licensed under the BSD license. See the License.txt file for more information.
///////////////////////////////////////////////////////////////////////////////////////////////////

#endregion

using System;
using System.Collections.Generic;
using Lucene.Net.Analysis;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
using Similarity.Net;
using Subtext.Framework.Configuration;
using Subtext.Framework.Logging;

namespace Subtext.Framework.Services.SearchEngine
{

public class SearchEngineService : ISearchEngineService
{
private readonly Directory _directory;
private readonly Analyzer _analyzer;
private static IndexWriter _writer;
private readonly FullTextSearchEngineSettings _settings;
// Special Lucene characters: http://lucene.apache.org/java/2_4_0/queryparsersyntax.html#Escaping Special Characters
private readonly static string[] specialLuceneCharacters = { @"\", "+", "-", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "*", "?", ":" };

private const string Title = "Title";
private const string Body = "Body";
private const string Tags = "Tags";
private const string Pubdate = "PubDate";
private const string Blogid = "BlogId";
private const string Groupid = "GroupId";
private const string BlogName = "BlogName";
private const string Entryid = "PostId";
private const string Published = "IsPublished";
private const string EntryName = "EntryName";

private static readonly Object WriterLock = new Object();

private static readonly Log Log = new Log();
private bool _disposed;

public SearchEngineService(Directory directory, Analyzer analyzer, FullTextSearchEngineSettings settings)
{
_directory = directory;
_analyzer = analyzer;
_settings = settings;
}

private void DoWriterAction(Action<IndexWriter> action)
{
lock (WriterLock)
{
EnsureIndexWriter();
}
action(_writer);
}

private T DoWriterAction<T>(Func<IndexWriter, T> action)
{
lock (WriterLock)
{
EnsureIndexWriter();
}
return action(_writer);
}

// Method should only be called from within a lock.
void EnsureIndexWriter()
{
if (_writer == null)
{
if (IndexWriter.IsLocked(_directory))
{
Log.Error("Something left a lock in the index folder: deleting it");
IndexWriter.Unlock(_directory);
Log.Info("Lock Deleted... can proceed");
}
_writer = new IndexWriter(_directory, _analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
_writer.SetMergePolicy(new LogDocMergePolicy(_writer));
_writer.SetMergeFactor(5);
}
}

private IndexSearcher Searcher
{
get { return DoWriterAction(writer => new IndexSearcher(writer.GetReader())); }
}


private QueryParser BuildQueryParser()
{
var parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, Body, _analyzer);
parser.SetDefaultOperator(QueryParser.Operator.AND);
return parser;
}

public IEnumerable<IndexingError> AddPost(SearchEngineEntry post)
{
return AddPosts(new[] { post }, false);
}

public IEnumerable<IndexingError> AddPosts(IEnumerable<SearchEngineEntry> posts)
{
return AddPosts(posts, true);
}

public IEnumerable<IndexingError> AddPosts(IEnumerable<SearchEngineEntry> posts, bool optimize)
{
IList<IndexingError> errors = new List<IndexingError>();
foreach (var post in posts)
{
ExecuteRemovePost(post.EntryId);
try
{
var currentPost = post;
DoWriterAction(writer => writer.AddDocument(CreateDocument(currentPost)));
}
catch (Exception ex)
{
errors.Add(new IndexingError(post, ex));
}
}
DoWriterAction(writer =>
{
writer.Commit();
if (optimize)
{
writer.Optimize();
}

});

return errors;
}

public void RemovePost(int postId)
{
ExecuteRemovePost(postId);
DoWriterAction(writer => writer.Commit());
}

public int GetIndexedEntryCount(int blogId)
{
var query = GetBlogIdSearchQuery(blogId);
TopDocs hits = Searcher.Search(query, 1);
return hits.totalHits;
}

public int GetTotalIndexedEntryCount()
{
return DoWriterAction(writer => writer.GetReader().NumDocs());
}

private void ExecuteRemovePost(int entryId)
{
Query searchQuery = GetIdSearchQuery(entryId);
DoWriterAction(writer => writer.DeleteDocuments(searchQuery));
}

private static Query GetIdSearchQuery(int id)
{
return new TermQuery(new Term(Entryid, NumericUtils.IntToPrefixCoded(id)));
}

private static Query GetBlogIdSearchQuery(int id)
{
return new TermQuery(new Term(Blogid, NumericUtils.IntToPrefixCoded(id)));
}

protected virtual Document CreateDocument(SearchEngineEntry post)
{
var doc = new Document();

var postId = new Field(Entryid,
NumericUtils.IntToPrefixCoded(post.EntryId),
Field.Store.YES,
Field.Index.NOT_ANALYZED,
Field.TermVector.NO);

var title = new Field(Title,
post.Title,
Field.Store.YES,
Field.Index.ANALYZED,
Field.TermVector.YES);
title.SetBoost(_settings.Parameters.TitleBoost);

var body = new Field(Body,
post.Body,
Field.Store.NO,
Field.Index.ANALYZED,
Field.TermVector.YES);
body.SetBoost(_settings.Parameters.BodyBoost);

var tags = new Field(Tags,
post.Tags,
Field.Store.NO,
Field.Index.ANALYZED,
Field.TermVector.YES);
tags.SetBoost(_settings.Parameters.TagsBoost);

var blogId = new Field(Blogid,
NumericUtils.IntToPrefixCoded(post.BlogId),
Field.Store.NO,
Field.Index.NOT_ANALYZED,
Field.TermVector.NO);


var published = new Field(Published,
post.IsPublished.ToString(),
Field.Store.NO,
Field.Index.NOT_ANALYZED,
Field.TermVector.NO);

var pubDate = new Field(Pubdate,
DateTools.DateToString(post.PublishDate, DateTools.Resolution.MINUTE),
Field.Store.YES,
Field.Index.NOT_ANALYZED,
Field.TermVector.NO);

var groupId = new Field(Groupid,
NumericUtils.IntToPrefixCoded(post.GroupId),
Field.Store.NO,
Field.Index.NOT_ANALYZED,
Field.TermVector.NO);

var blogName = new Field(BlogName,
post.BlogName,
Field.Store.YES,
Field.Index.NO,
Field.TermVector.NO);

var postName = new Field(EntryName,
post.EntryName ?? "",
Field.Store.YES,
Field.Index.NO,
Field.TermVector.NO);
postName.SetBoost(_settings.Parameters.EntryNameBoost);


doc.Add(postId);
doc.Add(title);
doc.Add(body);
doc.Add(tags);
doc.Add(blogId);
doc.Add(published);
doc.Add(pubDate);
doc.Add(groupId);
doc.Add(blogName);
doc.Add(postName);

return doc;
}

protected virtual SearchEngineResult CreateSearchResult(Document doc, float score)
{
var result = new SearchEngineResult
{
BlogName = doc.Get(BlogName),
EntryId = NumericUtils.PrefixCodedToInt(doc.Get(Entryid)),
PublishDate = DateTools.StringToDate(doc.Get(Pubdate)),
Title = doc.Get(Title),
Score = score
};
string entryName = doc.Get(EntryName);
result.EntryName = !String.IsNullOrEmpty(entryName) ? entryName : null;

return result;
}

public IEnumerable<SearchEngineResult> RelatedContents(int entryId, int max, int blogId)
{
var list = new List<SearchEngineResult>();

//First look for the original doc
Query query = GetIdSearchQuery(entryId);
TopDocs hits = Searcher.Search(query, max);

if (hits.scoreDocs.Length <= 0)
{
return list;
}

int docNum = hits.scoreDocs[0].doc;

//Setup MoreLikeThis searcher
var reader = DoWriterAction(w => w.GetReader());
var mlt = new MoreLikeThis(reader);
mlt.SetAnalyzer(_analyzer);
mlt.SetFieldNames(new[] { Title, Body, Tags });
mlt.SetMinDocFreq(_settings.Parameters.MinimumDocumentFrequency);
mlt.SetMinTermFreq(_settings.Parameters.MinimumTermFrequency);
mlt.SetBoost(_settings.Parameters.MoreLikeThisBoost);

var moreResultsQuery = mlt.Like(docNum);
return PerformQuery(list, moreResultsQuery, max + 1, blogId, entryId);
}

public IEnumerable<SearchEngineResult> Search(string queryString, int max, int blogId)
{
return Search(queryString, max, blogId, -1);
}

public IEnumerable<SearchEngineResult> Search(string queryString, int max, int blogId, int entryId)
{
var list = new List<SearchEngineResult>();
if (String.IsNullOrEmpty(queryString))
{
return list;
}

QueryParser parser = BuildQueryParser();
foreach (var specialCharacter in specialLuceneCharacters)
{
if (queryString.Contains(specialCharacter))
{
queryString = queryString.Replace(specialCharacter, @"\" + specialCharacter);
}
}

Query bodyQuery = parser.Parse(queryString);

if (bodyQuery.ToString() == "")
{
return list;
}

string queryStringMerged = String.Format("({0}) OR ({1}) OR ({2})",
bodyQuery,
bodyQuery.ToString().Replace("Body", "Title"),
bodyQuery.ToString().Replace("Body", "Tags"));

Query query = parser.Parse(queryStringMerged);


return PerformQuery(list, query, max, blogId, entryId);
}

private IEnumerable<SearchEngineResult> PerformQuery(ICollection<SearchEngineResult> list, Query queryOrig, int max, int blogId, int idToFilter)
{
Query isPublishedQuery = new TermQuery(new Term(Published, true.ToString()));
Query isBlogQuery = GetBlogIdSearchQuery(blogId);

var query = new BooleanQuery();
query.Add(isPublishedQuery, BooleanClause.Occur.MUST);
query.Add(queryOrig, BooleanClause.Occur.MUST);
query.Add(isBlogQuery, BooleanClause.Occur.MUST);
IndexSearcher searcher = Searcher;
TopDocs hits = searcher.Search(query, max);
int length = hits.scoreDocs.Length;
int resultsAdded = 0;
float minScore = _settings.MinimumScore;
float scoreNorm = 1.0f / hits.GetMaxScore();
for (int i = 0; i < length && resultsAdded < max; i++)
{
float score = hits.scoreDocs[i].score * scoreNorm;
SearchEngineResult result = CreateSearchResult(searcher.Doc(hits.scoreDocs[i].doc), score);
if (idToFilter != result.EntryId && result.Score > minScore && result.PublishDate < DateTime.UtcNow)
{
list.Add(result);
resultsAdded++;
}

}
return list;
}

~SearchEngineService()
{
Dispose();
}

public void Dispose()
{
lock (WriterLock)
{
if (!_disposed)
{
//Never checking for disposing = true because there are
//no managed resources to dispose

var writer = _writer;

if (writer != null)
{
try
{
writer.Close();
}
catch (ObjectDisposedException e)
{
Log.Error("Exception while disposing SearchEngineService", e);
}
_writer = null;
}

var directory = _directory;
if (directory != null)
{
try
{
directory.Close();
}
catch (ObjectDisposedException e)
{
Log.Error("Exception while disposing SearchEngineService", e);
}
}

_disposed = true;
}
}
GC.SuppressFinalize(this);
}
}
}

Change log

r4213 by haacked on Jun 2, 2011   Diff
Fixed  Issue #300  Aggregated home page now
respects time zones on future posts.
Subtext now stores everything using UTC.
Go to: 
Sign in to write a code review

Older revisions

r4187 by haacked on May 15, 2011   Diff
 Issue #196 : Fixed exception thrown by
Lucene when searching for phrase
containing parenthesis by escaping the
set of Lucene reserved characters.
r4047 by simone.chiaretta on May 25, 2010   Diff
 Issue 216 : Added a "kill" switch to
Lucene.Net
I created a NoOp version of both
SearchEngine and the Indexing services
and made a few other changes around to
...
r3987 by haacked on Jan 17, 2010   Diff
Applied R# recommendations.
All revisions of this file

File info

Size: 15333 bytes, 432 lines
Powered by Google Project Hosting