NEST.net Client For Elasticsearch简单应用
查询入口
{
IndustryPost = industryPost,
Region = region,
MinSalary = minSalary,
};
return FailActionRequest(gl.webapi_error);
if (gl.total <= 0)
{
return OkActionRequest(gl);
}
return OkListActionRequest(gl);
QueryPositionsAsync 部分代码
//查看搜索语句
//var temp= Encoding.UTF8.GetString(resultData.RequestInformation.Request).Trim();
if (!resultData.Hits.Any())
{
return new GListResult<eJobPositionSimple>(new eJobPositionSimple[0]);
}
var result = resultData.Documents;
var g1 = result.Select(position => new eJobPositionSimple
{
Pstn_Id = position._id,
...
Phone = position.Phone,
Name = position.BrandName,
}).ToList();
return new GListResult<eJobPositionSimple>(g1, (int) resultData.Total);
QueryAsync部分代码
/// 生成搜索客户端
/// </summary>
/// <returns></returns>
private static ElasticClient GetSearchClient()
{
var connectString = ConfigurationManager.ConnectionStrings["ElasticSearch"].ConnectionString;
var nodesStr = connectString.Split('|');
var nodes = nodesStr.Select(s => new Uri(s)).ToList();
var connectionPool = new SniffingConnectionPool(nodes);
var settings = new ConnectionSettings(connectionPool).SetDefaultIndex("position");
settings.SetDefaultPropertyNameInferrer(p => p.ToString());
var client = new ElasticClient(settings);
return client;
}
{
var client = GetSearchClient();
var resultData = await client.SearchAsync<QPosition>(s =>
s.Index("position").Type("tbJobPosition").
Query(
q => QueryContainerPosition(condition, searchText, q)).
SortDescending(f => f.UpdateTime).Skip(skip).Take(top));
return resultData;
}
/// 生成职位查询表达式
/// </summary>
/// <param name="condition">职位条件</param>
/// <param name="searchText">查询的关键字</param>
/// <param name="q">用于返回的查询表达式</param>
/// <returns>返回用于搜索的表达式</returns>
private static QueryContainer QueryContainerPosition(eJobQueryPosition condition, string searchText, QueryDescriptor<QPosition> q)
{
var query = new QueryContainer();
//未过有效期
query &= q.Range(rr => rr.OnField(ff => ff.ExpireTime).GreaterOrEquals(DateTime.UtcNow.Date));
if (!string.IsNullOrWhiteSpace(searchText))
{
query &= QueryText(searchText, q, query);
}
return query;
query1 = q.Term(s => s.OnField(ff => ff.BrandName).Value("szbrandname1"));
var query2 = new QueryContainer();
query2 = q.Term(s => s.OnField(ff => ff.BrandName).Value("szbrandname2"));
var query3 = new QueryContainer();
query2 = q.Term(s => s.OnField(ff => ff.BrandName).Value("szbrandname3"));
query = query1 || query2 || query3;
}
/// 搜索文本表达式,一定要放在所有表达式的最后
/// </summary>
/// <param name="searchText">搜索文本</param>
/// <param name="q">搜索表达式</param>
/// <param name="query">要返回的搜索表达式类</param>
/// <returns></returns>
private static QueryContainer QueryText(string searchText, QueryDescriptor<QPosition> q, QueryContainer query)
{
if (!string.IsNullOrWhiteSpace(searchText))
{
query = q.MatchPhrase(p => p.OnField(o => o.Descript).Query(searchText));
}
return query;
}
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using **.Entity;
using Elasticsearch.Net.ConnectionPool;
using Nest;