/// <summary> /// 查询ES数据 /// </summary> /// <param name="keyWord"></param> /// <param name="idxName"></param> /// <param name="pageIndex">分页的页号</param> /// <returns></returns> public SearchRs QueryByKeyWord(string keyWord, string idxName, int pageIndex) { SearchRs searchRs = new SearchRs(); //高亮关键字? var rs = m_client.Search <QGSSLuaXmlFile>(s => s.Index(idxName). Query( q => q.Match(qs => qs.Query(keyWord).Operator(Operator.Or).Field(fd => fd.FileContent))) .Size(PageSize) .From(PageSize * pageIndex) .Highlight(h => h .PreTags("<strong>") .PostTags("</strong>") .Encoder(HighlighterEncoder.Html) .Fields( fs => fs.Field(p => p.FileContent) ) ) ); //高亮关键字内容存到字段 FileContentHighlight string fileContent = string.Empty; foreach (var hit in rs.Hits) { foreach (var highlightField in hit.Highlight) { if (highlightField.Key == "fileContent") { hit.Source.FileContentHighlight = string.Empty; foreach (var highlight in highlightField.Value) { hit.Source.FileContentHighlight += highlight.ToString(); } } } } searchRs.Documents = rs.Documents; searchRs.Hits = rs.Hits; searchRs.PageData = new ESTestWeb.NEST.Model.PageParam(); //填充PageParam searchRs.PageData.PageIndex = pageIndex + 1; searchRs.PageData.PageSize = PageSize; searchRs.PageData.TotalCount = rs.Total; searchRs.PageData.Took = rs.Took; //var rs = m_client.Search<QGSSLuaXmlFile>(s => s.Index(idxName).Query(q => q.QueryString(qs => qs.Query(keyWord).DefaultOperator(Operator.Or)))); return(searchRs); }
public async Task <SearchRs> QueryByKeyWordAsync(string keyWord, string idxName, int pageIndex) { return(await Task.Run(() => { SearchRs searchRs = new SearchRs(); var rs = m_client.Search <QGSSLuaXmlFile>(s => s.Index(idxName).Query(q => q.QueryString(qs => qs.Query(keyWord).DefaultOperator(Operator.Or))) .Size(PageSize) .From(PageSize *pageIndex)); searchRs.Documents = rs.Documents; return searchRs; })); }