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. [CentOS] 7 不执行文件 /etc/rc.d/rc.local

    chmod 0755 /etc/rc.local systemctl enable rc-local.service --now systemctl restart rc-local.service

  2. 不信任的 .exe 怎么办,用 Windows 沙盒啊!

    简评:维基百科,在计算机安全领域,沙盒(sandbox)是种安全机制,为执行中的程式提供的隔离环境.通常是作为一些来源不可信.具破坏力或无法判定程序意图的程序提供实验之用. 微软正在尝试解决人们对运行 ...

  3. Spring aop+自定义注解统一记录用户行为日志

    写在前面 本文不涉及过多的Spring aop基本概念以及基本用法介绍,以实际场景使用为主. 场景 我们通常有这样一个需求:打印后台接口请求的具体参数,打印接口请求的最终响应结果,以及记录哪个用户在什 ...

  4. Java中常用到的文件操作那些事(二)——使用POI解析Excel的两种常用方式对比

    最近生产环境有个老项目一直内存报警,不时的还出现内存泄漏,导致需要重启服务器,已经严重影响正常服务了.获取生成dump文件后,使用MAT工具进行分析,发现是其中有个Excel文件上传功能时,经常会导致 ...

  5. Linux系统NAT模式下设置网络网关

    1.配置Vm网络编辑器 2.配置固定IP地址 命令:vi /etc/sysconfig/network-scripts/ifcfg-ens33 #下面内容直接复制进去,如果有重复的可以去除 TYPE= ...

  6. POJ 2234

    #include<iostream> #include<stdio.h> #include<algorithm> #define MAXN 100 using na ...

  7. Luogu P1342 请柬 题解

    差不多是Dijkstra的裸题吧... 这道题可以分为来回两个阶段. 去的时候很简单,直接用一次Dijkstra,然后统计答案. 回来的时候就有些巧妙了,虽然表面上是每个点回到起点,但是何尝不可将其看 ...

  8. jinja url_for js 参数

    在JavaScript中,也就是客户端,向flask路由服务器端使用post请求并在url_for中传递参数,服务器端获取不到该参数, Jinja不能使用Javascript变量,如下所示: var ...

  9. JAVA数据结构--Array数组实现

    所谓数组,是有序的元素序列. [1]  若将有限个类型相同的变量的集合命名,那么这个名称为数组名.组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量.用于区分数组的各个元素的数字编 ...

  10. 关于ubuntu环境下gcc使用的几点说明

    sudo apt-get build-dep gcc //安装gcc编译器 /* 假设已经创建hello.c文件 */ //方法一 $gcc hello.c //将源文件直接编译成文件名为a.out的 ...