隐藏

solr .Net端(SolrNet)

发布:2019/12/30 14:09:10作者:管理员 来源:本站 浏览次数:1095

首先

引用SolrNet.dll 

Microsoft.Practices.ServiceLocation

HttpWebAdapters

也可以用.net IDe 中的 nuget下载 solrnet包

//清除上次请求(不清除会导致重复请求报错)
SolrNet.Startup.Container.Clear();
SolrNet.Startup.InitContainer();

//连接Solr服务器

 SolrNet.Startup.Init<Object_acceptanceformsolr>("http://00.000.000.00:8983/solr/connection");

//定义solr
ISolrOperations<class> solr = ServiceLocator.Current.GetInstance<ISolrOperations<class>>();

//建立排序,条件.
QueryOptions options = new QueryOptions();
options.Rows = ;//数据条数
options.Start = ;//start;//开始项

//创建条件集合
List<ISolrQuery> query = new List<ISolrQuery>();

//创建查询条件(此处可以是单一条件查,或者是分词配置的name名称查找分词下字段所有列里包含的值)
var qTB = new SolrQueryByField("text_search", text_search);
//添加条件
query.Add(qTB);

//创建时间范围实例
SolrQueryByRange<DateTime> qDateRange = null;

//此处用于查询数据定义的时间范围(没有特殊情况time值0:查询所有数据)
 //七天内数据
 if (time == "-7")
{
//后两个参数,一个是开始时间,一个是结束时时间
 qDateRange = new SolrQueryByRange<DateTime>("createdate", DateTime.Now.AddDays(int.Parse(time)), DateTime.MaxValue);
 }
 else if (time == "0")//所有数据
 {
 qDateRange = new SolrQueryByRange<DateTime>("createdate", DateTime.MinValue, DateTime.MaxValue);
}
 else//其他
 {
 qDateRange = new SolrQueryByRange<DateTime>("createdate", DateTime.Today.AddMonths(int.Parse(time)), DateTime.MaxValue);
 }
 query.Add(qDateRange);

//一列中多个值得关系

List<ISolrQuery> DepMent = new List<ISolrQuery>();
for (int i = 0; i < list.Count; i++)
{
DepMent.Add(new SolrQueryByField("department", list[i].childid));
}
//创建关系,是OR还是AND
var qArea = new SolrMultipleCriteriaQuery(DepMent, "OR");
//添加至条件集合
query.Add(qArea);

 

List<ISolrQuery> filtersli = new List<ISolrQuery>();
//包含,不包含,相等,不相等,开头为,结尾为,大于,小于,大于等于,小于等于
if (Obj.filter.filters[i].OPERATOR.ToLower() == "contains".ToLower())
{
var f = new SolrQueryByField(Obj.filter.filters[i].field, "*" + Obj.filter.filters[i].value + "*") { Quoted = false };
filtersli.Add(f);
}
else if (Obj.filter.filters[i].OPERATOR.ToLower() == "doesnotcontain".ToLower())
{
var f = new SolrQueryByField("-" + Obj.filter.filters[i].field, "*" + Obj.filter.filters[i].value + "*") { Quoted = false };
filtersli.Add(f);
}
else if (Obj.filter.filters[i].OPERATOR.ToLower() == "eq".ToLower())
{
var f = new SolrQueryByField(Obj.filter.filters[i].field, Obj.filter.filters[i].value) { Quoted = false };
filtersli.Add(f);
}
else if (Obj.filter.filters[i].OPERATOR.ToLower() == "neq".ToLower())
{
var f = new SolrQueryByField("-" + Obj.filter.filters[i].field, Obj.filter.filters[i].value) { Quoted = false };
filtersli.Add(f);
}
else if (Obj.filter.filters[i].OPERATOR.ToLower() == "startswith".ToLower())
{
var f = new SolrQueryByField(Obj.filter.filters[i].field, "*" + Obj.filter.filters[i].value) { Quoted = false };
filtersli.Add(f);
}
else if (Obj.filter.filters[i].OPERATOR.ToLower() == "endswith".ToLower())
{
var f = new SolrQueryByField(Obj.filter.filters[i].field, Obj.filter.filters[i].value + "*") { Quoted = false };
filtersli.Add(f);
}
else if (Obj.filter.filters[i].OPERATOR.ToLower() == "gt".ToLower())
{
qDate = new SolrQueryByRange<DateTime>(Obj.filter.filters[i].field, Convert.ToDateTime(Obj.filter.filters[i].value), DateTime.MaxValue, false);
}
else if (Obj.filter.filters[i].OPERATOR.ToLower() == "lt".ToLower())
{
qDate = new SolrQueryByRange<DateTime>(Obj.filter.filters[i].field, DateTime.MinValue, Convert.ToDateTime(Obj.filter.filters[i].value), true, false);
}
else if (Obj.filter.filters[i].OPERATOR.ToLower() == "gte".ToLower())
{
qDate = new SolrQueryByRange<DateTime>(Obj.filter.filters[i].field, Convert.ToDateTime(Obj.filter.filters[i].value), DateTime.MaxValue);
}
else if (Obj.filter.filters[i].OPERATOR.ToLower() == "lte".ToLower())
{
qDate = new SolrQueryByRange<DateTime>(Obj.filter.filters[i].field, DateTime.MinValue, Convert.ToDateTime(Obj.filter.filters[i].value));
}
query.Add(qDate);

//创建关系,是OR还是AND
var qAred = new SolrMultipleCriteriaQuery(filtersli, Obj.filter.logic);
query.Add(qAred);
}

//创建关系,是OR还是AND
var qAred = new SolrMultipleCriteriaQuery(filtersli, Obj.filter.logic);
query.Add(qAred);
}

}
}
}

//排序

 options.AddOrder(new SolrNet.SortOrder(Obj.sort[i].field, Order.ASC));

//条件集合之间的关系
var qTBO = new SolrMultipleCriteriaQuery(query, "AND");

//执行查询,有5个重载(Solr中SolrConfig中默认wt=json)在取数据时候一直报xml:第一行第一列经过试验我修改了 SolrConfig中的配置wt=xml
SolrQueryResults<class> results = solr.Query(qTBO, options);

 

在请求数据方面要注意大小写一定要与Solr中对应,Solr文件中也一样

 

  var Key= ServiceLocator.Current.GetInstance<ISolrOperations<class>>();   Key。可以对数据进行增删改