B2C商城关键技术点总结(站内搜索、定时任务)

时间:2023-03-09 19:44:29
B2C商城关键技术点总结(站内搜索、定时任务)

1.站内搜索

1.1Lucene.Net建立信息索引  

             string indexPath = @"E:\xxx\xxx";//索引保存路径
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory());
bool isUpdate = IndexReader.IndexExists(directory);
if (isUpdate)
{
//如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁
if (IndexWriter.IsLocked(directory))
{
IndexWriter.Unlock(directory);
}
}
IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED); for (int i = ; i <= ; i++)
{
//因为从服务器下载页面有可能失败,为了避免失败时程序终止,所以要处理异常,写入日志
//这里能预知的异常是服务器下载失败异常,WebException
try
{
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string url = "http://localhost:3448/Book.aspx?id=" + i;
string html = wc.DownloadString(url); HTMLDocumentClass htmlDoc = new HTMLDocumentClass();
htmlDoc.designMode = "on"; //不让解析引擎去尝试运行javascript
htmlDoc.IHTMLDocument2_write(html);
htmlDoc.close(); string title = htmlDoc.title;
string content = "";
if (htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DetailsView1_txtContent") != null)
{
if (htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DetailsView1_txtContent").innerText != null)
{
content = htmlDoc.getElementById("ctl00_ContentPlaceHolder1_DetailsView1_txtContent").innerText;
}
}
//为避免重复索引,所以要先删除"url"=url的记录,再重新添加
writer.DeleteDocuments(new Term("url", url)); Document document = new Document();
document.Add(new Field("url", url, Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
document.Add(new Field("content", content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.AddDocument(document); logger.Debug("索引" + i + "完毕");
}
catch (WebException webe)
{
logger.Error(webe.Message);
}
}
writer.Close();
directory.Close();//不要忘了Close,否则索引结果搜不到
logger.Debug("全部索引完毕");

1.2盘古分词并高亮

         public List<SearchContentResult> GetSearchContentResult(string kw, int startIndex,int pageSize,  out int count)
{
string indexPath = @"E:xxx\xxx\index";
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NoLockFactory());
IndexReader reader = IndexReader.Open(directory, true);
IndexSearcher searcher = new IndexSearcher(reader); //将用户搜索的关键字进行分词
string[] strs = CommonHelper.FenCi(kw.ToLower());
PhraseQuery query = new PhraseQuery();
foreach (string str in strs)
{
query.Add(new Term("content", str));
}
query.SetSlop(); TopScoreDocCollector collector = TopScoreDocCollector.create(, true);
searcher.Search(query, null, collector);
count = collector.GetTotalHits();
ScoreDoc[] docs = collector.TopDocs(startIndex,pageSize).scoreDocs;
List<SearchContentResult> scs = new List<SearchContentResult>();
for (int i = ; i < docs.Length; i++)
{
int docId = docs[i].doc;
Document doc = searcher.Doc(docId);
SearchContentResult sc = new SearchContentResult();
sc.Url = doc.Get("url");
sc.Title = doc.Get("title");
sc.Body = highLight(kw, doc.Get("content"));
scs.Add(sc);
}
return scs;
} private static String highLight(string keyword, String content)
{
PanGu.HighLight.SimpleHTMLFormatter formatter = new PanGu.HighLight.SimpleHTMLFormatter("<font color='red'>", "</font>");
PanGu.HighLight.Highlighter highlighter = new PanGu.HighLight.Highlighter(formatter, new Segment());
highlighter.FragmentSize = ;
string msg = highlighter.GetBestFragment(keyword, content);
if (string.IsNullOrEmpty(msg))
{
return content;
}
else
{
return msg;
}
}

2.Quartz.Net定时任务

在Global类中声明一个静态变量

static IScheduler sched;

保证其在系统中是唯一的

             //建立一个Quartz任务
ISchedulerFactory sf = new StdSchedulerFactory();
sched = sf.GetScheduler();
JobDetail job = new JobDetail("job1", "group1", typeof(IndexJob));//IndexJob为实现了IJob接口的类 Trigger trigger = TriggerUtils.MakeDailyTrigger("trigger", , );
trigger.JobGroup = "group1";
trigger.JobName = "job1"; sched.AddJob(job, true);
sched.ScheduleJob(trigger);
sched.Start();

添加任务类,并继承接口

     public class IndexJob : IJob
{
private static ILog logger = LogManager.GetLogger(typeof(IndexJob));
public void Execute(JobExecutionContext context)
{
//此处写执行的代码
}
}