一、如何使用NEST客户端
1、连接(参考文档:http://nest.azurewebsites.net/nest/quick-start.html)
var node = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(
node,
defaultIndex: "test"
);
var client = new ElasticClient(settings);
2、创建索引
/// <summary>
/// 创建索引
/// </summary>
/// <param name="indexname">索引名称</param>
/// <param name="NumberOfShard">分片数量</param>
/// <param name="NumberOfReplica">每个分片副本的数量</param>
public static void CreateIndex(string indexName, int numberOfShard, int numberOfReplica)
{
var descriptor = new CreateIndexDescriptor(indexName)
.Settings(s => s.NumberOfShards(numberOfShard).NumberOfReplicas(numberOfReplica));
client.CreateIndex(descriptor);
}
3、删除索引
/// <summary>
/// 删除索引
/// </summary>
/// <param name="indexName">索引名称</param>
public static void DeleteIndex(string indexName)
{
var descriptor = new DeleteIndexDescriptor(indexName).Index(indexName);
client.DeleteIndex(descriptor);
}
4、添加数据
/// <summary>
/// 新增一条数据
/// </summary>
/// <param name="obj"></param>
public static void Insert(MeetupEvents obj)
{
client.Index(obj);
}
/// <summary>
/// 新增多条数据
/// </summary>
/// <param name="list"></param>
public static void InsertBulk(List<MeetupEvents> list)
{
client.IndexMany(list);
}
二、查询
测试数据介绍:总共7条数据
1、匹配所有文档
结构化查询:
{
"query":{
"match_all": {}
}
}
代码实现:
Func<SearchDescriptor<MeetupEvents>, ISearchRequest> searchRequest;
searchRequest = r => r.Query(s => s.MatchAll());
var result= ESHelper.client.Search<MeetupEvents>(searchRequest);
List<MeetupEvents> list = result.Documents.ToList();
2、或者-or
{
"query": {
"bool": {
"should": [
{
"match": {
"eventname": "红楼梦"
}
},
{
"match": {
"description": "姥姥"
}
}
]
}
}
}
结果如下:
代码实现:
public class QueryInfo
{
public string andinfo { get; set; }
public string orinfo { get; set; }
}
QueryInfo info = new QueryInfo()
{
orinfo = "红楼梦 姥姥"
};
var search =ESHelper.client.Search<MeetupEvents>(s => s
.Query(q => q
.Bool(m => m
.Should(a => a.Match(b => b.Field(c => c.eventname).Query(info.orinfo)),
a => a.Match(b => b.Field(c => c.description).Query(info.orinfo)))
)).From(0)
.Size(10));
List<MeetupEvents> list = search.Documents.ToList();
3、并且-and
{
"query": {
"bool": {
"must": [
{
"match": {
"eventname": "红楼梦 "
}
},
{
"match": {
"description": "贾宝玉"
}
}
]
}
}
}
代码实现:
QueryInfo info = new QueryInfo()
{
andinfo = "红楼梦 贾宝玉"
};
var search =ESHelper.client.Search<MeetupEvents>(s => s
.Query(q => q
.Bool(m => m
.Must(a => a.Match(b => b.Field(c => c.eventname).Query(info.andinfo)),
a => a.Match(b => b.Field(c => c.description).Query(info.andinfo)))
)).From(0)
.Size(10));
List<MeetupEvents> list = search.Documents.ToList();
4、and-or组合查询
{
"query": {
"bool": {
"should": [
{
"match": {
"eventname": "水浒传"
}
},
{
"bool": {
"must": [
{
"match": {
"eventname": "红楼梦 贾宝玉"
}
},
{
"match": {
"description": "红楼梦 贾宝玉"
}
}
]
}
}
]
}
}
}
代码实现:
QueryInfo info = new QueryInfo()
{
andinfo = "红楼梦 贾宝玉",
orinfo= "水浒传"
};
var search = ESHelper.client.Search<MeetupEvents>(s => s
.Query(q => q
.Bool(m => m
.Should(a => a.Match(b => b.Field(c => c.eventname).Query(info.orinfo)),
a => a.Bool(f => f.Must(x => x.Match(b => b.Field(c => c.eventname).Query(info.andinfo)),
x => x.Match(b => b.Field(c => c.description).Query(info.andinfo)))))
)).From(0)
.Size(10));
List<MeetupEvents> list = search.Documents.ToList();
5、querystring条件组合查询
{
"query": {
"query_string": {
"query": "(+红楼梦 +贾宝玉) |水浒传",
"fields": [
"_all"
],
"default_operator": "or"
}
},
"size": 10
}
代码实现:
string str = "(+红楼梦 +贾宝玉) |水浒传";
var search1 = ESHelper.client.Search<MeetupEvents>(s => s
.Query(x=>x.QueryString(n=>n.Query(str).DefaultOperator(Operator.Or))));
List<MeetupEvents> list = search1.Documents.ToList();
参考:https://elasticsearch.cn/question/2124
6、term精准查询
{
"query": {
"bool": {
"must": [
{
"term": {
"eventname": "西游记"
}
}
]
}
},
"from": 0,
"size": 10
}
代码实现:
Func<SearchDescriptor<MeetupEvents>, ISearchRequest> searchRequest;
searchRequest = r => r.Query(s => s.Term(x=>x.eventname,"西游记"));
var result = ESHelper.client.Search<MeetupEvents>(searchRequest).Documents.ToList();
foreach (var item in result)
{
string s = string.Format("name:{0},description:{1}", item.eventname, item.description);
Console.WriteLine(s);
}
【注意:】
刚开始我使用上面的查询方式,是无法查出数据的。
原因:ES5.0及以后的版本取消了string类型,将原先的string类型拆分为text和keyword两种类型。它们的区别在于text会对字段进行分词处理而keyword则不会。ES的term query做的是精确匹配而不是分词查询,因此对text类型的字段做term查询将是查不到结果的(除非字段本身经过分词器处理后不变,未被转换或分词)。
解决方法:映射的时候将字段配置为keyword类型。
参考:https://segmentfault.com/q/1010000017312707