本文介紹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的數據了