.net NEST7.17.0 ES客户端帮助类
分页模型
public class PageModel where T : class
{
///
/// 总数
///
public long TotalCount { get; set; }
///
/// 文档数据
///
public List Data { get; set; }
}
帮助类
public static class EsUtil
{
///
/// 获取ElasticClient
///
/// ElasticSearch服务器地址
/// 默认索引名称
///
public static ElasticClient Client(string url, string defaultIndex = "")
{
Uri uri = new Uri(url);
ConnectionSettings setting = new ConnectionSettings(uri);
if (!string.IsNullOrWhiteSpace(defaultIndex))
{
setting.DefaultIndex(defaultIndex);
}
return new ElasticClient(setting);
}
///
/// 获取ElasticClient
///
/// ElasticSearch集群地址
/// 默认索引名称
///
public static ElasticClient Client(string[] urls, string defaultIndex = "")
{
Uri[] uris = urls.Select(h => new Uri(h)).ToArray();
SniffingConnectionPool pool = new SniffingConnectionPool(uris);
ConnectionSettings setting = new ConnectionSettings(pool);
if (!string.IsNullOrWhiteSpace(defaultIndex))
{
setting.DefaultIndex(defaultIndex);
}
return new ElasticClient(setting);
}
/////
///// 如果同名索引不存在则创建索引
/////
/////
///// ElasticClient实例
///// 要创建的索引名称
///// 默认副本数量,如果是单实例,注意改成0
///// 默认分片数量
/////
//public static bool CreateIndex(this ElasticClient client, string indexName = "", int numberOfReplicas = 1, int numberOfShards = 5) where T : class
//{
// if (client.IndexExists(indexName).Exists) return false;
// var indexState = new IndexState
// {
// Settings = new IndexSettings
// {
// NumberOfReplicas = numberOfReplicas, //副本数
// NumberOfShards = numberOfShards //分片数
// }
// };
// if (string.IsNullOrWhiteSpace(indexName))
// {
// indexName = typeof(T).Name.ToLower();
// }
// var result = client.CreateIndex(indexName, c => c.InitializeUsing(indexState).Mappings(ms => ms.Map(m => m.AutoMap())));
// return result.Acknowledged;
//}
///
/// 创建文档
///
///
///
///
///
public static bool Create(this ElasticClient client, T obj) where T : class
{
CreateRequest log = new CreateRequest(Guid.NewGuid());
log.Document = obj;
CreateResponse result = client.Create(log);
return result.IsValid;
}
///
/// 删除文档
///
///
///
///
///
public static bool Delete(this ElasticClient client, string id) where T : class
{
DeleteResponse result = client.Delete(new DocumentPath(id));
return result.IsValid;
}
///
/// 更新文档
///
///
///
///
///
///
public static bool Update(this ElasticClient client, string id, T obj) where T : class
{
UpdateRequest data = new UpdateRequest(Guid.NewGuid());
UpdateResponse result = client.Update(new DocumentPath(id), f => f.Doc(obj));
CheckValid(result);
return result.IsValid;
}
///
/// 获取文档
///
///
///
///
///
public static T Get(this ElasticClient client, string id) where T : class
{
GetResponse data = client.Get(id);
CheckValid(data);
return data.Source;
//var tweet = JsonConvert.SerializeObject(stu);
}
///
/// 获取文档list
///
///
///
///
///
public static List GetList(this ElasticClient client, List ids) where T : class
{
var data = client.Search(f => f.Query(q => q.Ids(id => id.Values(ids))));
CheckValid(data);
return data.Documents.ToList();
}
///
/// 获取分页文档
///
///
///
///
///
///
///
///
public static PageModel GetPageList(this ElasticClient client, Func, QueryContainer> query, int pageIndex = 1, int pageSize = 10) where T : class
{
if (pageIndex <= 0 || pageSize <= 0)
{
throw new Exception("ErrorPageInfo");
}
//,
var data = client.Search(s => s
.From(pageIndex - 1)
.Size(pageSize)
.Query(query));
// var stus = client.Search(s => s
//.From(0)
//.Size(10)
//.Query(q =>
// q.Term(t => t.name, "jim")
// || q.Match(mq => mq.Field(f => f.age).Query("1"))
// )
// );
CheckValid(data);
return new PageModel
{
Data= data.Documents.ToList(),
TotalCount= data.HitsMetadata.Total.Value
};
}
private static void CheckValid(IResponse response) where T : class
{
if (!response.IsValid)
{
throw response.OriginalException;
}
}
///
/// 返回一个正序排列的委托
///
///
///
///
public static Func, SortDescriptor> Sort(string field) where T : class
{
return sd => sd.Ascending(field);
}
public static Func, SortDescriptor> Sort(Expression> field) where T : class
{
return sd => sd.Ascending(field);
}
///
/// 返回一个倒序排列的委托
///
///
///
///
public static Func, SortDescriptor> SortDesc(string field) where T : class
{
return sd => sd.Descending(field);
}
public static Func, SortDescriptor> SortDesc(Expression> field) where T : class
{
return sd => sd.Descending(field);
}
///
/// 返回一个Must条件集合
///
///
///
public static List, QueryContainer>> Must() where T : class
{
return new List, QueryContainer>>();
}
public static List, QueryContainer>> Should() where T : class
{
return new List, QueryContainer>>();
}
///
/// 添加Match子句
///
///
///
/// 要查询的列
/// 要查询的关键字
///
public static void AddMatch(this List, QueryContainer>> musts, string field,
string value, double? boost = null) where T : class
{
musts.Add(d => d.Match(mq => mq.Field(field).Query(value).Boost(boost)));
}
public static void AddMatch(this List, QueryContainer>> musts,
Expression> field, string value) where T : class
{
musts.Add(d => d.Match(mq => mq.Field(field).Query(value)));
}
///
/// 添加MultiMatch子句
///
///
///
/// 要查询的列
/// 要查询的关键字
public static void AddMultiMatch(this List, QueryContainer>> musts,
string[] fields, string value) where T : class
{
musts.Add(d => d.MultiMatch(mq => mq.Fields(fields).Query(value)));
}
///
/// 添加MultiMatch子句
///
///
///
/// 例如:f=>new [] {f.xxx, f.xxx}
/// 要查询的关键字
public static void AddMultiMatch(this List, QueryContainer>> musts,
Expression> fields, string value) where T : class
{
musts.Add(d => d.MultiMatch(mq => mq.Fields(fields).Query(value)));
}
///
/// 添加大于子句
///
///
///
/// 要查询的列
/// 要比较的值
public static void AddGreaterThan(this List, QueryContainer>> musts, string field,
double value) where T : class
{
musts.Add(d => d.Range(mq => mq.Field(field).GreaterThan(value)));
}
public static void AddGreaterThan(this List, QueryContainer>> musts,
Expression> field, double value) where T : class
{
musts.Add(d => d.Range(mq => mq.Field(field).GreaterThan(value)));
}
///
/// 添加大于等于子句
///
///
///
/// 要查询的列
/// 要比较的值
public static void AddGreaterThanEqual(this List, QueryContainer>> musts, string field,
double value) where T : class
{
musts.Add(d => d.Range(mq => mq.Field(field).GreaterThanOrEquals(value)));
}
public static void AddGreaterThanEqual(this List, QueryContainer>> musts,
Expression> field, double value) where T : class
{
musts.Add(d => d.Range(mq => mq.Field(field).GreaterThanOrEquals(value)));
}
///
/// 添加小于子句
///
///
///
/// 要查询的列
/// 要比较的值
public static void AddLessThan(this List, QueryContainer>> musts, string field,
double value) where T : class
{
musts.Add(d => d.Range(mq => mq.Field(field).LessThan(value)));
}
public static void AddLessThan(this List, QueryContainer>> musts,
Expression> field, double value) where T : class
{
musts.Add(d => d.Range(mq => mq.Field(field).LessThan(value)));
}
///
/// 添加小于等于子句
///
///
///
/// 要查询的列
/// 要比较的值
public static void AddLessThanEqual(this List, QueryContainer>> musts, string field,
double value) where T : class
{
musts.Add(d => d.Range(mq => mq.Field(field).LessThanOrEquals(value)));
}
public static void AddLessThanEqual(this List, QueryContainer>> musts,
Expression> field, double value) where T : class
{
musts.Add(d => d.Range(mq => mq.Field(field).LessThanOrEquals(value)));
}
///
/// 添加一个Term,一个列一个值
///
///
///
/// 要查询的列
/// 要比较的值
public static void AddTerm(this List, QueryContainer>> musts, string field,
object value) where T : class
{
musts.Add(d => d.Term(field, value));
}
public static void AddTerm(this List, QueryContainer>> musts,
Expression> field, object value) where T : class
{
musts.Add(d => d.Term(field, value));
}
///
/// 添加一个Terms,一个列多个值
///
///
///
///
///
public static void AddTerms(this List, QueryContainer>> musts, string field,
object[] values) where T : class
{
musts.Add(d => d.Terms(tq => tq.Field(field).Terms(values)));
}
public static void AddTerms(this List, QueryContainer>> musts,
Expression> field, object[] values) where T : class
{
musts.Add(d => d.Terms(tq => tq.Field(field).Terms(values)));
}
}