using System;
using System.Collections.Generic;
namespace Seek.SearchIndex
{
public partial class IndexProductModel
{
public IndexProductModel()
{
}
#region Properties
public int ID { get; set; }
public int ProductID { get; set; }
public string ClassPath { get; set; }
public int ClassID1 { get; set; }
public int ClassID2 { get; set; }
public int ClassID3 { get; set; }
public string Title { get; set; }
public string Model { get; set; }
public string PriceRange { get; set; }
public string AttributeValues { get; set; }
public string ProductImages { get; set; }
public int MemberID { get; set; }
public System.DateTime CreateDate { get; set; }
public System.DateTime LastEditDate { get; set; }
public string FileName { get; set; }
public string ProductType { get; set; }
public string Summary { get; set; }
public string Details { get; set; }
public string RelatedKeywords { get; set; }
public int MemberGrade { get; set; }
#endregion
}
}
(3)配置Solr服务器端的xml,就是将咱们的这个索引实体类配置到Solr服务器上,进入D:\SorlServer\one\collection1\conf,打开schema.xml文件,配置如下
(4)开始创建索引,最好能写一个生成索引的客户端程序,我这里提供一下自己的索引器关键代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Seek.SearchIndex;
using System.Data;
using System.Threading;
using System.Configuration;
using System.Reflection;
using EasyNet.Solr;
using EasyNet.Solr.Impl;
using EasyNet.Solr.Commons;
using System.Xml.Linq;
using EasyNet.Solr.Commons.Params;
using System.Threading.Tasks;
namespace Seek.SearchIndex
{
///
/// 索引器
///
public class Indexer
{
private readonly static OptimizeOptions optimizeOptions = new OptimizeOptions();
private readonly static CommitOptions commitOptions = new CommitOptions() { SoftCommit = true };
private readonly static ISolrResponseParser binaryResponseHeaderParser = new BinaryResponseHeaderParser();
private readonly static IUpdateParametersConvert updateParametersConvert = new BinaryUpdateParametersConvert();
private readonly static ISolrQueryConnection connection = new SolrQueryConnection() { ServerUrl = ConfigurationManager.AppSettings["SolrServer"] };
private readonly static ISolrUpdateConnection solrUpdateConnection = new SolrUpdateConnection() { ServerUrl = ConfigurationManager.AppSettings["SolrServer"], ContentType = "application/javabin" };
private readonly static ISolrUpdateOperations solr = new SolrUpdateOperations(solrUpdateConnection, updateParametersConvert) { ResponseWriter = "javabin" };
private readonly static ISolrQueryOperations solrQuery = new SolrQueryOperations(connection) { ResponseWriter = "javabin" };
public enum State
{
///
/// 运行中
///
Runing,
///
/// 停止
///
Stop,
///
/// 中断
///
Break
}
///
/// 窗口
///
private Main form;
///
/// 线程
///
public Thread t;
///
/// 消息状态
///
public State state = State.Stop;
///
/// 当前索引
///
private long currentIndex = 0;
public long CurrentIndex
{
get { return currentIndex; }
set { currentIndex = value; }
}
private int _startId = AppCongfig.StartId;
public int StartId
{
get { return _startId; }
set { _startId = value; }
}
///
/// 产品总数
///
private int productsCount = 0;
///
/// 起始时间
///
private DateTime startTime = DateTime.Now;
///
/// 结束时间
///
private DateTime endTime = DateTime.MinValue;
private static object syncLock = new object();
#region 单利模式
private static Indexer instance = null;
private Indexer(Main _form)
{
form = _form;
productsCount = DataAccess.GetCount(0); //产品数统计
form.fullerTsslMaxNum.Text = productsCount.ToString();
form.fullerProgressBar.Minimum = 0;
form.fullerProgressBar.Maximum = productsCount;
}
public static Indexer GetInstance(Main form)
{
if (instance == null)
{
lock (syncLock)
{
if (instance == null)
{
instance = new Indexer(form);
}
}
}
return instance;
}
#endregion
///
/// 启动
///
public void Start()
{
ThreadStart ts = new ThreadStart(FullerRun);
t = new Thread(ts);
t.Start();
}
///
/// 停止
///
public void Stop()
{
state = State.Stop;
}
///
/// 中断
///
public void Break()
{
state = State.Break;
}
///
/// 创建索引
///
public void InitIndex(object data)
{
var docs = new List();
DataTable list = data as DataTable;
foreach (DataRow pro in list.Rows)
{
var model = new SolrInputDocument();
PropertyInfo[] properites = typeof(IndexProductModel).GetProperties();//得到实体类属性的集合
string[] dateFields = { "CreateDate", "LastEditDate" };
string field = string.Empty;//存储fieldname
foreach (PropertyInfo propertyInfo in properites)//遍历数组
{
object val = pro[propertyInfo.Name];
if (val != DBNull.Value)
{
model.Add(propertyInfo.Name, new SolrInputField(propertyInfo.Name, val));
}
}
docs.Add(model);
StartId = Convert.ToInt32(pro["ID"]);
}
GetStartId();
lock (syncLock)
{
if (currentIndex
{
//从数据库查询商品详细属性
if (row != null)
{
var docs = new List();
var model = new SolrInputDocument();
PropertyInfo[] properites = typeof(IndexProductModel).GetProperties();//得到实体类属性的集合
string[] dateFields = { "CreateDate", "LastEditDate" };
string field = string.Empty;//存储fieldname
foreach (PropertyInfo propertyInfo in properites)//遍历数组
{
object val = row[propertyInfo.Name];
if (val != DBNull.Value)
{
model.Add(propertyInfo.Name, new SolrInputField(propertyInfo.Name, val));
}
}
docs.Add(model);
StartId = Convert.ToInt32(row["ID"]);
var result = solr.Update("/update", new UpdateOptions() { Docs = docs });
}
});
//GetStartId();
lock (syncLock)
{
if (currentIndex 0 && state == State.Runing)
{
try
{
InitIndex(dt);//单线程
// CreateIndexer(dt);//多线程
}
catch (Exception ex)
{
state = State.Stop;
form.btnInitIndex.Enabled = true;
form.btnSuspend.Enabled = false;
form.btnStop.Enabled = false;
GetStartId();
this.SendMesasge(ex.Message.ToString());
}
form.fullerTsslTimeSpan.Text = "已运行 :" + GetTimeSpanShow(DateTime.Now - startTime) + ",预计还需:" + GetTimeSpanForecast();
try
{
dt = DataAccess.GetNextProductsInfo(StartId);//获取下一组产品
}
catch (Exception err)
{
this.SendMesasge("获取下一组产品出错,起始ID[" + StartId + "]:" + err.Message);
}
}
if (state == State.Runing)
{
state = State.Stop;
form.btnInitIndex.Enabled = true;
form.btnSuspend.Enabled = false;
form.btnStop.Enabled = false;
AppCongfig.SetValue("StartId", StartId.ToString());
this.SendMesasge("完全索引已完成,总计索引数[" + currentIndex + "]结束的产品Id" + StartId);
}
else if (state == State.Break)
{
GetStartId();
state = State.Break;
form.btnInitIndex.Enabled = true;
form.btnSuspend.Enabled = false;
form.btnStop.Enabled = false;
AppCongfig.SetValue("StartId", StartId.ToString());
this.SendMesasge("完全索引已暂停,当前索引位置[" + currentIndex + "]结束的产品Id" + StartId);
}
else if (state == State.Stop)
{
GetStartId();
state = State.Stop;
this.SendMesasge("完全索引已停止,已索引数[" + currentIndex + "]结束的产品Id" + StartId);
form.btnInitIndex.Enabled = true;
form.btnSuspend.Enabled = false;
form.btnStop.Enabled = false;
AppCongfig.SetValue("StartId", StartId.ToString());
productsCount = DataAccess.GetCount(StartId); //产品数统计
form.fullerTsslMaxNum.Text = productsCount.ToString();
form.fullerProgressBar.Minimum = 0;
form.fullerProgressBar.Maximum = productsCount;
}
endTime = DateTime.Now;
}
///
/// 多线程构建索引数据方法
///
///
public void MultiThreadCreateIndex(object threadDataParam)
{
InitIndex(threadDataParam);
}
///
/// 获取最大的索引id
///
private void GetStartId()
{
IDictionary options = new Dictionary();
options[CommonParams.SORT] = new string[] { "ProductID DESC" };
options[CommonParams.START] = new string[] { "0" };
options[CommonParams.ROWS] = new string[] { "1" };
options[HighlightParams.FIELDS] = new string[] { "ProductID" };
options[CommonParams.Q] = new string[] { "*:*" };
var result = solrQuery.Query("/select", null, options);
var solrDocumentList = (SolrDocumentList)result.Get("response");
currentIndex = solrDocumentList.NumFound;
if (solrDocumentList != null && solrDocumentList.Count() > 0)
{
StartId = (int)solrDocumentList[0]["ProductID"];
//AppCongfig.SetValue("StartId", solrDocumentList[0]["ProductID"].ToString());
}
else
{
StartId = 0;
// AppCongfig.SetValue("StartId", "0");
}
}
///
/// 优化索引
///
public void Optimize()
{
this.SendMesasge("开始优化索引,请耐心等待...");
var result = solr.Update("/update", new UpdateOptions() { OptimizeOptions = optimizeOptions });
var header = binaryResponseHeaderParser.Parse(result);
this.SendMesasge("优化索引耗时:" + header.QTime + "毫秒");
}
///
/// 发送消息到界面
///
/// 发送消息到界面
protected void SendMesasge(string message)
{
form.fullerDgvMessage.Rows.Add(form.fullerDgvMessage.Rows.Count + 1, message, DateTime.Now.ToString());
}
///
/// 获取时间间隔显示
///
/// 时间间隔
///
protected string GetTimeSpanShow(TimeSpan ts)
{
string text = "";
if (ts.Days > 0)
{
text += ts.Days + "天";
}
if (ts.Hours > 0)
{
text += ts.Hours + "时";
}
if (ts.Minutes > 0)
{
text += ts.Minutes + "分";
}
if (ts.Seconds > 0)
{
text += ts.Seconds + "秒";
}
return text;
}
///
/// 获取预测时间
///
///
protected string GetTimeSpanForecast()
{
if (currentIndex != 0)
{
TimeSpan tsed = DateTime.Now - startTime;
double d = ((tsed.TotalMilliseconds / currentIndex) * productsCount) - tsed.TotalMilliseconds;
return GetTimeSpanShow(TimeSpan.FromMilliseconds(d));
}
return "";
}
}
}