基本用法
-
安装Nest(安装后默认会装上Elasticsearch.Net),注意:版本尽量选择跟ES版本一致的
-
批量初始化数据到ES
using Nest;
try
{
// 测试环境配置SSL证书需要的设置
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => { return true; });
// 批量初始化数据到ES
var uri = new Uri(ElasticsearchUrl);
var setting = new ConnectionSettings(uri)
.DisableDirectStreaming()// 默认情况下,请求和响应字节在调试信息中不可用,但可以通过设置DisableDirectStreaming在“连接设置”上全局启用
.RequestTimeout(TimeSpan.FromSeconds(30))
.DefaultFieldNameInferrer(name => name)// 按字段命初始化,防止IndexMany出现大小写字段名问题
.BasicAuthentication(UserName, Password)// 设置账号密码
.DefaultIndex("order")
;
var client = new ElasticClient(setting);// 初始化ES客户端
var orders = GetOrders();// 从关系库获取你要初始化进ES的数据
var result = client.IndexMany(orders);
if (result.IsValid)
{
Console.WriteLine("success");
}
else
{
Console.WriteLine("fail");
}
}
catch (Exception ex)
{
}
- 删(官方抛弃过,实际使用会有点问题,实际开发中可以根据id去删除比较好,如果自定义Id在从关系库取数据的时候就能拿到Id了,不是自定义Id的话则要查询一次获取Id)
client.DeleteByQuery<ElasticsearchOrder>(d => d
.Query(q => q
.Match(m=>m.Field("Name").Query("DHL"))
)
);
- 改
client.UpdateByQuery<ElasticsearchOrder>(u => u
.Query(q => q
.Match(m => m.Field("Name").Query("DHL"))
)
);
- 查
var must = new List<Func<QueryContainerDescriptor<ElasticsearchOrder>, QueryContainer>>();
// 查询单字段
must.Add(a=>a
.Match(m=>m.Field("your field").Query("your value"))
);
// 查询 &&
must.Add(a => a
.Bool(b=>b
.Must(
m => m.Match(h => h.Field("your field").Query("your value")),
m => m.Match(h => h.Field("your field").Query("your value"))
)
)
);
// 查询日期
must.Add(a => a
.DateRange(r => r
.Field("your field").GreaterThanOrEquals(Convert.ToDateTime("your value"))
)
);
// 查询日期区间
must.Add(a => a
.Bool(b => b
.Must(
m => m.DateRange(r => r.Field("your field").GreaterThanOrEquals(Convert.ToDateTime("start time"))),
m => m.DateRange(r => r.Field("your field").LessThanOrEquals(Convert.ToDateTime("end time")))
)
)
);
// 查询数值区间
must.Add(a => a
.Bool(b => b
.Must(
m => m.Range(r => r.Field("your field").GreaterThanOrEquals(Convert.ToDouble("your value"))),
m => m.Range(r => r.Field("your field").LessThanOrEquals(Convert.ToDouble("your value")))
)
)
);
// 模糊查询(这个方式性能可能不好,还没展开细细研究)
var value = "your value";
must.Add(a => a
.Wildcard(w => w
.Field("PONO").Value($"*{value}*")
)
);
// 查询空字段
must.Add(a => a
.Bool(b => b
.Must(
m => m.Range(r => r.Field("your field").GreaterThan(0)
)
)
));
// 查询某对象为null或者某字段有多个值
must.Add(a => a
.Bool(b => b
.Should(s => s
.Bool(
l => l.MustNot(m => m.Exists(e => e.Field("your entity")))),
l => l.Terms(t => t.Field("your field").Terms("your value")
)
)
)
);
var from = 0;
var take = 10;
var should = new List<Func<QueryContainerDescriptor<ElasticsearchOrder>, QueryContainer>>();//如果有 || 需求使用
var res = client.Search<ElasticsearchOrder>(sd =>
sd.Query(q => q.Bool(b => b.Must(must).Should(should)))
.From(from)
.Take(take)
.Sort(s => s.Descending("InputTime"))
.TrackTotalHits(true)
);
if (res.IsValid)
{
}
else
{
}
标签:field,Nest,value,Field,Elasticsearch,var,Net,your,must
From: https://www.cnblogs.com/BOSET/p/17275702.html