基于netcore对ElasitSearch客户端NEST查询功能的简单封装NEST.Repository
NEST.Repository
A simple encapsulation with NEST client for search data form elasticsearch.
API
NESTReaderRepository
TEntity Get(TKey id);
TEntity Get(Func<QueryContainerDescriptor<TEntity>, QueryContainer> filterExp = null,
Func<SourceFilterDescriptor<TEntity>, ISourceFilter> includeFieldExp = null,
Expression<Func<TEntity, object>> sortExp = null, SortOrder sortType = SortOrder.Ascending);
Tuple<long, List<TEntity>> GetList(Func<QueryContainerDescriptor<TEntity>, QueryContainer> filterExp = null,
Func<SourceFilterDescriptor<TEntity>, ISourceFilter> includeFieldExp = null,
Expression<Func<TEntity, object>> sortExp = null, SortOrder sortType = SortOrder.Ascending
, int limit = 10, int skip = 0)
NESTReaderRepositoryAsync
Task<TEntity> GetAsync(TKey id);
Task<TEntity> GetAsync(Func<QueryContainerDescriptor<TEntity>, QueryContainer> filterExp = null,
Func<SourceFilterDescriptor<TEntity>, ISourceFilter> includeFieldExp = null,
Expression<Func<TEntity, object>> sortExp = null, SortOrder sortType = SortOrder.Ascending);
Task<Tuple<long, List<TEntity>>> GetListAsync(Func<QueryContainerDescriptor<TEntity>, QueryContainer> filterExp = null,
Func<SourceFilterDescriptor<TEntity>, ISourceFilter> includeFieldExp = null,
Expression<Func<TEntity, object>> sortExp = null, SortOrder sortType = SortOrder.Ascending
, int limit = 0, int skip = 0)
Depend on
NEST 6.0.2
Repository.IEntity 2.0.1 (or you can write IEntity<T> interface and you entity inherit it.)
How to Use
First, you need have an entity inherit IEntity<T>, T is type of PrimaryKey. eg
[Serializable]
[BsonIgnoreExtraElements]
public class User : IEntity<long>
{
[BsonId]
public long ID { get; set; } public double Age { get; set; } public double Sex { get; set; } public string Like { get; set; }
}
Second, you need have a repository inherit NESTReaderRepository or NESTReaderRepositoryAsync. eg
public class UserRepo : NESTReaderRepository<User, long>
{
public static string connString = "http://localhost:9200/"; public UserRepo()
: base(connString)
{ }
}
Now, you can search data with simple api. eg
static void Main(string[] args)
{
Repository.Container.RepositoryContainer.Register<UserRepo>();
var userRepo = Repository.Container.RepositoryContainer.Resolve<UserRepo>(); var user = userRepo.Get(9);
var users = userRepo.GetList(
filterExp: q => +q.Range(r => r.Field(f => f.Age).GreaterThan(13).LessThan(28)),
includeFieldExp: p => p.Includes(i => i.Fields(f => f.Age, f => f.Sex, f => f.Like)),
sortExp: s => s.Age,
sortType: Nest.SortOrder.Ascending,
limit: 100,
skip: 0
);
}
How to write a Query
0x00. Structured Search
By default, documents will be returned in _score descending order, where the _score for each hit is the relevancy score calculated for how well the document matched the query criteria.
q => q.DateRange(r => r
.Field(f => f.{Field with DateTime Type})
.GreaterThanOrEquals(new DateTime(2017, 01, 01))
.LessThan(new DateTime(2018, 01, 01))
)
The benefit of executing a query in a filter context is that Elasticsearch is able to forgo calculating a relevancy score, as well as cache filters for faster subsequent performance.
q => q.Bool(b => b.Filter(bf => bf
.DateRange(r => r
.Field(f => f.{Field with DateTime Type})
.GreaterThanOrEquals(new DateTime(2017, 01, 01))
.LessThan(new DateTime(2018, 01, 01))
)
)
)
0x01. Unstructured Search
Full text queries (find all documents that contain "Russ" in the lead developer first name field)
q => q.Match(m => m
.Field(f => f.LeadDeveloper.FirstName)
.Query("Russ")
)
0x02. Combining Search
q => q.Bool(b => b
.Must(mu => mu
.Match(m => m
.Field(f => f.LeadDeveloper.FirstName)
.Query("Russ")
), mu => mu
.Match(m => m
.Field(f => f.LeadDeveloper.LastName)
.Query("Cam")
)
)
.Filter(fi => fi
.DateRange(r => r
.Field(f => f.StartedOn)
.GreaterThanOrEquals(new DateTime(2017, 01, 01))
.LessThan(new DateTime(2018, 01, 01))
)
)
)
use operator
q => q.Match(m => m
.Field(f => f.LeadDeveloper.FirstName)
.Query("Russ")
) && q
.Match(m => m
.Field(f => f.LeadDeveloper.LastName)
.Query("Cam")
) && +q
.DateRange(r => r
.Field(f => f.StartedOn)
.GreaterThanOrEquals(new DateTime(2017, 01, 01))
.LessThan(new DateTime(2018, 01, 01))
)
)
Should ==> OR ==> ||
Must ==> And ==> &&
Must_Not ==> NOT==> !
Filter ==> +
the query will be converted to a bool query if use any operator, and the answer to the bool query is always yes or no , so that will not score.
Reference
https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/writing-queries.html
https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/bool-queries.html
基于netcore对ElasitSearch客户端NEST查询功能的简单封装NEST.Repository的更多相关文章
- 基于node的tcp客户端和服务端的简单通信
1.简单介绍下TCP/IP TCP/IP是互联网相关协议的集合,分为以下四层:应用层.传输层.网络层.数据链路层. 分成四层的好处是,假如只有一层,某个地方需要改变设计时,就必须把所有整体替换掉,而分 ...
- 基于Solr的HBase多条件查询测试
背景: 某电信项目中采用HBase来存储用户终端明细数据,供前台页面即时查询.HBase无可置疑拥有其优势,但其本身只对rowkey支持毫秒级 的快 速检索,对于多字段的组合查询却无能为力.针对HBa ...
- 基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...
- 基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...
- 基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...
- 实现基于NTP协议的网络校时功能
无论PC端还是移动端系统都自带时间同步功能,基于的都是NTP协议,这里使用C#来实现基于NTP协议的网络校时功能(也就是实现时间同步). 1.NTP原理 NTP[Network Time Protoc ...
- 实现基于dotnetcore的扫一扫登录功能
第一次写博客,前几天看到.netcore的认证,就心血来潮想实现一下基于netcore的一个扫一扫的功能,实现思路构思大概是web端通过cookie认证进行授权,手机端通过jwt授权,web端登录界面 ...
- 使用Surging Mqtt 开发基于WS的MqttClient客户端
原文:使用Surging Mqtt 开发基于WS的MqttClient客户端 最近一段时间由于要做一套智能设备系统,而有幸了解到Surging中的Mqtt broker,学习了很多东西本篇文章基于Su ...
- TomatoLog 是一个基于 .NETCore 平台的产品。
TomatoLog TomatoLog 是一个基于 .NETCore 平台的产品. The TomatoLog 是一个中间件,包含客户端.服务端,非常容易使用和部署. 客户端实现了ILoggerFac ...
随机推荐
- express 直接返回HTML文件
一般情况下用的是模板引擎,如jade: res.render('detail',{ // 使用render() #http://www.expressjs.com.cn/4x/api.html#res ...
- byte转文件流 下载到本地
此方法将byte类型文件转为文件流保存到本地 byte 经过BASE64Decoder 进行编码之后的类型 所以需要解码 防止出现乱码及文件损毁 /** * byte 转文件 下载到本地 * @par ...
- Linux(ubuntu18.04)切换python版本
前言 Ubuntu18.04系统在安装python时会安装两个版本:2.7和3.6.默认情况下系统环境使用的是python2,但是我们有时需要使用python3来作为我们的开发环境,所以需要自由切换p ...
- JS对Date的扩展,将 Date 转化为指定格式的String
/** * 对Date的扩展,将 Date 转化为指定格式的String * 月(M).日(d).12小时(h).24小时(H).分(m).秒(s).周(E).季度(q) 可以用 1-2 个占位符 * ...
- Dijkstra+优先队列 堆优化
关于堆优化 传统\(Dijkstra\),在选取中转站时,是遍历取当前最小距离节点,但是我们其实可以利用小根堆(STL的priority_queue)优化这个过程,从而大大降低复杂(\(O(V2+E) ...
- webpack/gulp的z-index被改写
webpack方法 new OptimizeCSSPlugin({ cssProcessorOptions: { safe: true } }) gulp-cssnano 方法 .pipe(cssna ...
- Windows开发经验 - WinDbg
1. 远程调试 参考文章:https://docs.microsoft.com/en-us/windows-hardware/drivers/debugger/remode-debugging-usi ...
- Mac 10.12安装OpenVPN客户端
说明: 1.在Mac下有很多漂亮的客户端可以安装,比如Tunnelblick这些等等. 2.但这里直接先原版的OpenVPN进行搭建,这个比较爽. 安装: brew install openvpn 提 ...
- 完全国人自主研发原创的智能软件路由器BDS即将发布,附带企业服务总线ESB功能
完全国人自主研发原创的智能软件路由器即将发布: 完全国人自主研发原创的智能软件路由器BDS即将发布,附带企业服务总线ESB功能 智能软件路由器 BDS 简要介绍 http://kan.weibo.co ...
- (转)深入剖析Redis主从复制
一.主从概述 Redis 支持 Master-Slave(主从)模式,Redis Server 可以设置为另一个 Redis Server 的主机(从机),从机定期从主机拿数据.特殊的,一个从机同样可 ...