Elasticsearch server
curl -X GET "localhost:9200/"
{
"name" : "debian",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "xtNEuvEeRVmtK9iCT2WWyQ",
"version" : {
"number" : "9.1.4",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "0b7fe68d2e369469ff9e9f344ab6df64ab9c5293",
"build_date" : "2025-09-16T22:05:19.073893347Z",
"build_snapshot" : false,
"lucene_version" : "10.2.2",
"minimum_wire_compatibility_version" : "8.19.0",
"minimum_index_compatibility_version" : "8.0.0"
},
"tagline" : "You Know, for Search"
}
.Net Client
Elastic.Clients.Elasticsearch 9.18
#region 01-创建索引(带分词器配置)
/// <summary>
/// 01-创建索引(带分词器配置)
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task CreateIndexAsync()
{
var exists = await _client.Indices.ExistsAsync(indexName);
if (exists.Exists) return;
var response = await _client.Indices.CreateAsync(indexName, c => c
.Settings(s => s.NumberOfShards(1).NumberOfReplicas(0))
.Mappings(m => m
.Properties<Book>(p => p
.Keyword(k => k.id)
.Text(t => t.title, f => f.Analyzer("ik_max_word")) // 中文分词(需安装IK插件)
.Text(t => t.author, f => f.Analyzer("ik_smart")) // 中文分词(需安装IK插件)
.Keyword(t=>t.category)
.FloatNumber(n => n.price)
.Date(d => d.published_date)
.Keyword(k => k.tags) // 标签不分词,用于精确查询
)
)
);
if (!response.IsValidResponse)
throw new Exception($"创建索引失败: {response.DebugInformation}");
}
#endregion
https://www.cnblogs.com/yaopengfei/p/18984246
2.create a mapping
curl -XPOST http://localhost:9200/index/_mapping -H 'Content-Type:application/json' -d'
{
"properties": {
"content": {
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_smart"
}
}
}'
View the mapping of an index
GET /my-index-000001/_mapping
{
"my-index-000001" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "integer"
},
"email" : {
"type" : "keyword"
},
"employee-id" : {
"type" : "keyword",
"index" : false
},
"name" : {
"type" : "text"
}
}
}
}
}
https://www.elastic.co/docs/manage-data/data-store/mapping/explicit-mapping
guide:
https://www.elastic.co/docs/reference/elasticsearch/clients/dotnet/examples
https://www.elastic.co/docs/reference/elasticsearch/clients/dotnet/recommendations
sample
https://github.com/elastic/elasticsearch-net/blob/main/examples/aot/Program.cs
mapping sample
https://www.elastic.co/docs/reference/elasticsearch/clients/dotnet/mappings