solrnet 查询
裘光启
2023-12-01
public ShowSelectVo SearchVos(string value, Guid? siteId, string areaName)
{
var showSelectVo = new ShowSelectVo();
Startup.Container.Clear();
Startup.InitContainer();
Startup.Init<ShowContent>("http://localhost:8080/solr/collection1");
ISolrOperations<ShowContent> solr = ServiceLocator.Current.GetInstance<ISolrOperations<ShowContent>>();
ISolrQuery query = SolrQuery.All;
QueryOptions options = new QueryOptions();
#region 检索
//IList<ISolrQuery> filter = new List<ISolrQuery>();
//if (!string.IsNullOrWhiteSpace(value))
//{
// ISolrQuery q1 = new SolrQueryByField("text", value);
// filter.Add(q1);
//}
//if (!string.IsNullOrWhiteSpace(areaName))
//{
// areaName = GetType(areaName).ToString();
// ISolrQuery q2 = new SolrQueryByField("area", areaName);
// filter.Add(q2);
//}
//if (siteId.HasValue)
//{
// ISolrQuery q3 = new SolrQueryByField("siteid", "\"" + siteId.ToString().ToUpper() + "\"");
// filter.Add(q3);
//}
//if (filter.Count > 0)
//{
// query = new SolrMultipleCriteriaQuery(filter, "AND");
//}
// // 创建查询对象
// SolrQuery solrQuery = new SolrQuery("");
var massage = new StringBuilder("siteid:" + "\"" + siteId.ToString().ToUpper() + "\"");
if (!string.IsNullOrWhiteSpace(areaName))
{
massage.Append("AND title:" + areaName);
}
if (!string.IsNullOrWhiteSpace(areaName))
{
areaName = GetType(areaName).ToString();
massage.Append("AND area:" + areaName);
}
//if (!string.IsNullOrWhiteSpace(value))
//{
// query = new SolrQueryByField("text", value);
//}
//if (!string.IsNullOrWhiteSpace(areaName))
//{
// query = new SolrQueryByField("area", areaName);
//}
//if (siteId.HasValue)
//{
// query = new SolrQueryByField("siteId", siteId.ToString());
//}
// SolrQueryResults<ShowContent> result = solr.Query(query, options);
SolrQueryResults<ShowContent> result = solr.Query(new SolrQuery(massage.ToString()));
#endregion
#region 分组
ISolrQuery querygrop = SolrQuery.All;
if (!string.IsNullOrWhiteSpace(value))
{
querygrop = new SolrQueryByField("text", value);
}
//IList<ISolrQuery> filter2 = new List<ISolrQuery>();
//if (!string.IsNullOrWhiteSpace(value))
//{
// ISolrQuery q1 = new SolrQueryByField("text", value);
// filter2.Add(q1);
//}
//if (siteId.HasValue)
//{
// ISolrQuery q2 = new SolrQueryByField("siteid", siteId.ToString());
// filter2.Add(q2);
//}
//if (filter2.Count >1)
//{
// querygrop = new SolrMultipleCriteriaQuery(filter2, "AND");
//}
var groupingParameters = new GroupingParameters();
groupingParameters.Fields = new Collection<string> { "area" };
groupingParameters.Ngroups = true; //设置查询分组的总个数为true
options.Grouping = groupingParameters;
SolrQueryResults<ShowContent> results = solr.Query(querygrop, options);
GroupedResults<ShowContent> items = results.Grouping["area"];
var areaCounts = new List<AreaCount>();
int num = 0;
foreach (var itema in items.Groups)
{
var areaCount = new AreaCount();
//var areaname = "优课中心";
//try
//{
// areaname = GetAreaName((AreaType)Enum.Parse(typeof(AreaType), itema.GroupValue));
//}
//catch (Exception ex)
//{
// areaname;
//}
areaCount.Area = GetAreaName((AreaType)Enum.Parse(typeof(AreaType), itema.GroupValue));
areaCount.Count = itema.NumFound;
areaCounts.Add(areaCount);
num = num + itema.NumFound;
}
showSelectVo.AreaCounts = areaCounts;
showSelectVo.TotalCount = num;
#endregion
#region 高亮
var highlights = result.Highlights;
var url = HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port + "/";
foreach (var item in result)
{
var areatype = item.area;
var name = "";
try
{
name = GetAreaName((AreaType)Enum.Parse(typeof(AreaType), item.area));
}
catch (Exception ex)
{
name = "优课中心";
}
item.area = name;
for (int i = 0; i < highlights[item.id.ToString()].Values.Count; i++)
{
if (highlights[item.id.ToString()].Keys.ToList()[i] == "title")
{
var title = highlights[item.id.ToString()].Values.ToList()[i].ToList()[0];
item.title = title;
}
if (highlights[item.id.ToString()].Keys.ToList()[i] == "teacher")
{
var teacher = highlights[item.id.ToString()].Values.ToList()[i].ToList()[0];
item.teacher = teacher;
}
if (highlights[item.id.ToString()].Keys.ToList()[i] == "area")
{
var area = highlights[item.id.ToString()].Values.ToList()[i].ToList()[0];
area = area.Replace(areatype, name);
item.area = area;
}
if (highlights[item.id.ToString()].Keys.ToList()[i] == "orgname")
{
var orgname = highlights[item.id.ToString()].Values.ToList()[i].ToList()[0];
item.orgname = orgname;
}
if (highlights[item.id.ToString()].Keys.ToList()[i] == "description")
{
var description = highlights[item.id.ToString()].Values.ToList()[i].ToList()[0];
item.description = description;
}
if (highlights[item.id.ToString()].Keys.ToList()[i] == "classify")
{
var classify = highlights[item.id.ToString()].Values.ToList()[i].ToList()[0];
item.classify = classify;
}
}
}
#endregion
showSelectVo.ShowContents = result;
return showSelectVo;
}