基于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 ...
随机推荐
- javassist学习
最近学习了javassist的使用方法. javassist是一个字节码类库,可以用他来动态生成类,动态修改类等等. 下面是如果用javassist来动态创建一个类的demol 我们需要创建的目标类, ...
- Bootstrap Modal 关闭时右侧滚动条消失,页面左移的解决方法
问题描述:页面在打开Modal之前右侧有滚动条,Modal关闭之后,body中的class="modal-open"和style="padding-right: 17px ...
- Ionic2 自学须知的基本知识点
http://www.cnblogs.com/zsl123/p/5991336.html Ionic(ionicframework)一款接近原生的HTML5移动App开发框架. IONIC 是目前最有 ...
- Apache Maven的入门使用之常用操作以及核心概念介绍(2)
我们接着上篇文章,来继续介绍Maven中几个核心的概念: POM (Project Object Model) Maven 插件 Maven 生命周期 Maven 依赖管理 Maven 库 POM ( ...
- kubernetes pod termination pending
在将k8s从1.7.9 升级到1.10.2 之后,发现删除pod一直处于terminating状态, 调查发现删不掉的pod都有一个特点就是pod yaml中command部分写错了,如下所示: ap ...
- spring boot快速入门 1 :创建项目、 三种启动项目方式
准备工作: (转载)IDEA新建项目时,没有Spring Initializr选项 最近开始使用IDEA作为开发工具,然后也是打算开始学习使用spring boot. 看着博客来进行操作上手sprin ...
- Map集合的四种遍历方式(转载)
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class TestMap { pu ...
- phpqrcode实现二维码(含图片)
---恢复内容开始--- 1,http://phpqrcode.sourceforge.net/ 下载 2,解压以后只需要一个文件 3,原生php测试: <?php include 'phpqr ...
- Linux 命令 su 和 sudo 比较
su : 可以用来切换到任何用户,但是要求输入目标用户的密码 比如:su - su - root (等价 su -) su - user su root 与 su - root 的区别在于,后者不切换 ...
- JavaScript设计模式-21.命令模式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...