NEST.Repository

A simple encapsulation with NEST client for search data form elasticsearch.

github

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的更多相关文章

  1. 基于node的tcp客户端和服务端的简单通信

    1.简单介绍下TCP/IP TCP/IP是互联网相关协议的集合,分为以下四层:应用层.传输层.网络层.数据链路层. 分成四层的好处是,假如只有一层,某个地方需要改变设计时,就必须把所有整体替换掉,而分 ...

  2. 基于Solr的HBase多条件查询测试

    背景: 某电信项目中采用HBase来存储用户终端明细数据,供前台页面即时查询.HBase无可置疑拥有其优势,但其本身只对rowkey支持毫秒级 的快 速检索,对于多字段的组合查询却无能为力.针对HBa ...

  3. 基于.NetCore开发博客项目 StarBlog - (13) 加入友情链接功能

    系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...

  4. 基于.NetCore开发博客项目 StarBlog - (14) 实现主题切换功能

    系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...

  5. 基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)

    系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...

  6. 实现基于NTP协议的网络校时功能

    无论PC端还是移动端系统都自带时间同步功能,基于的都是NTP协议,这里使用C#来实现基于NTP协议的网络校时功能(也就是实现时间同步). 1.NTP原理 NTP[Network Time Protoc ...

  7. 实现基于dotnetcore的扫一扫登录功能

    第一次写博客,前几天看到.netcore的认证,就心血来潮想实现一下基于netcore的一个扫一扫的功能,实现思路构思大概是web端通过cookie认证进行授权,手机端通过jwt授权,web端登录界面 ...

  8. 使用Surging Mqtt 开发基于WS的MqttClient客户端

    原文:使用Surging Mqtt 开发基于WS的MqttClient客户端 最近一段时间由于要做一套智能设备系统,而有幸了解到Surging中的Mqtt broker,学习了很多东西本篇文章基于Su ...

  9. TomatoLog 是一个基于 .NETCore 平台的产品。

    TomatoLog TomatoLog 是一个基于 .NETCore 平台的产品. The TomatoLog 是一个中间件,包含客户端.服务端,非常容易使用和部署. 客户端实现了ILoggerFac ...

随机推荐

  1. SpringBoot 异步线程简单三种样式

    引用:在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3.x ...

  2. JDK下载与安装、 Eclipse下载与使用的总结心得_20173311118_牛明旺

     一.JDK下载与安装心得: ① 从官网http://www.oracl.com/technetwork/java上下载JDK,注意一定要同意该网站上的协议,否则下载不了(即点击“Accept Lic ...

  3. POJ1095 Trees Made to Order(JAVA)

    这题用到了卡特兰数,比较麻烦.关于卡特兰数的基本概念百度一下你就知道. 使用卡特兰数对数组元素进行分组之后,需要具体计算一下要求的是第几组的第几个数,然后向下递归. 首先来看利用卡特兰数分组: 从1开 ...

  4. C#-WebForm JS定时器

    JS定时器: 1.window.setTimeout(function(){},3000) 延迟3秒执行 2.window.setInterval(function(){},3000) 也叫重复器,每 ...

  5. Flask-mail 发邮件慢(即使异步)

    Flask-mail 发邮件慢(即使异步) 一开始,按照狗书上的代码异步发邮件,但是发现原本响应只需要150ms的页面加了邮件发送就变成了5s响应(这怕不是假异步) 狗书的异步发邮件代码: def s ...

  6. 基于iTop4412的FM收音机系统设计(三)

    说明:第一版架构为:APP+JNI(NDK)+Driver(linux),优点是开发简单,周期短,也作为自己的毕业设计 现在更新第二版,FM服务完全植入Android系统中,成为系统服务,架构为:AP ...

  7. 【总结】sqli-labs Less(1-35) 小结

    0x00   工具 phpstudy2016:链接:http://pan.baidu.com/s/1bpbEBCj 密码:fmr4 sqli-labs-master:链接:http://pan.bai ...

  8. FAQ of db2fmp messages in the db2diag.log

    http://www-01.ibm.com/support/docview.wss?uid=swg21470035 Technote (FAQ) Question What do these mess ...

  9. IE8下不识别indexOf的问题

    1.为Array原型添加indexOf方法(如果学过面向对象,相当于给Array类添加实例方法),方法体如下: //添加数组IndexOf方法 if (!Array.prototype.indexOf ...

  10. wampserver实现外网访问

    1.打开运行WampServer3.0.4,鼠标移到wampserver上去,单击右键,出来个wamp Settings, 按照如图所示,选择Menu item : Online / Offline. ...