This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SearchIndex.cs
54 lines (49 loc) · 1.65 KB
/
SearchIndex.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
using System;
using CMS.Search;
using CMS.MediaLibrary;
using System.Collections.Generic;
using System.Linq;
using CMS.DataEngine;
using CMS.EventLog;
using MediaLibrarySearchIndex;
using CMS;
namespace MediaLibrarySearchIndex
{
public class SearchIndex : ICustomSearchIndex
{
public void Rebuild(SearchIndexInfo srchInfo)
{
if (srchInfo == null || srchInfo.IndexSettings == null)
return;
var iw = srchInfo.Provider.GetWriter(true);
if (iw == null)
return;
try
{
var provider = MediaFileInfoProvider.GetMediaFiles();
if (srchInfo.IndexBatchSize > 0)
provider = provider.TopN(srchInfo.IndexBatchSize);
var lastId = 0;
List<MediaFileInfo> files;
List<ISearchDocument> searchDocuments = new List<ISearchDocument>();
while ((files = provider.WhereGreaterThan(nameof(MediaFileInfo.FileID), lastId).OrderBy(nameof(MediaFileInfo.FileID)).ToList()).Any())
{
files.ForEach(file => searchDocuments.Add(SearchHelper.GetSearchDocument(file, srchInfo)));
lastId = files.Last().FileID;
}
searchDocuments.ForEach(doc => iw.AddDocument(doc));
iw.Flush();
}
catch (Exception ex)
{
EventLogProvider.LogException("Smart search", "rebuild", ex);
srchInfo.IndexStatus = IndexStatusEnum.ERROR;
}
finally
{
iw.Close();
}
}
}
}