-
安装包
-
添加配置
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"EsConfig": {
"host": "192.168.214.131",
"port": 9200,
"user": "elastic",
"password": "ellischen"
}
}
添加配置类
public class EsConfig
{
public string host { get; set; }
public string port { get; set; }
public string user { get; set; }
public string password { get; set; }
}
- 将配置文件与类绑定
builder.Services.AddScoped<EsConfig>();
builder.Services.Configure<EsConfig>(
builder.Configuration.GetSection("EsConfig")
);
- 封装helper 类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Elasticsearch.Net;
using EsHelper.CONFIG;
using EsHelper.Model;
using Microsoft.Extensions.Options;
using Nest;
namespace EsHelper.ESHelper
{
public class EsHelper
{
public IOptionsSnapshot<EsConfig> _esConfig;
public readonly ElasticClient elasticClient;
public EsHelper(IOptionsSnapshot<EsConfig> esConfig)
{
_esConfig = esConfig;
var pool = new SingleNodeConnectionPool(new Uri("http://192.168.214.131:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex("books").BasicAuthentication("elastic","ellischen");
elasticClient = new ElasticClient(settings);
}
public List<string> GroupDistinct()
{
AggregationDictionary aggs = new CompositeAggregation("ellis")
{
// After = new CompositeKey(new Dictionary<string, object>
//{
// { "myfield_comp_terms_agg", string.Empty }
//}),
Sources = new ICompositeAggregationSource[]
{
new TermsCompositeAggregationSource("ellis")
{
Field = "language"
}
},
Aggregations = new TermsAggregation("myfield")
{
Field = "language",
Size = 1000
}
};
var searchRequest = new SearchRequest<Item>("items")
{
Aggregations = aggs,
Size = 0,
Query = new MatchAllQuery(),
};
ISearchResponse<Item> item = elasticClient.Search<Item>(searchRequest);
List<string> distinct = new List<string>();
//ISearchResponse<Item> item = elasticClient.Search<Item>(opt =>
//opt.Index("items").Query(q=>q.MatchAll()).Size(0).Aggregations(a=>
//a.Composite("ellis",c=>c.Sources(s=>s.Terms("ellis",s=>s.Field("language"))).Size(1000))));
AggregateDictionary keyValuePairs = item.Aggregations;
foreach (BucketAggregate value in keyValuePairs.Values.ToList())
{
foreach (CompositeBucket key in value.Items)
{
CompositeKey keyValues = key.Key;
distinct.AddRange(keyValues.Values.ToList().Select(x => x.ToString()));
}
}
return distinct;
}
public async Task<double?> distinctcount()
{
AggregationDictionary aggs = new CardinalityAggregation("ellis", "language");
var searchRequest = new SearchRequest<Item>("items")
{
Aggregations = aggs,
Size = 0,
Query = new MatchAllQuery()
};
ISearchResponse<Item> item = elasticClient.Search<Item>(searchRequest);
// ISearchResponse<Item> item = await elasticClient.SearchAsync<Item>(opt =>
//opt.Index("items").Query(q => q.MatchAll()).Size(0).Aggregations(a =>
// a.Cardinality("ellis", c=>c.Field("language"))));
ValueAggregate valueAggregate = item.Aggregations.Cardinality("ellis");
return valueAggregate.Value;
}
}
}