发布:2023/1/13 16:28:21作者:管理员 来源:本站 浏览次数:778
本文介绍C#使用Elasticsearch的基本方法,并提供一个demo
以下说明中包含的http调用,为ElasticsearchTestController中编写的测试方法
初始化
引用NEST
创建ElasticClient对象
ElasticClient elasticClient = new ElasticClient(new ConnectionSettings(new Uri(address));
新增索引
关键代码
CreateIndexResponse createIndexResponse = await elasticClient.Indices.CreateAsync(indexName, createIndexDescriptor =>
{
return createIndexDescriptor.
Map(typeMappingDescriptor =>
{
return typeMappingDescriptor.Properties(propertiesSelector =>
{
foreach (PropertyInfo propertyInfo in typeof(StudentForElasticsearch).GetProperties())
{
if (!propertyInfo.CanWrite)
continue;
switch (propertyInfo.PropertyType.Name)
{
case nameof(Int16):
case nameof(Int32):
case nameof(Int64):
case nameof(UInt16):
case nameof(UInt32):
case nameof(UInt64):
case nameof(Decimal):
case nameof(Single):
case nameof(Double):
case nameof(Byte):
propertiesSelector = propertiesSelector.Number(propertyDescriptor => propertyDescriptor.Name(ToJavaScriptPropertyName(propertyInfo.Name)));
break;
case nameof(Boolean):
propertiesSelector = propertiesSelector.Boolean(propertyDescriptor => propertyDescriptor.Name(ToJavaScriptPropertyName(propertyInfo.Name)));
break;
case nameof(DateTime):
propertiesSelector = propertiesSelector.Date(propertyDescriptor => propertyDescriptor.Name(ToJavaScriptPropertyName(propertyInfo.Name)));
break;
case nameof(String):
propertiesSelector = propertiesSelector.Keyword(propertyDescriptor => propertyDescriptor.Name(ToJavaScriptPropertyName(propertyInfo.Name)));
break;
default:
break;
}
}
return propertiesSelector;
});
});
});
为索引添加别名
PutAliasResponse putAliasResponse = await elasticClient.Indices.PutAliasAsync(indexName, indexAliasName);
调用http://localhost:5000/api/ElasticsearchTest/CreateIndex?indexName=stu&indexAliasName=stuAliasName创建索引
查询索引结构
GET
http://xxxxxxx:9200/stu/_mapping
返回结果
{
"stu": {
"mappings": {
"properties": {
"email": {
"type": "keyword"
},
"id": {
"type": "float"
},
"name": {
"type": "keyword"
}
}
}
}
}
新增数据
关键代码
BulkResponse bulkResponse = await elasticClient.BulkAsync(bulkDescriptor =>
{
foreach (StudentForElasticsearch document in datas)
{
bulkDescriptor = bulkDescriptor.Index<StudentForElasticsearch>(bulkIndexDescriptor =>
{
return bulkIndexDescriptor.Index(indexAliasName).Id(document.Id).Document(document);
});
}
return bulkDescriptor;
});
调用http://localhost:5000/api/ElasticsearchTest/AddOrUpdateData?indexAliasName=stuAliasName新增数据
此时查询数据
GET
http://xxxxxxx:9200/stu/_search
{
"from": 0,
"size": 1000
}
返回结果
{
"took": 33,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1.0,
"hits": [
{
"_index": "stu",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"id": 1,
"name": "Student1",
"email": "11111@qq.com"
}
},
{
"_index": "stu",
"_type": "_doc",
"_id": "2",
"_score": 1.0,
"_source": {
"id": 2,
"name": "Student2",
"email": "2222@qq.com"
}
}
]
}
}
查询
关键代码
ISearchResponse<StudentForElasticsearch> searchResponse = await elasticClient.SearchAsync<StudentForElasticsearch>(searchDescriptor =>
{
return searchDescriptor.Index(indexAliasName).Query(queryContainerDescriptor =>
{
return queryContainerDescriptor.Bool(boolQueryDescriptor =>
{
if (!string.IsNullOrEmpty(name))
{
IList<Func<QueryContainerDescriptor<StudentForElasticsearch>, QueryContainer>> queryContainers = new List<Func<QueryContainerDescriptor<StudentForElasticsearch>, QueryContainer>>();
queryContainers.Add(queryContainerDescriptor =>
{
return queryContainerDescriptor.Wildcard(c => c
.Field(ToJavaScriptPropertyName(nameof(StudentForElasticsearch.Name))).Value($"{name}*"));
});
boolQueryDescriptor.Must(x => x.Bool(b => b.Should(queryContainers)));
}
return boolQueryDescriptor;
});
})
.From(0).Size(10);
});
这里采用Wildcard查询,并且只查询前10个匹配数据
调用http://localhost:5000/api/ElasticsearchTest/Get?indexAliasName=stuAliasName&name=Student2
返回结果
[{"id":2,"name":"Student2","email":"2222@qq.com"}]
删除数据
关键代码
BulkResponse bulkResponse = await elasticClient.BulkAsync(bulkDescriptor =>
{
foreach (int id in deleteIds)
{
bulkDescriptor = bulkDescriptor.Delete<StudentForElasticsearch>(bulkIndexDescriptor =>
{
return bulkIndexDescriptor.Index(indexAliasName).Id(id);
});
}
return bulkDescriptor;
});
调用http://localhost:5000/api/ElasticsearchTest/DeleteData?indexAliasName=stuAliasName
此时再查询数据,只剩下id为2的数据了
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4