ElasticSearch之Quick.ElasticSearch.Furion組件的使用

ElasticSearch 使用說明
本章,我們主要講解在.Net 中對Quick.ElasticSearch.Furion的使用進行介紹!

ElasticSearch 的官方客戶端 API 文檔地址://www.elastic.co/guide/en/elasticsearch/client/index.html

ElasticSearch 的官方客戶端 API 文檔地址(.Net,ElasticSearch8.x)://www.elastic.co/guide/en/elasticsearch/client/net-api/current/index.html

ElasticSearch 的官方客戶端 API 文檔地址(.Net,ElasticSearch7.x)://www.elastic.co/guide/en/elasticsearch/client/net-api/7.17/index.html

我們知道,在.Net 中有Elasticsearch.NetNEST等組件可以對 ElasticSearch 服務端進行各種操作。

但是但是但是,由於 ElasticSearch 提供的是各類 API 介面,並且查詢參數(JSON)條件會有很深的嵌套,這就導致了Elasticsearch.NetNEST等組件在使用的時候也有很深的內嵌條件,包括各種鏈式操作等,如果不是老手使用難度非常大,為了降低使用難度,Quick.ElasticSearch.Furion組件由此誕生了。

為了更好更簡單的在.Net Core 中使用 ElasticSearch,特此基於NEST封裝了Quick.ElasticSearch.Furion組件。

  • Quick.ElasticSearch.Furion:依賴於.Net6+、Furion

Quick.ElasticSearch.Furion 包地址為://www.nuget.org/packages/Quick.ElasticSearch.Furion

關於 Quick.ElasticSearch.Furion 的詳細使用說明,如下所示:

1、🍹 更新日誌

  • 1.0.3

    • 調整(簡化)了方法GetModelsAllGetModelsAllAsyncGetModelsGetModelsAsyncGetModelsFieldsGetModelsFieldsAsync的排序參數傳入方式;

    • 調整了InsertModelsInsertModelsAsync方法的內部實現,判斷是否有添加失敗採用了Errors屬性以及ItemsWithErrors錯誤對象集合;

    • 增加了InsertModelsBatch方法,分批添加數據,適用於大批量添加數據的情況;

    • 調整了GetGroupValuesGetGroupValuesAsyncGetGroupCountGetGroupCountAsyncGetGroupCountSmmaGetGroupCountSmmaAsyncGetGroupsCountGetGroupsCountAsyncGetGroupsDateGetGroupsDateAsyncGetGroupsDateSmmaGetGroupsDateSmmaAsyncGetGroupsDateSubGetGroupsDateSubAsyncGetStatiCountGetStatiCountAsyncGetStatiSumMultiGetStatiSumMultiAsync方法的內部實現;

    • 調整了AddLike擴展方式的實現方式為 Wildcard;

    • 增加了AddIn條件查詢擴展方法,類似 SQL 的 IN 條件;

    • GetStatiSumMultiGetStatiSumMultiAsync方法進行了性能優化,減少了 Http 請求次數。

  • 1.0.2

    • 方法GetGroupsCountGetGroupsCountAsync增加了第三個參數,以便於用戶自定義返回統計數量的 Key 名稱,默認為:GroupCount;

    • 增加了獲取ElasticClient客戶端對象方法:GetClient;

    • 增加獲取所有索引的方法GetIndexsGetIndexsAsync

    • 增加了根據主鍵 Id 修改一個或多個欄位值的方法UpdateModelFieldsByIdUpdateModelFieldsByIdAsync

    • 增加了根據查詢條件修改一個或多個欄位值的方法UpdateModelFieldsByConUpdateModelFieldsByConAsync

    • 增加了根據 Id 欄位獲取一條數據的方法GetModelGetModelAsync

    • 調整了InsertModelsInsertModelsAsync方法的實現,由原來的Bulk(BulkAsync)調整為了IndexMany(IndexManyAsync)方式。

  • 1.0.1

    • 增加了擴展方法 CreateEsQueryModel 和 AddEsQueryModel;

    • 重寫了查詢條件的使用方式;

    • 將所有涉及到查詢條件的方法都進行了調整;

    • 簡化了GetStatiSumMultiGetStatiSumMultiAsync方法所需參數的傳入方式;

    • 方法GetStatiSumMultiGetStatiSumMultiAsync增加了第三個參數,錯誤回調方法。

  • 1.0.0

    • 全新組件重磅發布;

    • 支援切換連接功能;

    • 支援檢查創建刪除索引;

    • 支援獲取索引健康狀態;

    • 支援添加數據;

    • 支援修改數據;

    • 支援刪除數據;

    • 支援獲取數據;

    • 支援分組統計查詢;

    • 支援獲取總條數、最大、最小、求和或平均值等。


2、🍟 Quick.ElasticSearch.Furion 使用說明

該組件是基於NESTFurion組件進行封裝使用的,目的在於結合.Net Core 更快、更簡單和更靈活的使用 ElasticSearch!!!

組件使用對比:

假設我們要實現如下數據格式的統計:

[
    {
        "Name": "2021-01",
        "Count": 100,
        "Group": [
            {
                "Name": "碎石",
                "Count": 3,
                "Sum": 1000,
                "Max": 100,
                "Min": 10,
                "Avg": 50
            }
        ]
    }
]

使用 Quick.ElasticSearch.Furion 組件的程式碼如下:

//獲取分組統計數據(日期欄位分組,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值)
//統計的時間間隔維度支援的表達式有(1y:1年、1q:1季度、1M:1個月、1w:1星期、1d:1天、1h:1小時、1m:1分鐘、1s:1秒鐘)
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupsDateSub = _quickElasticSearch.GetGroupsDateSub<TestElasticSearchModel>(m => m.CreateTime, m => m.UserName, m => m.UserAge, "1h", "yyyy-MM-dd HH:mm", queryCon);

//非同步
var retGetGroupsDateSubAsync = await _quickElasticSearch.GetGroupsDateSubAsync<TestElasticSearchModel>(m => m.CreateTime, m => m.UserName, m => m.UserAge, "1h", "yyyy-MM-dd HH:mm", queryCon);

使用 NEST 組件的程式碼如下:

/// <summary>
/// 獲取分組統計數據(日期欄位分組,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="field">要分組的欄位(日期欄位),格式如:m=>m.CreateTime</param>
/// <param name="fieldGroup">另一個要分組的欄位,格式如:m=>m.TypeName</param>
/// <param name="fieldGroupStati">要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price</param>
/// <param name="dateInterval">統計的日期間隔,默認為1M(支援的表達式有1y:1年、1q:1季度、1M:1個月、1w:1星期、1d:1天、1h:1小時、1m:1分鐘、1s:1秒鐘)</param>
/// <param name="dateFormat">統計的日期格式化方式,默認為yyyy-MM(注意大小寫,完整的格式化格式為yyyy-MM-dd HH:mm:ss)</param>
/// <param name="esQueryList">動態查詢條件</param>
/// <returns>返回元組對象,包含3個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為<![CDATA[List<Dictionary<string, object>>]]>的集合,格式如:[{"Name":"2021-01","Count":100,"Group":[{"Name":"碎石","Count":3,"Sum":1000,"Max":100,"Min":10,"Avg":50}]}]</returns>
public (bool IsSucc, string ErrMsg, List<Dictionary<string, object>>? Data) GetGroupsDateSub<T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroup, Expression<Func<T, object>> fieldGroupStati, string dateInterval = "1M", string dateFormat = "yyyy-MM", List<EsQueryModel<T>>? esQueryList = null) where T : class, new()
{
    try
    {
        //查詢條件
        Func<QueryContainerDescriptor<T>, QueryContainer> query = q =>
        {
            QueryContainer ret = q;

            if (esQueryList != null && esQueryList.Count > 0)
            {
                //組裝條件為&&或||關係
                for (int i = 0; i < esQueryList.Count; i++)
                {
                    if (esQueryList[i].QueryCon.Count > 0)
                    {
                        if (i == 0)
                        {
                            ret = esQueryList[i].QueryType == EsQueryType.And ? q.Bool(b => b.Must(esQueryList[i].QueryCon)) : q.Bool(b => b.Should(esQueryList[i].QueryCon));
                        }
                        else
                        {
                            ret = esQueryList[i].PrevConType == EsQueryType.And ?
                                ret && (esQueryList[i].QueryType == EsQueryType.And ? q.Bool(b => b.Must(esQueryList[i].QueryCon)) : q.Bool(b => b.Should(esQueryList[i].QueryCon))) ://添加&&並且關係
                                ret || (esQueryList[i].QueryType == EsQueryType.And ? q.Bool(b => b.Must(esQueryList[i].QueryCon)) : q.Bool(b => b.Should(esQueryList[i].QueryCon))); //添加||或關係
                        }
                    }
                }
            }

            return ret;
        };

        var ret = new List<Dictionary<string, object>>();

        var response = _client.Search<T>(s => s
            .Index(GetIndexName<T>())
            .Query(query)
            .Aggregations(ag => ag
                .DateHistogram("StatiGroup", dh => dh
                    .Field(field)                       //需要聚合分組的欄位名稱, 類型需要為date, 格式沒有要求
                    .CalendarInterval(dateInterval)     //時間間隔,此處設置為1個月(1y:1年、1q:1季度、1M:1個月、1w:1星期、1d:1天、1h:1小時、1m:1分鐘、1s:1秒鐘)
                    .TimeZone("+08:00")                 //設置時區, 這樣就相當於東八區的時間
                    .Format(dateFormat)                 //返回值格式化,HH大寫,不然不能區分上午、下午
                    .MinimumDocumentCount(0)            //為空的話則填充0
                    .Order(HistogramOrder.KeyAscending) //根據日期欄位升序排列

                    .Aggregations(ag => ag
                        .Terms("StatiGroup-1", t => t
                            .Field(fieldGroup)
                            .Size(_allConfig.MaxQueryCount)
                            .Order(o => o
                                .KeyAscending()  //按照分組欄位升序排序
                                .CountAscending()//按照統計數量升序排序
                            )

                            //統計fieldStati欄位的總和、最大值、最小值和平均值
                            .Aggregations(ags => ags
                                .Sum("StatiSum", m => m.Field(fieldGroupStati))         //求和
                                .Max("StatiMax", m => m.Field(fieldGroupStati))         //最大值
                                .Min("StatiMin", m => m.Field(fieldGroupStati))         //最小值
                                .Average("StatiAvg", m => m.Field(fieldGroupStati))     //平均值
                            )
                        )
                    )
                )
            )
            .TrackTotalHits(true)//TrackTotalHits必須設置為true,否則返回total總條數超過10000條時總是返回10000
        );

        if (response.IsValid)
        {
            //獲取分組標識
            var stati = response.Aggregations["StatiGroup"];
            //獲取分組後的集合
            var statiItems = ((BucketAggregate)stati).Items;

            foreach (var bucketMain in statiItems)
            {
                var item = (DateHistogramBucket)bucketMain;
                var retGroup = new List<Dictionary<string, object>>();

                foreach (var bucket in ((Nest.BucketAggregate)(item.Values.ToList()[0])).Items)
                {
                    var obj = (KeyedBucket<Object>)bucket;

                    retGroup.Add(new Dictionary<string, object> {
                        { "Name",obj.Key.ToString()!},
                        { "Count",obj.DocCount??0},
                        { "Sum",IsNotNull(obj.AverageBucket("StatiSum").Value)? ToDouble2Dec(obj.AverageBucket("StatiSum").Value!):0},
                        { "Max",IsNotNull(obj.AverageBucket("StatiMax").Value)? ToDouble2Dec(obj.AverageBucket("StatiMax").Value!):0},
                        { "Min",IsNotNull(obj.AverageBucket("StatiMin").Value)? ToDouble2Dec(obj.AverageBucket("StatiMin").Value!):0},
                        { "Avg",IsNotNull(obj.AverageBucket("StatiAvg").Value)? ToDouble2Dec(obj.AverageBucket("StatiAvg").Value!):0},
                    });
                }

                ret.Add(new Dictionary<string, object>
                {
                    { "Name",item.KeyAsString},
                    { "Count",item.DocCount??0},
                    { "Group",retGroup}
                });
            }
        }

        return (response.IsValid, !response.IsValid ? response.DebugInformation : string.Empty, ret);
    }
    catch (Exception ex)
    {
        return (false, ex.Message, null);
    }
}

由此可見,NEST 使用的複雜程度,更不用說 ElasticSearch.Net 了。

功能說明:

  • 根據配置文件讀取 ElasticSearch 連接的各個配置(如:ElasticSearch 服務地址、帳號和密碼等);

  • 支援配置多個 ElasticSearch 的連接配置;

  • 支援動態切換 ElasticSearch 的連接配置;

  • 支援檢查創建刪除索引;

  • 支援獲取索引健康狀態;

  • 支援添加數據(單條、多條);

  • 支援修改數據(單條、多條);

  • 支援刪除數據(根據 Id 刪除、根據條件刪除);

  • 支援獲取數據(獲取所有數據、獲取所有滿足條件的數據、獲取所有滿足條件並返回指定欄位的數據、根據 Id 獲取一條數據、獲取分頁的數據、獲取分頁並返回指定欄位的數據);

  • 支援分組統計查詢(統計單欄位並返回該欄位所有統計值、統計單欄位並返回該欄位所有統計值及對應的數量、統計單段並返回另一個欄位的總和最大值最小值和平均值、統計多個欄位並返回多個欄位對應的值以及對應分組的數據數量、按月統計並返回每個月及其數量、獲取分組統計數據【日期欄位分組,支援按年、季度、月份、星期、天、小時、分、秒等維度進行統計】、獲取分組統計數據【日期欄位分組,按月統計,並返回另一個欄位的總和、最大值、最小值和平均值】、獲取分組統計數據【日期欄位分組,並返回另一個欄位的總和、最大值、最小值和平均值】、獲取分組統計數據【日期欄位分組,按某年 1~12 月統計每個月的某個欄位每個月的總和以及這一年的總和】、獲取分組統計數據【日期欄位分組,按月統計,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值】、獲取分組統計數據【日期欄位分組,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值】);

  • 支援獲取總條數、最大、最小、求和或平均值等(獲取符合條件的數據總條數、獲取某欄位的最大值最小值求和平均值和總數等、獲取某些欄位相加的多個總和)。


3、🍖 安裝

安裝命令如下所示:

Install-Package Quick.ElasticSearch.Furion

該組件的命名空間為:Quick.ElasticSearch

Quick.ElasticSearch.Furion

Quick.ElasticSearch.Furion


4、🧀 配置

4.1、🥞 配置appsettings.json

appsettings.json配置文件中創建節點QuickElasticSearch>MaxQueryCountQuickElasticSearchConfigs,MaxQueryCount 為 ElasticSearch 索引查詢的最大條數(ElasticSearch 內部默認為 10000),QuickElasticSearchConfigs 為數組類型(即可配置多個 ElasticSearch 服務地址),具體配置如下所示:

{
    "QuickElasticSearch": {
        "MaxQueryCount": 2000000000,
        "QuickElasticSearchConfigs": [
            {
                "Default": true,
                "ConnId": 1,
                "DefaultIndex": "",
                "UserName": "elastic",
                "Password": "123456",
                "Uri": ["//192.168.3.200:9200/"]
            }
        ]
    }
}

配置說明:

屬性名稱 屬性說明 是否必填 備註
MaxQueryCount 全局設置 ElasticSearch 索引查詢的最大條數,ElasticSearch 默認為 10000 注意:此處我們最好設置為 2000000000,原因是 ElasticSearch 內部默認一次性最多返回 10000 條數據
Default 是否為默認連接 默認為 false
ConnId 連接 Id(請確保該 Id 的唯一性) 如果要動態切換連接配置,請確保該 Id 有值並且唯一
DefaultIndex 默認索引
UserName 連接賬戶
Password 連接密碼
Uri 連接地址集合,數據類型為 List<string>

4.2、🍞 實體特性

我們為實體提供了QuickElasticSearchConfig特性,起作用是設置該實體的索引名稱主鍵欄位,如下所示就是我們定義的一個實體對象:

using Nest;

namespace Quick.ElasticSearch.TestFurion
{
    [QuickElasticSearchConfig(indexName: "test_index", primaryKey: "Id")]
    public class TestElasticSearchModel
    {
        public long Id { get; set; }

        [Keyword]
        public string UserType { get; set; }

        public int UserId { get; set; }

        [Keyword]
        public string UserName { get; set; }

        public int UserAge { get; set; }

        public float Money { get; set; }

        public float MoneyOth { get; set; }

        public DateTime CreateTime { get; set; }
    }
}

說明:

  • indexName:索引名稱;

  • primaryKey:主鍵名稱。

4.3、🥐 配置 Program.cs

由於我們使用的是Furion,因此,我們可在程式啟動文件中配置如下程式碼(具體可參考Furion 入門指南),目的是註冊ElasticSearch 服務


[STAThread]
static void Main()
{
    ApplicationConfiguration.Initialize();

    //初始化Furion
    Serve.Run(GenericRunOptions.DefaultSilence);
}


public void ConfigureServices(IServiceCollection services)
{
    //註冊FrmMain窗體類
    services.AddScoped<FrmMain>();

    //注入IQuickElasticSearch的方式
    //通過AddElasticSearch添加依賴注入
    services.AddElasticSearch();

    ////使用構造函數獲取實例的方式:
    ////通過AddElasticSearch添加依賴注入,並註冊TestConsumerClassForDI類
    //services.AddElasticSearch<TestConsumerClassForDI>()

    //DI容器生成serviceProvider
    var serviceProvider = services.BuildServiceProvider();

    //通過serviceProvider獲取MainForm的註冊實例
    var frmMain = serviceProvider.GetRequiredService<FrmMain>();
    //var frmMain = (FrmMain)serviceProvider.GetService(typeof(FrmMain));

    Application.Run(frmMain);
}


說明:上述的關鍵點就在於調用.AddElasticSearch()或者.AddElasticSearch<T>()方法對服務進行註冊。

4.4、🍝 獲取依賴注入對象

定義IQuickElasticSearch對象(依賴注入方式):

public partial class FrmMain : Form
{
    //定義IQuickElasticSearch對象
	private readonly IQuickElasticSearch _quickElasticSearch;

	public FrmMain(IQuickElasticSearch quickElasticSearch)
	{
		InitializeComponent();

		//設置IQuickElasticSearch對象
		_quickElasticSearch = quickElasticSearch;
	}
}

5、🥪 具體使用

5.1、📮 獲取 ElasticSearch 客戶端

使用示例:

_quickElasticSearch.GetClient();

5.2、🌮 切換連接

使用示例:

_quickElasticSearch.ChangeConn(2);

5.3、🌯 獲取檢查創建刪除索引

使用示例(獲取所有索引):

//同步
var retGetIndexs = _quickElasticSearch.GetIndexs();

//非同步
var retGetIndexsAsync = await _quickElasticSearch.GetIndexsAsync();

使用示例(檢查索引是否存在):

//同步
var retIndexIsExist = _quickElasticSearch.IndexIsExist();

//非同步
var retIndexIsExistAsync = await _quickElasticSearch.IndexIsExistAsync();

使用示例(創建索引):

//同步
var retCreateIndex = _quickElasticSearch.CreateIndex<TestElasticSearchModel>();

//非同步
var retCreateIndexAsync = await _quickElasticSearch.CreateIndexAsync<TestElasticSearchModel>();

使用示例(刪除索引):

//同步
var retDeleteIndex = _quickElasticSearch.DeleteIndex();

//非同步
var retDeleteIndexAsync = await _quickElasticSearch.DeleteIndexAsync();

5.4、🥙 獲取索引健康狀態

使用示例(獲取索引健康狀態):

//同步
var retGetHealthStatus = _quickElasticSearch.GetHealthStatus();

//非同步
var retGetHealthStatusAsyncc = await _quickElasticSearch.GetHealthStatusAsync();

5.5、🧆 添加數據

使用示例(添加數據-單條):

var rand = new Random(Guid.NewGuid().GetHashCode());
var testData = new TestElasticSearchModel
{
    Id = YitIdHelper.NextId(),
    UserId = rand.Next(1, 9999),
    UserName = "Quber",
    UserAge = rand.Next(20, 80),
    Money = (float)1.429,
    MoneyOth = (float)2.556,
    CreateTime = DateTime.Now
};
var testDataAsync = new TestElasticSearchModel
{
    Id = YitIdHelper.NextId(),
    UserId = rand.Next(1, 9999),
    UserName = "Jack",
    UserAge = rand.Next(20, 80),
    Money = (float)1.429,
    MoneyOth = (float)2.556,
    CreateTime = DateTime.Now
};

//同步
var retInsertModel = _quickElasticSearch.InsertModel(testData);

//非同步
var retInsertModelAsync = await _quickElasticSearch.InsertModelAsync(testDataAsync);

使用示例(添加數據-多條):

var testDatas = new List<TestElasticSearchModel>
{
    new TestElasticSearchModel(),
    new TestElasticSearchModel()
};

//同步
var retInsertModels = _quickElasticSearch.InsertModels(testDatas, (errList, errMsg, allList) =>
{
    var thisErrList = errList;
    var thisErrMsg = errMsg;
});

//非同步
var retInsertModelsAsync = await _quickElasticSearch.InsertModelsAsync(testDatas, async (errList, errMsg, allList) =>
{
    var thisErrList = errList;
    var thisErrMsg = errMsg;

    await Task.CompletedTask;
});

使用示例(添加數據-多條,分批次):

var testDatas = new List<TestElasticSearchModel>
{
    new TestElasticSearchModel(),
    new TestElasticSearchModel()
};

var retInsertModelsBatch = _quickElasticSearch.InsertModelsBatch(testDatas, (errList, errMsg, allList) =>
{
    var thisErrList = errList;
    var thisErrMsg = errMsg;
},

//每批次添加的數據條數
1000,

//集群繁忙,報429錯誤碼的時候,等待多久進行重試
"5s",

//重試次數
2);

5.6、🥗 修改數據

使用示例(修改數據-根據主鍵 Id 修改一個或多個欄位的值):

//類似SQL:UPDATE A SET col1=123,col2=456 WHERE Id=1

//同步
var retUpdateModelFieldById = _quickElasticSearch.UpdateModelFieldsById<TestElasticSearchModel>(342235158519882, new { UserAge = 32, Money = 66.66 });

//非同步
var retUpdateModelFieldByIdAsync = await _quickElasticSearch.UpdateModelFieldsByIdAsync<TestElasticSearchModel>(342235158519882, new { UserAge = 32, Money = 66.66 });

使用示例(修改數據-根據查詢條件修改一個或多個欄位的值):

//類似SQL:UPDATE A SET col1=123,col2=456 WHERE (Id=1 OR Id=2)
//定義查詢條件:(Id = 342235158519882 OR Id = 342235158519883)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,
        EsQueryType.Or,
        (queryCon) =>
        {
            //查詢條件:(Id = 342235158519882 OR Id = 342235158519883)
            queryCon
                .AddEqu(m => m.Id, 342235158519882)
                .AddEqu(m => m.Id, 342235158519883);
        });

//非同步
var retUpdateModelFieldsByCon = _quickElasticSearch.UpdateModelFieldsByCon<TestElasticSearchModel>(queryCon, new { UserAge = 13, Money = 996.33 });

//同步
var retUpdateModelFieldsByConAsync = await _quickElasticSearch.UpdateModelFieldsByConAsync<TestElasticSearchModel>(queryCon, new { UserAge = 14, Money = 997.44 });

使用示例(修改數據-單條):

var rand = new Random(Guid.NewGuid().GetHashCode());
var testData = new TestElasticSearchModel
{
    Id = 1,
    UserId = rand.Next(1, 9999),
    UserName = "Quber1",
    UserAge = rand.Next(20, 80),
    Money = (float)2.429,
    MoneyOth = (float)3.556,
    CreateTime = DateTime.Now
};
var testDataAsync = new TestElasticSearchModel
{
    Id = 3,
    UserId = rand.Next(1, 9999),
    UserName = "Jack1",
    UserAge = rand.Next(20, 80),
    Money = (float)2.429,
    MoneyOth = (float)3.556,
    CreateTime = DateTime.Now
};

//同步
var retUpdateModel = _quickElasticSearch.UpdateModel(testData);

//非同步
var retUpdateModelAsync = await _quickElasticSearch.UpdateModelAsync(testDataAsync);

使用示例(修改數據-多條):

var rand = new Random(Guid.NewGuid().GetHashCode());
var testDatas = new List<TestElasticSearchModel>
{
    new TestElasticSearchModel
    {
        Id = 1,
        UserId = rand.Next(1, 9999),
        UserName = "Quber1",
        UserAge = rand.Next(20, 80),
        Money = (float)2.429,
        MoneyOth = (float)3.556,
        CreateTime = DateTime.Now
    },
    new TestElasticSearchModel
    {
        Id = 3,
        UserId = rand.Next(1, 9999),
        UserName = "Jack1",
        UserAge = rand.Next(20, 80),
        Money = (float)2.429,
        MoneyOth = (float)3.556,
        CreateTime = DateTime.Now
    },
    new TestElasticSearchModel
    {
        Id = 4,
        UserId = rand.Next(1, 9999),
        UserName = "Jack1",
        UserAge = rand.Next(20, 80),
        Money = (float)2.429,
        MoneyOth = (float)3.556,
        CreateTime = DateTime.Now
    }
};

//同步
var retUpdateModels = _quickElasticSearch.UpdateModels(testDatas, (errList, errMsg, allList) =>
{
    var thisErrList = errList;
    var thisErrMsg = errMsg;
});

//非同步
var retUpdateModelsAsync = await _quickElasticSearch.UpdateModelsAsync(testDatas, async (errList, errMsg, allList) =>
{
    var thisErrList = errList;
    var thisErrMsg = errMsg;

    await Task.CompletedTask;
});

5.7、🥘 刪除數據

使用示例(刪除數據-根據 Id):

//同步
var retDeleteModel = _quickElasticSearch.DeleteModel<TestElasticSearchModel>(340504986927178);

//非同步
var retDeleteModelAsync = await _quickElasticSearch.DeleteModelAsync<TestElasticSearchModel>(340504986927176);

使用示例(刪除數據-根據條件):

//定義查詢條件:(Id >= 340506710462542 AND UserAge <= 60) OR (Id = 340506710462539 OR Id = 340506710462538)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 340506710462542 AND UserAge <= 60)
            queryCon
                .AddGte(m => m.Id, 340506710462542)
                .AddLte(m => m.UserAge, 60);
        })
    //添加另一個組合條件
    .AddEsQueryModel(
        EsQueryType.Or,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.Or,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id = 340506710462539 OR Id = 340506710462538)
            queryCon
                .AddEqu(m => m.Id, 340506710462539)
                .AddEqu(m => m.Id, 340506710462538);
        });

//同步
var retDeleteModelBy = _quickElasticSearch.DeleteModelBy<TestElasticSearchModel>(queryCon);

//非同步
var retDeleteModelByAsync = await _quickElasticSearch.DeleteModelByAsync<TestElasticSearchModel>(queryCon);

5.8、🍲 獲取數據

使用示例(獲取所有數據):


//排序條件:ORDER BY Id DESC

//同步
var retGetModelsAll = _quickElasticSearch.GetModelsAll<TestElasticSearchModel>(m => m.Id, true);

//非同步
var retGetModelsAllAsync = await _quickElasticSearch.GetModelsAllAsync<TestElasticSearchModel>(m => m.CreateTime, false);


# 獲取所有數據
# 類似SQL:SELECT TOP 2000000000 * FROM A ORDER BY CreateTime DESC
POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "from":0,
    "query":{
        "match_all":{

        }
    },
    "size":2000000000,
    "sort":[
        {
            "CreateTime":{
                "order":"asc"
            }
        }
    ]
}


{
    "took": 8,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362545766476",
                "_score": null,
                "_source": {
                    "Id": 343362545766476,
                    "UserType": "高級用戶",
                    "UserId": 5133,
                    "UserName": "Quber9",
                    "UserAge": 63,
                    "Money": 9.429,
                    "MoneyOth": 10.556,
                    "CreateTime": "2022-10-17T16:05:48.5451264+08:00"
                },
                "sort": [1665993948545]
            },
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362545766473",
                "_score": null,
                "_source": {
                    "Id": 343362545766473,
                    "UserType": "系統用戶",
                    "UserId": 7360,
                    "UserName": "Quber6",
                    "UserAge": 57,
                    "Money": 6.429,
                    "MoneyOth": 7.5559998,
                    "CreateTime": "2022-10-17T16:05:48.5451259+08:00"
                },
                "sort": [1665993948545]
            }
            ……
        ]
    }
}


使用示例(獲取匹配條件的所有數據):


//排序條件:ORDER BY Id DESC
//定義查詢條件:(Id >= 340506710462535 AND UserAge <= 70)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 340506710462535 AND UserAge <= 70)
            queryCon
                .AddGte(m => m.Id, 340506710462535)
                .AddLte(m => m.UserAge, 70);
        });

//同步
var retGetModels = _quickElasticSearch.GetModels<TestElasticSearchModel>(m => m.Id, true, queryCon);

//非同步
var retGetModelsAsync = await _quickElasticSearch.GetModelsAsync<TestElasticSearchModel>(m => m.Id, true, queryCon);


# 獲取匹配條件的所有數據
# 類似SQL:SELECT TOP 2000000000 * FROM A WHERE Id >= 340506710462535 AND UserAge <= 70 ORDER BY Id DESC
POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "from":0,
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":340506710462535
                        }
                    }
                },
                {
                    "range":{
                        "UserAge":{
                            "lte":70
                        }
                    }
                }
            ]
        }
    },
    "size":2000000000,
    "sort":[
        {
            "Id":{
                "order":"desc"
            }
        }
    ]
}


{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 65,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362866188366",
                "_score": null,
                "_source": {
                    "Id": 343362866188366,
                    "UserType": "會員用戶",
                    "UserId": 2106,
                    "UserName": "Quber10",
                    "UserAge": 30,
                    "Money": 10.429,
                    "MoneyOth": 11.556,
                    "CreateTime": "2022-10-17T16:07:06.7733297+08:00"
                },
                "sort": [343362866188366]
            },
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362866188365",
                "_score": null,
                "_source": {
                    "Id": 343362866188365,
                    "UserType": "系統用戶",
                    "UserId": 829,
                    "UserName": "Quber9",
                    "UserAge": 39,
                    "Money": 9.429,
                    "MoneyOth": 10.556,
                    "CreateTime": "2022-10-17T16:07:06.7733294+08:00"
                },
                "sort": [343362866188365]
            },
            ……
        ]
    }
}


使用示例(獲取匹配條件的所有數據(返回指定欄位的值)):


//排序條件:ORDER BY Id DESC
//定義查詢條件:(Id >= 340506710462535 AND UserAge <= 70)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 340506710462535 AND UserAge <= 70)
            queryCon
                .AddGte(m => m.Id, 340506710462535)
                .AddLte(m => m.UserAge, 70);
        });

//同步
var retGetModelsFields = _quickElasticSearch.GetModelsFields<TestElasticSearchModel>(new List<string> { "UserId", "UserName", "UserAge" }, m => m.Id, true, queryCon);

//非同步
var retGetModelsFieldsAsync = await _quickElasticSearch.GetModelsFieldsAsync<TestElasticSearchModel>(new List<string> { "UserId", "UserName", "UserAge" }, m => m.Id, true, queryCon);


# 獲取匹配條件的所有數據(返回指定欄位的值)
# 類似SQL:SELECT TOP 2000000000 'UserId','UserName','UserAge' FROM A WHERE Id >= 340506710462535 AND UserAge <= 70 ORDER BY Id DESC
POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "from":0,
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":340506710462535
                        }
                    }
                },
                {
                    "range":{
                        "UserAge":{
                            "lte":70
                        }
                    }
                }
            ]
        }
    },
    "size":2000000000,
    "sort":[
        {
            "Id":{
                "order":"desc"
            }
        }
    ],
    "_source":{
        "includes":[
            "UserId",
            "UserName",
            "UserAge"
        ]
    }
}


{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 65,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362866188366",
                "_score": null,
                "_source": {
                    "UserName": "Quber10",
                    "UserId": 2106,
                    "UserAge": 30
                },
                "sort": [343362866188366]
            },
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362866188365",
                "_score": null,
                "_source": {
                    "UserName": "Quber9",
                    "UserId": 829,
                    "UserAge": 39
                },
                "sort": [343362866188365]
            }
            ……
        ]
    }
}


使用示例(獲取一條數據-根據主鍵 Id 查詢):


//同步
var retGetModel = _quickElasticSearch.GetModel<TestElasticSearchModel>(342235158519882);

//非同步
var retGetModelAsync = await _quickElasticSearch.GetModelAsync<TestElasticSearchModel>(342235158519883);


# 獲取一條數據(根據主鍵Id查詢)
# 類似SQL:SELECT * FROM A WHERE Id = 343362866188364
GET /test_index/_doc/343362866188364


{
    "_index": "test_index",
    "_type": "_doc",
    "_id": "343362866188364",
    "_version": 1,
    "_seq_no": 14,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "Id": 343362866188364,
        "UserType": "高級用戶",
        "UserId": 7438,
        "UserName": "Quber8",
        "UserAge": 53,
        "Money": 8.429,
        "MoneyOth": 9.556,
        "CreateTime": "2022-10-17T16:07:06.7733293+08:00"
    }
}


使用示例(獲取一條數據-根據某欄位查詢):


//同步
var retGetModel = _quickElasticSearch.GetModel<TestElasticSearchModel>(m => m.UserId, 340506710462542);

//非同步
var retGetModelAsync = await _quickElasticSearch.GetModelAsync<TestElasticSearchModel>(m => m.UserId, 340506710462537);


# 獲取一條數據(根據某欄位查詢)
# 類似SQL:SELECT * FROM A WHERE UserId = 7438
POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "query":{
        "term":{
            "UserId":{
                "value":7438
            }
        }
    }
}


{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362866188364",
                "_score": 1.0,
                "_source": {
                    "Id": 343362866188364,
                    "UserType": "高級用戶",
                    "UserId": 7438,
                    "UserName": "Quber8",
                    "UserAge": 53,
                    "Money": 8.429,
                    "MoneyOth": 9.556,
                    "CreateTime": "2022-10-17T16:07:06.7733293+08:00"
                }
            }
        ]
    }
}


使用示例(獲取分頁數據):


//分頁條件
var pagerCon = new EsPagerConModel
{
    PageIndex = 1,
    PageSize = 15,
    OrderField = "Id",
    OrderType = "desc"
};
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetPager = _quickElasticSearch.GetPager<TestElasticSearchModel>(pagerCon, queryCon);

//非同步
var retGetPagerAsync = await _quickElasticSearch.GetPagerAsync<TestElasticSearchModel>(pagerCon, queryCon);


# 獲取分頁數據
# 類似SQL:SELECT * FROM A WHERE Id >= 1 LIMIT 0,15 ORDER BY Id DESC
POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "from":0,
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":15,
    "sort":[
        {
            "Id":{
                "order":"desc"
            }
        }
    ]
}


{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362866188366",
                "_score": null,
                "_source": {
                    "Id": 343362866188366,
                    "UserType": "會員用戶",
                    "UserId": 2106,
                    "UserName": "Quber10",
                    "UserAge": 30,
                    "Money": 10.429,
                    "MoneyOth": 11.556,
                    "CreateTime": "2022-10-17T16:07:06.7733297+08:00"
                },
                "sort": [343362866188366]
            },
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362866188365",
                "_score": null,
                "_source": {
                    "Id": 343362866188365,
                    "UserType": "系統用戶",
                    "UserId": 829,
                    "UserName": "Quber9",
                    "UserAge": 39,
                    "Money": 9.429,
                    "MoneyOth": 10.556,
                    "CreateTime": "2022-10-17T16:07:06.7733294+08:00"
                },
                "sort": [343362866188365]
            }
            ……
        ]
    }
}


使用示例(獲取分頁數據-返回指定欄位的值):


//分頁條件
var pagerCon = new EsPagerConModel
{
    PageIndex = 1,
    PageSize = 15,
    OrderField = "Id",
    OrderType = "desc"
};
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetPagerFields = _quickElasticSearch.GetPagerFields<TestElasticSearchModel>(pagerCon, new List<string> { "UserId", "UserName", "UserAge" }, queryCon);

//非同步
var retGetPagerFieldsAsync = await _quickElasticSearch.GetPagerFieldsAsync<TestElasticSearchModel>(pagerCon, new List<string> { "UserId", "UserName", "UserAge" }, queryCon);


# 獲取分頁數據(返回指定欄位的值)
# 類似SQL:SELECT 'UserId','UserName','UserAge' FROM A WHERE Id >= 1 LIMIT 0,15 ORDER BY Id DESC
POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "from":0,
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":15,
    "sort":[
        {
            "Id":{
                "order":"desc"
            }
        }
    ],
    "_source":{
        "includes":[
            "UserId",
            "UserName",
            "UserAge"
        ]
    }
}


{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362866188366",
                "_score": null,
                "_source": {
                    "UserName": "Quber10",
                    "UserId": 2106,
                    "UserAge": 30
                },
                "sort": [343362866188366]
            },
            {
                "_index": "test_index",
                "_type": "_doc",
                "_id": "343362866188365",
                "_score": null,
                "_source": {
                    "UserName": "Quber9",
                    "UserId": 829,
                    "UserAge": 39
                },
                "sort": [343362866188365]
            }
            ……
        ]
    }
}


5.9、🥣 分組統計查詢

使用示例(單個欄位分組,並返回該欄位的所有值集合):


//類似SQL:SELECT col1 FROM A GROUP BY col1
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupValues = _quickElasticSearch.GetGroupValues<TestElasticSearchModel>(m => m.UserName, queryCon);

//非同步
var retGetGroupValuesAsync = await _quickElasticSearch.GetGroupValuesAsync<TestElasticSearchModel>(m => m.UserName, queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": ["Jack1", "Jack2", "Jack3"]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "terms":{
                "field":"UserName",
                "order":[
                    {
                        "_key":"asc"
                    }
                ],
                "size":2000000000
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "sterms#StatiGroup": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Jack1",
                    "doc_count": 2
                },
                {
                    "key": "Jack10",
                    "doc_count": 1
                },
                {
                    "key": "Jack2",
                    "doc_count": 2
                },
                {
                    "key": "Jack3",
                    "doc_count": 2
                },
                {
                    "key": "Jack4",
                    "doc_count": 2
                },
                {
                    "key": "Jack5",
                    "doc_count": 2
                },
                {
                    "key": "Jack6",
                    "doc_count": 1
                },
                {
                    "key": "Jack7",
                    "doc_count": 1
                },
                {
                    "key": "Jack8",
                    "doc_count": 1
                },
                {
                    "key": "Jack9",
                    "doc_count": 1
                },
                {
                    "key": "Quber1",
                    "doc_count": 7
                },
                {
                    "key": "Quber10",
                    "doc_count": 3
                },
                {
                    "key": "Quber11",
                    "doc_count": 1
                },
                {
                    "key": "Quber12",
                    "doc_count": 1
                },
                {
                    "key": "Quber2",
                    "doc_count": 7
                },
                {
                    "key": "Quber3",
                    "doc_count": 7
                },
                {
                    "key": "Quber4",
                    "doc_count": 7
                },
                {
                    "key": "Quber5",
                    "doc_count": 7
                },
                {
                    "key": "Quber6",
                    "doc_count": 6
                },
                {
                    "key": "Quber7",
                    "doc_count": 5
                },
                {
                    "key": "Quber8",
                    "doc_count": 5
                },
                {
                    "key": "Quber9",
                    "doc_count": 4
                }
            ]
        }
    }
}



使用示例(單個欄位分組,並返回該欄位的所有值和數量集合):


//類似SQL:SELECT col1,COUNT(1) FROM A GROUP BY col1
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupCount = _quickElasticSearch.GetGroupCount<TestElasticSearchModel>(m => m.UserName, queryCon);

//非同步
var retGetGroupCountAsync = await _quickElasticSearch.GetGroupCountAsync<TestElasticSearchModel>(m => m.UserName, queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        {
            "Name": "Jack1",
            "Count": 2
        },
        {
            "Name": "Jack2",
            "Count": 2
        },
        {
            "Name": "Jack3",
            "Count": 2
        }
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "terms":{
                "field":"UserName",
                "order":[
                    {
                        "_key":"asc"
                    },
                    {
                        "_count":"asc"
                    }
                ],
                "size":2000000000
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "sterms#StatiGroup": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Jack1",
                    "doc_count": 2
                },
                {
                    "key": "Jack10",
                    "doc_count": 1
                },
                {
                    "key": "Jack2",
                    "doc_count": 2
                },
                {
                    "key": "Jack3",
                    "doc_count": 2
                },
                {
                    "key": "Jack4",
                    "doc_count": 2
                },
                {
                    "key": "Jack5",
                    "doc_count": 2
                },
                {
                    "key": "Jack6",
                    "doc_count": 1
                },
                {
                    "key": "Jack7",
                    "doc_count": 1
                },
                {
                    "key": "Jack8",
                    "doc_count": 1
                },
                {
                    "key": "Jack9",
                    "doc_count": 1
                },
                {
                    "key": "Quber1",
                    "doc_count": 7
                },
                {
                    "key": "Quber10",
                    "doc_count": 3
                },
                {
                    "key": "Quber11",
                    "doc_count": 1
                },
                {
                    "key": "Quber12",
                    "doc_count": 1
                },
                {
                    "key": "Quber2",
                    "doc_count": 7
                },
                {
                    "key": "Quber3",
                    "doc_count": 7
                },
                {
                    "key": "Quber4",
                    "doc_count": 7
                },
                {
                    "key": "Quber5",
                    "doc_count": 7
                },
                {
                    "key": "Quber6",
                    "doc_count": 6
                },
                {
                    "key": "Quber7",
                    "doc_count": 5
                },
                {
                    "key": "Quber8",
                    "doc_count": 5
                },
                {
                    "key": "Quber9",
                    "doc_count": 4
                }
            ]
        }
    }
}



使用示例(單個欄位分組,並返回另一個欄位的總和、最大值、最小值和平均值):


//類似SQL:SELECT col1,COUNT(1),SUM(col2),MAX(col2),MIN(col2),AVG(col2) FROM A GROUP BY col1
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupCountSmma = _quickElasticSearch.GetGroupCountSmma<TestElasticSearchModel>(m => m.UserName, m => m.UserAge, queryCon);

//非同步
var retGetGroupCountSmmaAsync = await _quickElasticSearch.GetGroupCountSmmaAsync<TestElasticSearchModel>(m => m.UserName, m => m.UserAge, queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        {
            "Name": "Jack1",
            "Count": 2,
            "Sum": 54,
            "Max": 31,
            "Min": 23,
            "Avg": 27
        },
        {
            "Name": "Jack2",
            "Count": 2,
            "Sum": 98,
            "Max": 77,
            "Min": 21,
            "Avg": 49
        },
        {
            "Name": "Jack3",
            "Count": 2,
            "Sum": 126,
            "Max": 67,
            "Min": 59,
            "Avg": 63
        }
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "aggs":{
                "StatiSum":{
                    "sum":{
                        "field":"UserAge"
                    }
                },
                "StatiMax":{
                    "max":{
                        "field":"UserAge"
                    }
                },
                "StatiMin":{
                    "min":{
                        "field":"UserAge"
                    }
                },
                "StatiAvg":{
                    "avg":{
                        "field":"UserAge"
                    }
                }
            },
            "terms":{
                "field":"UserName",
                "order":[
                    {
                        "_key":"asc"
                    },
                    {
                        "_count":"asc"
                    }
                ],
                "size":2000000000
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "sterms#StatiGroup": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "Jack1",
                    "doc_count": 2,
                    "avg#StatiAvg": {
                        "value": 34.0
                    },
                    "max#StatiMax": {
                        "value": 39.0
                    },
                    "min#StatiMin": {
                        "value": 29.0
                    },
                    "sum#StatiSum": {
                        "value": 68.0
                    }
                },
                {
                    "key": "Jack10",
                    "doc_count": 1,
                    "avg#StatiAvg": {
                        "value": 60.0
                    },
                    "max#StatiMax": {
                        "value": 60.0
                    },
                    "min#StatiMin": {
                        "value": 60.0
                    },
                    "sum#StatiSum": {
                        "value": 60.0
                    }
                },
                {
                    "key": "Jack2",
                    "doc_count": 2,
                    "avg#StatiAvg": {
                        "value": 40.0
                    },
                    "max#StatiMax": {
                        "value": 48.0
                    },
                    "min#StatiMin": {
                        "value": 32.0
                    },
                    "sum#StatiSum": {
                        "value": 80.0
                    }
                },
                {
                    "key": "Jack3",
                    "doc_count": 2,
                    "avg#StatiAvg": {
                        "value": 39.5
                    },
                    "max#StatiMax": {
                        "value": 53.0
                    },
                    "min#StatiMin": {
                        "value": 26.0
                    },
                    "sum#StatiSum": {
                        "value": 79.0
                    }
                },
                {
                    "key": "Jack4",
                    "doc_count": 2,
                    "avg#StatiAvg": {
                        "value": 48.0
                    },
                    "max#StatiMax": {
                        "value": 65.0
                    },
                    "min#StatiMin": {
                        "value": 31.0
                    },
                    "sum#StatiSum": {
                        "value": 96.0
                    }
                },
                {
                    "key": "Jack5",
                    "doc_count": 2,
                    "avg#StatiAvg": {
                        "value": 54.0
                    },
                    "max#StatiMax": {
                        "value": 78.0
                    },
                    "min#StatiMin": {
                        "value": 30.0
                    },
                    "sum#StatiSum": {
                        "value": 108.0
                    }
                },
                {
                    "key": "Jack6",
                    "doc_count": 1,
                    "avg#StatiAvg": {
                        "value": 57.0
                    },
                    "max#StatiMax": {
                        "value": 57.0
                    },
                    "min#StatiMin": {
                        "value": 57.0
                    },
                    "sum#StatiSum": {
                        "value": 57.0
                    }
                },
                {
                    "key": "Jack7",
                    "doc_count": 1,
                    "avg#StatiAvg": {
                        "value": 40.0
                    },
                    "max#StatiMax": {
                        "value": 40.0
                    },
                    "min#StatiMin": {
                        "value": 40.0
                    },
                    "sum#StatiSum": {
                        "value": 40.0
                    }
                },
                {
                    "key": "Jack8",
                    "doc_count": 1,
                    "avg#StatiAvg": {
                        "value": 29.0
                    },
                    "max#StatiMax": {
                        "value": 29.0
                    },
                    "min#StatiMin": {
                        "value": 29.0
                    },
                    "sum#StatiSum": {
                        "value": 29.0
                    }
                },
                {
                    "key": "Jack9",
                    "doc_count": 1,
                    "avg#StatiAvg": {
                        "value": 72.0
                    },
                    "max#StatiMax": {
                        "value": 72.0
                    },
                    "min#StatiMin": {
                        "value": 72.0
                    },
                    "sum#StatiSum": {
                        "value": 72.0
                    }
                },
                {
                    "key": "Quber1",
                    "doc_count": 7,
                    "avg#StatiAvg": {
                        "value": 48.857142857142854
                    },
                    "max#StatiMax": {
                        "value": 79.0
                    },
                    "min#StatiMin": {
                        "value": 21.0
                    },
                    "sum#StatiSum": {
                        "value": 342.0
                    }
                },
                {
                    "key": "Quber10",
                    "doc_count": 3,
                    "avg#StatiAvg": {
                        "value": 31.0
                    },
                    "max#StatiMax": {
                        "value": 43.0
                    },
                    "min#StatiMin": {
                        "value": 20.0
                    },
                    "sum#StatiSum": {
                        "value": 93.0
                    }
                },
                {
                    "key": "Quber11",
                    "doc_count": 1,
                    "avg#StatiAvg": {
                        "value": 29.0
                    },
                    "max#StatiMax": {
                        "value": 29.0
                    },
                    "min#StatiMin": {
                        "value": 29.0
                    },
                    "sum#StatiSum": {
                        "value": 29.0
                    }
                },
                {
                    "key": "Quber12",
                    "doc_count": 1,
                    "avg#StatiAvg": {
                        "value": 74.0
                    },
                    "max#StatiMax": {
                        "value": 74.0
                    },
                    "min#StatiMin": {
                        "value": 74.0
                    },
                    "sum#StatiSum": {
                        "value": 74.0
                    }
                },
                {
                    "key": "Quber2",
                    "doc_count": 7,
                    "avg#StatiAvg": {
                        "value": 51.0
                    },
                    "max#StatiMax": {
                        "value": 78.0
                    },
                    "min#StatiMin": {
                        "value": 25.0
                    },
                    "sum#StatiSum": {
                        "value": 357.0
                    }
                },
                {
                    "key": "Quber3",
                    "doc_count": 7,
                    "avg#StatiAvg": {
                        "value": 48.142857142857146
                    },
                    "max#StatiMax": {
                        "value": 74.0
                    },
                    "min#StatiMin": {
                        "value": 28.0
                    },
                    "sum#StatiSum": {
                        "value": 337.0
                    }
                },
                {
                    "key": "Quber4",
                    "doc_count": 7,
                    "avg#StatiAvg": {
                        "value": 53.285714285714285
                    },
                    "max#StatiMax": {
                        "value": 76.0
                    },
                    "min#StatiMin": {
                        "value": 25.0
                    },
                    "sum#StatiSum": {
                        "value": 373.0
                    }
                },
                {
                    "key": "Quber5",
                    "doc_count": 7,
                    "avg#StatiAvg": {
                        "value": 47.285714285714285
                    },
                    "max#StatiMax": {
                        "value": 70.0
                    },
                    "min#StatiMin": {
                        "value": 25.0
                    },
                    "sum#StatiSum": {
                        "value": 331.0
                    }
                },
                {
                    "key": "Quber6",
                    "doc_count": 6,
                    "avg#StatiAvg": {
                        "value": 41.5
                    },
                    "max#StatiMax": {
                        "value": 78.0
                    },
                    "min#StatiMin": {
                        "value": 20.0
                    },
                    "sum#StatiSum": {
                        "value": 249.0
                    }
                },
                {
                    "key": "Quber7",
                    "doc_count": 5,
                    "avg#StatiAvg": {
                        "value": 43.0
                    },
                    "max#StatiMax": {
                        "value": 61.0
                    },
                    "min#StatiMin": {
                        "value": 21.0
                    },
                    "sum#StatiSum": {
                        "value": 215.0
                    }
                },
                {
                    "key": "Quber8",
                    "doc_count": 5,
                    "avg#StatiAvg": {
                        "value": 42.2
                    },
                    "max#StatiMax": {
                        "value": 61.0
                    },
                    "min#StatiMin": {
                        "value": 22.0
                    },
                    "sum#StatiSum": {
                        "value": 211.0
                    }
                },
                {
                    "key": "Quber9",
                    "doc_count": 4,
                    "avg#StatiAvg": {
                        "value": 54.75
                    },
                    "max#StatiMax": {
                        "value": 65.0
                    },
                    "min#StatiMin": {
                        "value": 39.0
                    },
                    "sum#StatiSum": {
                        "value": 219.0
                    }
                }
            ]
        }
    }
}



使用示例(多個欄位分組,並返回多個欄位對應的值以及對應分組的數據數量):


//類似SQL:SELECT col1,col2,COUNT(1) FROM A GROUP BY col1,col2
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupsCount = _quickElasticSearch.GetGroupsCount<TestElasticSearchModel>(new List<string> { "UserName", "UserAge" }, queryCon);

//非同步
var retGetGroupsCountAsync = await _quickElasticSearch.GetGroupsCountAsync<TestElasticSearchModel>(new List<string> { "UserName", "UserAge" }, queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        {
            "UserName": "Jack1",
            "UserAge": 23,
            "GroupCount": 1
        },
        {
            "UserName": "Jack1",
            "UserAge": 31,
            "GroupCount": 1
        },
        {
            "UserName": "Jack2",
            "UserAge": 21,
            "GroupCount": 1
        },
        {
            "UserName": "Jack2",
            "UserAge": 77,
            "GroupCount": 1
        },
        {
            "UserName": "Jack3",
            "UserAge": 59,
            "GroupCount": 1
        },
        {
            "UserName": "Jack3",
            "UserAge": 67,
            "GroupCount": 1
        }
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "multi_terms":{
                "order":[
                    {
                        "_key":"asc"
                    },
                    {
                        "_count":"asc"
                    }
                ],
                "size":2000000000,
                "terms":[
                    {
                        "field":"UserType"
                    },
                    {
                        "field":"UserName"
                    }
                ]
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "multi_terms#StatiGroup": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": ["會員用戶", "Jack1"],
                    "key_as_string": "會員用戶|Jack1",
                    "doc_count": 1
                },
                {
                    "key": ["會員用戶", "Jack6"],
                    "key_as_string": "會員用戶|Jack6",
                    "doc_count": 1
                },
                {
                    "key": ["會員用戶", "Jack9"],
                    "key_as_string": "會員用戶|Jack9",
                    "doc_count": 1
                },
                {
                    "key": ["會員用戶", "Quber1"],
                    "key_as_string": "會員用戶|Quber1",
                    "doc_count": 1
                },
                {
                    "key": ["會員用戶", "Quber10"],
                    "key_as_string": "會員用戶|Quber10",
                    "doc_count": 1
                },
                {
                    "key": ["會員用戶", "Quber11"],
                    "key_as_string": "會員用戶|Quber11",
                    "doc_count": 1
                },
                {
                    "key": ["會員用戶", "Quber2"],
                    "key_as_string": "會員用戶|Quber2",
                    "doc_count": 2
                },
                {
                    "key": ["會員用戶", "Quber3"],
                    "key_as_string": "會員用戶|Quber3",
                    "doc_count": 2
                },
                {
                    "key": ["會員用戶", "Quber4"],
                    "key_as_string": "會員用戶|Quber4",
                    "doc_count": 1
                },
                {
                    "key": ["會員用戶", "Quber5"],
                    "key_as_string": "會員用戶|Quber5",
                    "doc_count": 3
                },
                {
                    "key": ["會員用戶", "Quber6"],
                    "key_as_string": "會員用戶|Quber6",
                    "doc_count": 2
                },
                {
                    "key": ["會員用戶", "Quber7"],
                    "key_as_string": "會員用戶|Quber7",
                    "doc_count": 1
                },
                {
                    "key": ["會員用戶", "Quber8"],
                    "key_as_string": "會員用戶|Quber8",
                    "doc_count": 1
                },
                {
                    "key": ["會員用戶", "Quber9"],
                    "key_as_string": "會員用戶|Quber9",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Jack1"],
                    "key_as_string": "普通用戶|Jack1",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Jack10"],
                    "key_as_string": "普通用戶|Jack10",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Jack2"],
                    "key_as_string": "普通用戶|Jack2",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Jack4"],
                    "key_as_string": "普通用戶|Jack4",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Jack7"],
                    "key_as_string": "普通用戶|Jack7",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Jack8"],
                    "key_as_string": "普通用戶|Jack8",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Quber1"],
                    "key_as_string": "普通用戶|Quber1",
                    "doc_count": 3
                },
                {
                    "key": ["普通用戶", "Quber12"],
                    "key_as_string": "普通用戶|Quber12",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Quber3"],
                    "key_as_string": "普通用戶|Quber3",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Quber4"],
                    "key_as_string": "普通用戶|Quber4",
                    "doc_count": 2
                },
                {
                    "key": ["普通用戶", "Quber6"],
                    "key_as_string": "普通用戶|Quber6",
                    "doc_count": 1
                },
                {
                    "key": ["普通用戶", "Quber7"],
                    "key_as_string": "普通用戶|Quber7",
                    "doc_count": 2
                },
                {
                    "key": ["普通用戶", "Quber8"],
                    "key_as_string": "普通用戶|Quber8",
                    "doc_count": 2
                },
                {
                    "key": ["普通用戶", "Quber9"],
                    "key_as_string": "普通用戶|Quber9",
                    "doc_count": 1
                },
                {
                    "key": ["系統用戶", "Jack2"],
                    "key_as_string": "系統用戶|Jack2",
                    "doc_count": 1
                },
                {
                    "key": ["系統用戶", "Jack3"],
                    "key_as_string": "系統用戶|Jack3",
                    "doc_count": 2
                },
                {
                    "key": ["系統用戶", "Jack5"],
                    "key_as_string": "系統用戶|Jack5",
                    "doc_count": 1
                },
                {
                    "key": ["系統用戶", "Quber1"],
                    "key_as_string": "系統用戶|Quber1",
                    "doc_count": 1
                },
                {
                    "key": ["系統用戶", "Quber10"],
                    "key_as_string": "系統用戶|Quber10",
                    "doc_count": 2
                },
                {
                    "key": ["系統用戶", "Quber2"],
                    "key_as_string": "系統用戶|Quber2",
                    "doc_count": 3
                },
                {
                    "key": ["系統用戶", "Quber3"],
                    "key_as_string": "系統用戶|Quber3",
                    "doc_count": 1
                },
                {
                    "key": ["系統用戶", "Quber4"],
                    "key_as_string": "系統用戶|Quber4",
                    "doc_count": 2
                },
                {
                    "key": ["系統用戶", "Quber5"],
                    "key_as_string": "系統用戶|Quber5",
                    "doc_count": 3
                },
                {
                    "key": ["系統用戶", "Quber6"],
                    "key_as_string": "系統用戶|Quber6",
                    "doc_count": 3
                },
                {
                    "key": ["系統用戶", "Quber7"],
                    "key_as_string": "系統用戶|Quber7",
                    "doc_count": 1
                },
                {
                    "key": ["系統用戶", "Quber9"],
                    "key_as_string": "系統用戶|Quber9",
                    "doc_count": 1
                },
                {
                    "key": ["高級用戶", "Jack4"],
                    "key_as_string": "高級用戶|Jack4",
                    "doc_count": 1
                },
                {
                    "key": ["高級用戶", "Jack5"],
                    "key_as_string": "高級用戶|Jack5",
                    "doc_count": 1
                },
                {
                    "key": ["高級用戶", "Quber1"],
                    "key_as_string": "高級用戶|Quber1",
                    "doc_count": 2
                },
                {
                    "key": ["高級用戶", "Quber2"],
                    "key_as_string": "高級用戶|Quber2",
                    "doc_count": 2
                },
                {
                    "key": ["高級用戶", "Quber3"],
                    "key_as_string": "高級用戶|Quber3",
                    "doc_count": 3
                },
                {
                    "key": ["高級用戶", "Quber4"],
                    "key_as_string": "高級用戶|Quber4",
                    "doc_count": 2
                },
                {
                    "key": ["高級用戶", "Quber5"],
                    "key_as_string": "高級用戶|Quber5",
                    "doc_count": 1
                },
                {
                    "key": ["高級用戶", "Quber7"],
                    "key_as_string": "高級用戶|Quber7",
                    "doc_count": 1
                },
                {
                    "key": ["高級用戶", "Quber8"],
                    "key_as_string": "高級用戶|Quber8",
                    "doc_count": 2
                },
                {
                    "key": ["高級用戶", "Quber9"],
                    "key_as_string": "高級用戶|Quber9",
                    "doc_count": 1
                }
            ]
        }
    }
}



使用示例(日期欄位分組,按月統計):


//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupsDateMonth = _quickElasticSearch.GetGroupsDateMonth<TestElasticSearchModel>(m => m.CreateTime, queryCon);

//非同步
var retGetGroupsDateMonthAsync = await _quickElasticSearch.GetGroupsDateMonthAsync<TestElasticSearchModel>(m => m.CreateTime, queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        {
            "Name": "2022-08",
            "Count": 110
        },
        {
            "Name": "2022-09",
            "Count": 132
        },
        {
            "Name": "2022-10",
            "Count": 166
        }
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "date_histogram":{
                "field":"CreateTime",
                "format":"yyyy-MM",
                "calendar_interval":"1M",
                "min_doc_count":0,
                "order":{
                    "_key":"asc"
                },
                "time_zone":"+08:00"
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "date_histogram#StatiGroup": {
            "buckets": [
                {
                    "key_as_string": "2022-10",
                    "key": 1664553600000,
                    "doc_count": 75
                }
            ]
        }
    }
}



使用示例(日期欄位分組,支援按年、季度、月份、星期、天、小時、分、秒等維度進行統計):


//統計的時間間隔維度支援的表達式有(1y:1年、1q:1季度、1M:1個月、1w:1星期、1d:1天、1h:1小時、1m:1分鐘、1s:1秒鐘)
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupsDate = _quickElasticSearch.GetGroupsDate<TestElasticSearchModel>(m => m.CreateTime, "1m", "yyyy-MM-dd HH:mm", queryCon);

//非同步
var retGetGroupsDateAsync = await _quickElasticSearch.GetGroupsDateAsync<TestElasticSearchModel>(m => m.CreateTime, "1m", "yyyy-MM-dd HH:mm", queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        {
            "Name": "2022-10-12 09:24",
            "Count": 10
        },
        {
            "Name": "2022-10-12 09:25",
            "Count": 50
        },
        {
            "Name": "2022-10-12 09:26",
            "Count": 10
        },
        {
            "Name": "2022-10-12 09:27",
            "Count": 10
        },
        {
            "Name": "2022-10-12 09:28",
            "Count": 0
        },
        {
            "Name": "2022-10-12 09:29",
            "Count": 0
        },
        {
            "Name": "2022-10-12 09:30",
            "Count": 0
        },
        {
            "Name": "2022-10-12 09:31",
            "Count": 0
        },
        {
            "Name": "2022-10-12 09:32",
            "Count": 0
        },
        {
            "Name": "2022-10-12 09:33",
            "Count": 0
        },
        {
            "Name": "2022-10-12 09:34",
            "Count": 0
        },
        {
            "Name": "2022-10-12 09:35",
            "Count": 0
        },
        {
            "Name": "2022-10-12 09:36",
            "Count": 11
        },
        {
            "Name": "2022-10-12 09:37",
            "Count": 41
        }
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "date_histogram":{
                "field":"CreateTime",
                "format":"yyyy-MM-dd HH:mm",
                "calendar_interval":"1m",
                "min_doc_count":0,
                "order":{
                    "_key":"asc"
                },
                "time_zone":"+08:00"
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "date_histogram#StatiGroup": {
            "buckets": [
                {
                    "key_as_string": "2022-10-17 16:05",
                    "key": 1665993900000,
                    "doc_count": 15
                },
                {
                    "key_as_string": "2022-10-17 16:06",
                    "key": 1665993960000,
                    "doc_count": 50
                },
                {
                    "key_as_string": "2022-10-17 16:07",
                    "key": 1665994020000,
                    "doc_count": 10
                }
            ]
        }
    }
}



使用示例(日期欄位分組,按月統計,並返回另一個欄位的總和、最大值、最小值和平均值):


//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupsDateMonthSmma = _quickElasticSearch.GetGroupsDateMonthSmma<TestElasticSearchModel>(m => m.CreateTime, m => m.UserAge, queryCon);

//非同步
var retGetGroupsDateMonthSmmaAsync = await _quickElasticSearch.GetGroupsDateMonthSmmaAsync<TestElasticSearchModel>(m => m.CreateTime, m => m.UserAge, queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        {
            "Name": "2022-10",
            "Count": 132,
            "Sum": 6842,
            "Max": 79,
            "Min": 21,
            "Avg": 51.83
        }
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "aggs":{
                "StatiSum":{
                    "sum":{
                        "field":"UserAge"
                    }
                },
                "StatiMax":{
                    "max":{
                        "field":"UserAge"
                    }
                },
                "StatiMin":{
                    "min":{
                        "field":"UserAge"
                    }
                },
                "StatiAvg":{
                    "avg":{
                        "field":"UserAge"
                    }
                }
            },
            "date_histogram":{
                "field":"CreateTime",
                "format":"yyyy-MM",
                "calendar_interval":"1M",
                "min_doc_count":0,
                "order":{
                    "_key":"asc"
                },
                "time_zone":"+08:00"
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "date_histogram#StatiGroup": {
            "buckets": [
                {
                    "key_as_string": "2022-10",
                    "key": 1664553600000,
                    "doc_count": 75,
                    "avg#StatiAvg": {
                        "value": 46.92
                    },
                    "max#StatiMax": {
                        "value": 79.0
                    },
                    "min#StatiMin": {
                        "value": 20.0
                    },
                    "sum#StatiSum": {
                        "value": 3519.0
                    }
                }
            ]
        }
    }
}



使用示例(日期欄位分組,並返回另一個欄位的總和、最大值、最小值和平均值):


//統計的時間間隔維度支援的表達式有(1y:1年、1q:1季度、1M:1個月、1w:1星期、1d:1天、1h:1小時、1m:1分鐘、1s:1秒鐘)
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupsDateSmma = _quickElasticSearch.GetGroupsDateSmma<TestElasticSearchModel>(m => m.CreateTime, m => m.UserAge, "1m", "yyyy-MM-dd HH:mm", queryCon);

//非同步
var retGetGroupsDateSmmaAsync = await _quickElasticSearch.GetGroupsDateSmmaAsync<TestElasticSearchModel>(m => m.CreateTime, m => m.UserAge, "1m", "yyyy-MM-dd HH:mm", queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        {
            "Name": "2022-10-12 09:24",
            "Count": 10,
            "Sum": 588,
            "Max": 76,
            "Min": 31,
            "Avg": 58.8
        },
        {
            "Name": "2022-10-12 09:25",
            "Count": 50,
            "Sum": 2432,
            "Max": 79,
            "Min": 21,
            "Avg": 48.64
        },
        {
            "Name": "2022-10-12 09:26",
            "Count": 10,
            "Sum": 512,
            "Max": 79,
            "Min": 27,
            "Avg": 51.2
        },
        {
            "Name": "2022-10-12 09:27",
            "Count": 10,
            "Sum": 616,
            "Max": 76,
            "Min": 34,
            "Avg": 61.6
        },
        {
            "Name": "2022-10-12 09:28",
            "Count": 0,
            "Sum": 0,
            "Max": 0,
            "Min": 0,
            "Avg": 0
        },
        {
            "Name": "2022-10-12 09:29",
            "Count": 0,
            "Sum": 0,
            "Max": 0,
            "Min": 0,
            "Avg": 0
        },
        {
            "Name": "2022-10-12 09:30",
            "Count": 0,
            "Sum": 0,
            "Max": 0,
            "Min": 0,
            "Avg": 0
        },
        {
            "Name": "2022-10-12 09:31",
            "Count": 0,
            "Sum": 0,
            "Max": 0,
            "Min": 0,
            "Avg": 0
        },
        {
            "Name": "2022-10-12 09:32",
            "Count": 0,
            "Sum": 0,
            "Max": 0,
            "Min": 0,
            "Avg": 0
        },
        {
            "Name": "2022-10-12 09:33",
            "Count": 0,
            "Sum": 0,
            "Max": 0,
            "Min": 0,
            "Avg": 0
        },
        {
            "Name": "2022-10-12 09:34",
            "Count": 0,
            "Sum": 0,
            "Max": 0,
            "Min": 0,
            "Avg": 0
        },
        {
            "Name": "2022-10-12 09:35",
            "Count": 0,
            "Sum": 0,
            "Max": 0,
            "Min": 0,
            "Avg": 0
        },
        {
            "Name": "2022-10-12 09:36",
            "Count": 11,
            "Sum": 527,
            "Max": 74,
            "Min": 21,
            "Avg": 47.91
        },
        {
            "Name": "2022-10-12 09:37",
            "Count": 41,
            "Sum": 2167,
            "Max": 78,
            "Min": 25,
            "Avg": 52.85
        }
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "aggs":{
                "StatiSum":{
                    "sum":{
                        "field":"UserAge"
                    }
                },
                "StatiMax":{
                    "max":{
                        "field":"UserAge"
                    }
                },
                "StatiMin":{
                    "min":{
                        "field":"UserAge"
                    }
                },
                "StatiAvg":{
                    "avg":{
                        "field":"UserAge"
                    }
                }
            },
            "date_histogram":{
                "field":"CreateTime",
                "format":"yyyy-MM-dd HH:mm",
                "calendar_interval":"1m",
                "min_doc_count":0,
                "order":{
                    "_key":"asc"
                },
                "time_zone":"+08:00"
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "date_histogram#StatiGroup": {
            "buckets": [
                {
                    "key_as_string": "2022-10-17 16:05",
                    "key": 1665993900000,
                    "doc_count": 15,
                    "avg#StatiAvg": {
                        "value": 44.06666666666667
                    },
                    "max#StatiMax": {
                        "value": 76.0
                    },
                    "min#StatiMin": {
                        "value": 21.0
                    },
                    "sum#StatiSum": {
                        "value": 661.0
                    }
                },
                {
                    "key_as_string": "2022-10-17 16:06",
                    "key": 1665993960000,
                    "doc_count": 50,
                    "avg#StatiAvg": {
                        "value": 46.2
                    },
                    "max#StatiMax": {
                        "value": 79.0
                    },
                    "min#StatiMin": {
                        "value": 20.0
                    },
                    "sum#StatiSum": {
                        "value": 2310.0
                    }
                },
                {
                    "key_as_string": "2022-10-17 16:07",
                    "key": 1665994020000,
                    "doc_count": 10,
                    "avg#StatiAvg": {
                        "value": 54.8
                    },
                    "max#StatiMax": {
                        "value": 78.0
                    },
                    "min#StatiMin": {
                        "value": 30.0
                    },
                    "sum#StatiSum": {
                        "value": 548.0
                    }
                }
            ]
        }
    }
}



使用示例(日期欄位分組,按某年 1~12 月統計每個月的某個欄位每個月的總和以及這一年的總和):


//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupsDateMonthOneYear = _quickElasticSearch.GetGroupsDateMonthOneYear<TestElasticSearchModel>(m => m.CreateTime, m => m.UserName, m => m.UserAge, 2022, queryCon);

//非同步
var retGetGroupsDateMonthOneYearAsync = await _quickElasticSearch.GetGroupsDateMonthOneYearAsync<TestElasticSearchModel>(m => m.CreateTime, m => m.UserName, m => m.UserAge, 2022, queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        ["Jack1", 0, 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 54],
        ["Jack2", 0, 0, 0, 0, 0, 0, 0, 0, 0, 98, 0, 0, 98],
        ["Jack3", 0, 0, 0, 0, 0, 0, 0, 0, 0, 126, 0, 0, 126]
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "aggs":{
                "StatiGroup-1":{
                    "aggs":{
                        "StatiSum":{
                            "sum":{
                                "field":"UserAge"
                            }
                        },
                        "StatiMax":{
                            "max":{
                                "field":"UserAge"
                            }
                        },
                        "StatiMin":{
                            "min":{
                                "field":"UserAge"
                            }
                        },
                        "StatiAvg":{
                            "avg":{
                                "field":"UserAge"
                            }
                        }
                    },
                    "terms":{
                        "field":"UserName",
                        "order":[
                            {
                                "_key":"asc"
                            },
                            {
                                "_count":"asc"
                            }
                        ],
                        "size":2000000000
                    }
                }
            },
            "date_histogram":{
                "field":"CreateTime",
                "format":"yyyy-MM",
                "calendar_interval":"1M",
                "min_doc_count":0,
                "order":{
                    "_key":"asc"
                },
                "time_zone":"+08:00"
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "date_histogram#StatiGroup": {
            "buckets": [
                {
                    "key_as_string": "2022-10",
                    "key": 1664553600000,
                    "doc_count": 75,
                    "sterms#StatiGroup-1": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "Jack1",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 34.0
                                },
                                "max#StatiMax": {
                                    "value": 39.0
                                },
                                "min#StatiMin": {
                                    "value": 29.0
                                },
                                "sum#StatiSum": {
                                    "value": 68.0
                                }
                            },
                            {
                                "key": "Jack10",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 60.0
                                },
                                "max#StatiMax": {
                                    "value": 60.0
                                },
                                "min#StatiMin": {
                                    "value": 60.0
                                },
                                "sum#StatiSum": {
                                    "value": 60.0
                                }
                            },
                            {
                                "key": "Jack2",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 40.0
                                },
                                "max#StatiMax": {
                                    "value": 48.0
                                },
                                "min#StatiMin": {
                                    "value": 32.0
                                },
                                "sum#StatiSum": {
                                    "value": 80.0
                                }
                            },
                            {
                                "key": "Jack3",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 39.5
                                },
                                "max#StatiMax": {
                                    "value": 53.0
                                },
                                "min#StatiMin": {
                                    "value": 26.0
                                },
                                "sum#StatiSum": {
                                    "value": 79.0
                                }
                            },
                            {
                                "key": "Jack4",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 48.0
                                },
                                "max#StatiMax": {
                                    "value": 65.0
                                },
                                "min#StatiMin": {
                                    "value": 31.0
                                },
                                "sum#StatiSum": {
                                    "value": 96.0
                                }
                            },
                            {
                                "key": "Jack5",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 54.0
                                },
                                "max#StatiMax": {
                                    "value": 78.0
                                },
                                "min#StatiMin": {
                                    "value": 30.0
                                },
                                "sum#StatiSum": {
                                    "value": 108.0
                                }
                            },
                            {
                                "key": "Jack6",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 57.0
                                },
                                "max#StatiMax": {
                                    "value": 57.0
                                },
                                "min#StatiMin": {
                                    "value": 57.0
                                },
                                "sum#StatiSum": {
                                    "value": 57.0
                                }
                            },
                            {
                                "key": "Jack7",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 40.0
                                },
                                "max#StatiMax": {
                                    "value": 40.0
                                },
                                "min#StatiMin": {
                                    "value": 40.0
                                },
                                "sum#StatiSum": {
                                    "value": 40.0
                                }
                            },
                            {
                                "key": "Jack8",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 29.0
                                },
                                "max#StatiMax": {
                                    "value": 29.0
                                },
                                "min#StatiMin": {
                                    "value": 29.0
                                },
                                "sum#StatiSum": {
                                    "value": 29.0
                                }
                            },
                            {
                                "key": "Jack9",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 72.0
                                },
                                "max#StatiMax": {
                                    "value": 72.0
                                },
                                "min#StatiMin": {
                                    "value": 72.0
                                },
                                "sum#StatiSum": {
                                    "value": 72.0
                                }
                            },
                            {
                                "key": "Quber1",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 48.857142857142854
                                },
                                "max#StatiMax": {
                                    "value": 79.0
                                },
                                "min#StatiMin": {
                                    "value": 21.0
                                },
                                "sum#StatiSum": {
                                    "value": 342.0
                                }
                            },
                            {
                                "key": "Quber10",
                                "doc_count": 3,
                                "avg#StatiAvg": {
                                    "value": 31.0
                                },
                                "max#StatiMax": {
                                    "value": 43.0
                                },
                                "min#StatiMin": {
                                    "value": 20.0
                                },
                                "sum#StatiSum": {
                                    "value": 93.0
                                }
                            },
                            {
                                "key": "Quber11",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 29.0
                                },
                                "max#StatiMax": {
                                    "value": 29.0
                                },
                                "min#StatiMin": {
                                    "value": 29.0
                                },
                                "sum#StatiSum": {
                                    "value": 29.0
                                }
                            },
                            {
                                "key": "Quber12",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 74.0
                                },
                                "max#StatiMax": {
                                    "value": 74.0
                                },
                                "min#StatiMin": {
                                    "value": 74.0
                                },
                                "sum#StatiSum": {
                                    "value": 74.0
                                }
                            },
                            {
                                "key": "Quber2",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 51.0
                                },
                                "max#StatiMax": {
                                    "value": 78.0
                                },
                                "min#StatiMin": {
                                    "value": 25.0
                                },
                                "sum#StatiSum": {
                                    "value": 357.0
                                }
                            },
                            {
                                "key": "Quber3",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 48.142857142857146
                                },
                                "max#StatiMax": {
                                    "value": 74.0
                                },
                                "min#StatiMin": {
                                    "value": 28.0
                                },
                                "sum#StatiSum": {
                                    "value": 337.0
                                }
                            },
                            {
                                "key": "Quber4",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 53.285714285714285
                                },
                                "max#StatiMax": {
                                    "value": 76.0
                                },
                                "min#StatiMin": {
                                    "value": 25.0
                                },
                                "sum#StatiSum": {
                                    "value": 373.0
                                }
                            },
                            {
                                "key": "Quber5",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 47.285714285714285
                                },
                                "max#StatiMax": {
                                    "value": 70.0
                                },
                                "min#StatiMin": {
                                    "value": 25.0
                                },
                                "sum#StatiSum": {
                                    "value": 331.0
                                }
                            },
                            {
                                "key": "Quber6",
                                "doc_count": 6,
                                "avg#StatiAvg": {
                                    "value": 41.5
                                },
                                "max#StatiMax": {
                                    "value": 78.0
                                },
                                "min#StatiMin": {
                                    "value": 20.0
                                },
                                "sum#StatiSum": {
                                    "value": 249.0
                                }
                            },
                            {
                                "key": "Quber7",
                                "doc_count": 5,
                                "avg#StatiAvg": {
                                    "value": 43.0
                                },
                                "max#StatiMax": {
                                    "value": 61.0
                                },
                                "min#StatiMin": {
                                    "value": 21.0
                                },
                                "sum#StatiSum": {
                                    "value": 215.0
                                }
                            },
                            {
                                "key": "Quber8",
                                "doc_count": 5,
                                "avg#StatiAvg": {
                                    "value": 42.2
                                },
                                "max#StatiMax": {
                                    "value": 61.0
                                },
                                "min#StatiMin": {
                                    "value": 22.0
                                },
                                "sum#StatiSum": {
                                    "value": 211.0
                                }
                            },
                            {
                                "key": "Quber9",
                                "doc_count": 4,
                                "avg#StatiAvg": {
                                    "value": 54.75
                                },
                                "max#StatiMax": {
                                    "value": 65.0
                                },
                                "min#StatiMin": {
                                    "value": 39.0
                                },
                                "sum#StatiSum": {
                                    "value": 219.0
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}



使用示例(日期欄位分組,按月統計,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值):


//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupsDateMonth = _quickElasticSearch.GetGroupsDateMonth<TestElasticSearchModel>(m => m.CreateTime, m => m.UserName, m => m.UserAge, queryCon);

//非同步
var retGetGroupsDateMonthAsync = await _quickElasticSearch.GetGroupsDateMonthAsync<TestElasticSearchModel>(m => m.CreateTime, m => m.UserName, m => m.UserAge, queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        {
            "Name": "2022-10",
            "Count": 132,
            "Group": [
                {
                    "Name": "Jack1",
                    "Count": 2,
                    "Sum": 54,
                    "Max": 31,
                    "Min": 23,
                    "Avg": 27
                },
                {
                    "Name": "Jack2",
                    "Count": 2,
                    "Sum": 98,
                    "Max": 77,
                    "Min": 21,
                    "Avg": 49
                },
                {
                    "Name": "Jack3",
                    "Count": 2,
                    "Sum": 126,
                    "Max": 67,
                    "Min": 59,
                    "Avg": 63
                }
            ]
        }
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "aggs":{
                "StatiGroup-1":{
                    "aggs":{
                        "StatiSum":{
                            "sum":{
                                "field":"UserAge"
                            }
                        },
                        "StatiMax":{
                            "max":{
                                "field":"UserAge"
                            }
                        },
                        "StatiMin":{
                            "min":{
                                "field":"UserAge"
                            }
                        },
                        "StatiAvg":{
                            "avg":{
                                "field":"UserAge"
                            }
                        }
                    },
                    "terms":{
                        "field":"UserName",
                        "order":[
                            {
                                "_key":"asc"
                            },
                            {
                                "_count":"asc"
                            }
                        ],
                        "size":2000000000
                    }
                }
            },
            "date_histogram":{
                "field":"CreateTime",
                "format":"yyyy-MM",
                "calendar_interval":"1M",
                "min_doc_count":0,
                "order":{
                    "_key":"asc"
                },
                "time_zone":"+08:00"
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 2,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "date_histogram#StatiGroup": {
            "buckets": [
                {
                    "key_as_string": "2022-10",
                    "key": 1664553600000,
                    "doc_count": 75,
                    "sterms#StatiGroup-1": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "Jack1",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 34.0
                                },
                                "max#StatiMax": {
                                    "value": 39.0
                                },
                                "min#StatiMin": {
                                    "value": 29.0
                                },
                                "sum#StatiSum": {
                                    "value": 68.0
                                }
                            },
                            {
                                "key": "Jack10",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 60.0
                                },
                                "max#StatiMax": {
                                    "value": 60.0
                                },
                                "min#StatiMin": {
                                    "value": 60.0
                                },
                                "sum#StatiSum": {
                                    "value": 60.0
                                }
                            },
                            {
                                "key": "Jack2",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 40.0
                                },
                                "max#StatiMax": {
                                    "value": 48.0
                                },
                                "min#StatiMin": {
                                    "value": 32.0
                                },
                                "sum#StatiSum": {
                                    "value": 80.0
                                }
                            },
                            {
                                "key": "Jack3",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 39.5
                                },
                                "max#StatiMax": {
                                    "value": 53.0
                                },
                                "min#StatiMin": {
                                    "value": 26.0
                                },
                                "sum#StatiSum": {
                                    "value": 79.0
                                }
                            },
                            {
                                "key": "Jack4",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 48.0
                                },
                                "max#StatiMax": {
                                    "value": 65.0
                                },
                                "min#StatiMin": {
                                    "value": 31.0
                                },
                                "sum#StatiSum": {
                                    "value": 96.0
                                }
                            },
                            {
                                "key": "Jack5",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 54.0
                                },
                                "max#StatiMax": {
                                    "value": 78.0
                                },
                                "min#StatiMin": {
                                    "value": 30.0
                                },
                                "sum#StatiSum": {
                                    "value": 108.0
                                }
                            },
                            {
                                "key": "Jack6",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 57.0
                                },
                                "max#StatiMax": {
                                    "value": 57.0
                                },
                                "min#StatiMin": {
                                    "value": 57.0
                                },
                                "sum#StatiSum": {
                                    "value": 57.0
                                }
                            },
                            {
                                "key": "Jack7",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 40.0
                                },
                                "max#StatiMax": {
                                    "value": 40.0
                                },
                                "min#StatiMin": {
                                    "value": 40.0
                                },
                                "sum#StatiSum": {
                                    "value": 40.0
                                }
                            },
                            {
                                "key": "Jack8",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 29.0
                                },
                                "max#StatiMax": {
                                    "value": 29.0
                                },
                                "min#StatiMin": {
                                    "value": 29.0
                                },
                                "sum#StatiSum": {
                                    "value": 29.0
                                }
                            },
                            {
                                "key": "Jack9",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 72.0
                                },
                                "max#StatiMax": {
                                    "value": 72.0
                                },
                                "min#StatiMin": {
                                    "value": 72.0
                                },
                                "sum#StatiSum": {
                                    "value": 72.0
                                }
                            },
                            {
                                "key": "Quber1",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 48.857142857142854
                                },
                                "max#StatiMax": {
                                    "value": 79.0
                                },
                                "min#StatiMin": {
                                    "value": 21.0
                                },
                                "sum#StatiSum": {
                                    "value": 342.0
                                }
                            },
                            {
                                "key": "Quber10",
                                "doc_count": 3,
                                "avg#StatiAvg": {
                                    "value": 31.0
                                },
                                "max#StatiMax": {
                                    "value": 43.0
                                },
                                "min#StatiMin": {
                                    "value": 20.0
                                },
                                "sum#StatiSum": {
                                    "value": 93.0
                                }
                            },
                            {
                                "key": "Quber11",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 29.0
                                },
                                "max#StatiMax": {
                                    "value": 29.0
                                },
                                "min#StatiMin": {
                                    "value": 29.0
                                },
                                "sum#StatiSum": {
                                    "value": 29.0
                                }
                            },
                            {
                                "key": "Quber12",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 74.0
                                },
                                "max#StatiMax": {
                                    "value": 74.0
                                },
                                "min#StatiMin": {
                                    "value": 74.0
                                },
                                "sum#StatiSum": {
                                    "value": 74.0
                                }
                            },
                            {
                                "key": "Quber2",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 51.0
                                },
                                "max#StatiMax": {
                                    "value": 78.0
                                },
                                "min#StatiMin": {
                                    "value": 25.0
                                },
                                "sum#StatiSum": {
                                    "value": 357.0
                                }
                            },
                            {
                                "key": "Quber3",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 48.142857142857146
                                },
                                "max#StatiMax": {
                                    "value": 74.0
                                },
                                "min#StatiMin": {
                                    "value": 28.0
                                },
                                "sum#StatiSum": {
                                    "value": 337.0
                                }
                            },
                            {
                                "key": "Quber4",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 53.285714285714285
                                },
                                "max#StatiMax": {
                                    "value": 76.0
                                },
                                "min#StatiMin": {
                                    "value": 25.0
                                },
                                "sum#StatiSum": {
                                    "value": 373.0
                                }
                            },
                            {
                                "key": "Quber5",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 47.285714285714285
                                },
                                "max#StatiMax": {
                                    "value": 70.0
                                },
                                "min#StatiMin": {
                                    "value": 25.0
                                },
                                "sum#StatiSum": {
                                    "value": 331.0
                                }
                            },
                            {
                                "key": "Quber6",
                                "doc_count": 6,
                                "avg#StatiAvg": {
                                    "value": 41.5
                                },
                                "max#StatiMax": {
                                    "value": 78.0
                                },
                                "min#StatiMin": {
                                    "value": 20.0
                                },
                                "sum#StatiSum": {
                                    "value": 249.0
                                }
                            },
                            {
                                "key": "Quber7",
                                "doc_count": 5,
                                "avg#StatiAvg": {
                                    "value": 43.0
                                },
                                "max#StatiMax": {
                                    "value": 61.0
                                },
                                "min#StatiMin": {
                                    "value": 21.0
                                },
                                "sum#StatiSum": {
                                    "value": 215.0
                                }
                            },
                            {
                                "key": "Quber8",
                                "doc_count": 5,
                                "avg#StatiAvg": {
                                    "value": 42.2
                                },
                                "max#StatiMax": {
                                    "value": 61.0
                                },
                                "min#StatiMin": {
                                    "value": 22.0
                                },
                                "sum#StatiSum": {
                                    "value": 211.0
                                }
                            },
                            {
                                "key": "Quber9",
                                "doc_count": 4,
                                "avg#StatiAvg": {
                                    "value": 54.75
                                },
                                "max#StatiMax": {
                                    "value": 65.0
                                },
                                "min#StatiMin": {
                                    "value": 39.0
                                },
                                "sum#StatiSum": {
                                    "value": 219.0
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}



使用示例(日期欄位分組,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值):


//統計的時間間隔維度支援的表達式有(1y:1年、1q:1季度、1M:1個月、1w:1星期、1d:1天、1h:1小時、1m:1分鐘、1s:1秒鐘)
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetGroupsDateSub = _quickElasticSearch.GetGroupsDateSub<TestElasticSearchModel>(m => m.CreateTime, m => m.UserName, m => m.UserAge, "1h", "yyyy-MM-dd HH:mm", queryCon);

//非同步
var retGetGroupsDateSubAsync = await _quickElasticSearch.GetGroupsDateSubAsync<TestElasticSearchModel>(m => m.CreateTime, m => m.UserName, m => m.UserAge, "1h", "yyyy-MM-dd HH:mm", queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": [
        {
            "Name": "2022-10-12 09:00",
            "Count": 132,
            "Group": [
                {
                    "Name": "Jack1",
                    "Count": 2,
                    "Sum": 54,
                    "Max": 31,
                    "Min": 23,
                    "Avg": 27
                },
                {
                    "Name": "Jack2",
                    "Count": 2,
                    "Sum": 98,
                    "Max": 77,
                    "Min": 21,
                    "Avg": 49
                },
                {
                    "Name": "Jack3",
                    "Count": 2,
                    "Sum": 126,
                    "Max": 67,
                    "Min": 59,
                    "Avg": 63
                }
            ]
        }
    ]
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiGroup":{
            "aggs":{
                "StatiGroup-1":{
                    "aggs":{
                        "StatiSum":{
                            "sum":{
                                "field":"UserAge"
                            }
                        },
                        "StatiMax":{
                            "max":{
                                "field":"UserAge"
                            }
                        },
                        "StatiMin":{
                            "min":{
                                "field":"UserAge"
                            }
                        },
                        "StatiAvg":{
                            "avg":{
                                "field":"UserAge"
                            }
                        }
                    },
                    "terms":{
                        "field":"UserName",
                        "order":[
                            {
                                "_key":"asc"
                            },
                            {
                                "_count":"asc"
                            }
                        ],
                        "size":2000000000
                    }
                }
            },
            "date_histogram":{
                "field":"CreateTime",
                "format":"yyyy-MM-dd HH:mm",
                "calendar_interval":"1h",
                "min_doc_count":0,
                "order":{
                    "_key":"asc"
                },
                "time_zone":"+08:00"
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "date_histogram#StatiGroup": {
            "buckets": [
                {
                    "key_as_string": "2022-10-17 16:00",
                    "key": 1665993600000,
                    "doc_count": 75,
                    "sterms#StatiGroup-1": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                            {
                                "key": "Jack1",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 34.0
                                },
                                "max#StatiMax": {
                                    "value": 39.0
                                },
                                "min#StatiMin": {
                                    "value": 29.0
                                },
                                "sum#StatiSum": {
                                    "value": 68.0
                                }
                            },
                            {
                                "key": "Jack10",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 60.0
                                },
                                "max#StatiMax": {
                                    "value": 60.0
                                },
                                "min#StatiMin": {
                                    "value": 60.0
                                },
                                "sum#StatiSum": {
                                    "value": 60.0
                                }
                            },
                            {
                                "key": "Jack2",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 40.0
                                },
                                "max#StatiMax": {
                                    "value": 48.0
                                },
                                "min#StatiMin": {
                                    "value": 32.0
                                },
                                "sum#StatiSum": {
                                    "value": 80.0
                                }
                            },
                            {
                                "key": "Jack3",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 39.5
                                },
                                "max#StatiMax": {
                                    "value": 53.0
                                },
                                "min#StatiMin": {
                                    "value": 26.0
                                },
                                "sum#StatiSum": {
                                    "value": 79.0
                                }
                            },
                            {
                                "key": "Jack4",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 48.0
                                },
                                "max#StatiMax": {
                                    "value": 65.0
                                },
                                "min#StatiMin": {
                                    "value": 31.0
                                },
                                "sum#StatiSum": {
                                    "value": 96.0
                                }
                            },
                            {
                                "key": "Jack5",
                                "doc_count": 2,
                                "avg#StatiAvg": {
                                    "value": 54.0
                                },
                                "max#StatiMax": {
                                    "value": 78.0
                                },
                                "min#StatiMin": {
                                    "value": 30.0
                                },
                                "sum#StatiSum": {
                                    "value": 108.0
                                }
                            },
                            {
                                "key": "Jack6",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 57.0
                                },
                                "max#StatiMax": {
                                    "value": 57.0
                                },
                                "min#StatiMin": {
                                    "value": 57.0
                                },
                                "sum#StatiSum": {
                                    "value": 57.0
                                }
                            },
                            {
                                "key": "Jack7",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 40.0
                                },
                                "max#StatiMax": {
                                    "value": 40.0
                                },
                                "min#StatiMin": {
                                    "value": 40.0
                                },
                                "sum#StatiSum": {
                                    "value": 40.0
                                }
                            },
                            {
                                "key": "Jack8",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 29.0
                                },
                                "max#StatiMax": {
                                    "value": 29.0
                                },
                                "min#StatiMin": {
                                    "value": 29.0
                                },
                                "sum#StatiSum": {
                                    "value": 29.0
                                }
                            },
                            {
                                "key": "Jack9",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 72.0
                                },
                                "max#StatiMax": {
                                    "value": 72.0
                                },
                                "min#StatiMin": {
                                    "value": 72.0
                                },
                                "sum#StatiSum": {
                                    "value": 72.0
                                }
                            },
                            {
                                "key": "Quber1",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 48.857142857142854
                                },
                                "max#StatiMax": {
                                    "value": 79.0
                                },
                                "min#StatiMin": {
                                    "value": 21.0
                                },
                                "sum#StatiSum": {
                                    "value": 342.0
                                }
                            },
                            {
                                "key": "Quber10",
                                "doc_count": 3,
                                "avg#StatiAvg": {
                                    "value": 31.0
                                },
                                "max#StatiMax": {
                                    "value": 43.0
                                },
                                "min#StatiMin": {
                                    "value": 20.0
                                },
                                "sum#StatiSum": {
                                    "value": 93.0
                                }
                            },
                            {
                                "key": "Quber11",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 29.0
                                },
                                "max#StatiMax": {
                                    "value": 29.0
                                },
                                "min#StatiMin": {
                                    "value": 29.0
                                },
                                "sum#StatiSum": {
                                    "value": 29.0
                                }
                            },
                            {
                                "key": "Quber12",
                                "doc_count": 1,
                                "avg#StatiAvg": {
                                    "value": 74.0
                                },
                                "max#StatiMax": {
                                    "value": 74.0
                                },
                                "min#StatiMin": {
                                    "value": 74.0
                                },
                                "sum#StatiSum": {
                                    "value": 74.0
                                }
                            },
                            {
                                "key": "Quber2",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 51.0
                                },
                                "max#StatiMax": {
                                    "value": 78.0
                                },
                                "min#StatiMin": {
                                    "value": 25.0
                                },
                                "sum#StatiSum": {
                                    "value": 357.0
                                }
                            },
                            {
                                "key": "Quber3",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 48.142857142857146
                                },
                                "max#StatiMax": {
                                    "value": 74.0
                                },
                                "min#StatiMin": {
                                    "value": 28.0
                                },
                                "sum#StatiSum": {
                                    "value": 337.0
                                }
                            },
                            {
                                "key": "Quber4",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 53.285714285714285
                                },
                                "max#StatiMax": {
                                    "value": 76.0
                                },
                                "min#StatiMin": {
                                    "value": 25.0
                                },
                                "sum#StatiSum": {
                                    "value": 373.0
                                }
                            },
                            {
                                "key": "Quber5",
                                "doc_count": 7,
                                "avg#StatiAvg": {
                                    "value": 47.285714285714285
                                },
                                "max#StatiMax": {
                                    "value": 70.0
                                },
                                "min#StatiMin": {
                                    "value": 25.0
                                },
                                "sum#StatiSum": {
                                    "value": 331.0
                                }
                            },
                            {
                                "key": "Quber6",
                                "doc_count": 6,
                                "avg#StatiAvg": {
                                    "value": 41.5
                                },
                                "max#StatiMax": {
                                    "value": 78.0
                                },
                                "min#StatiMin": {
                                    "value": 20.0
                                },
                                "sum#StatiSum": {
                                    "value": 249.0
                                }
                            },
                            {
                                "key": "Quber7",
                                "doc_count": 5,
                                "avg#StatiAvg": {
                                    "value": 43.0
                                },
                                "max#StatiMax": {
                                    "value": 61.0
                                },
                                "min#StatiMin": {
                                    "value": 21.0
                                },
                                "sum#StatiSum": {
                                    "value": 215.0
                                }
                            },
                            {
                                "key": "Quber8",
                                "doc_count": 5,
                                "avg#StatiAvg": {
                                    "value": 42.2
                                },
                                "max#StatiMax": {
                                    "value": 61.0
                                },
                                "min#StatiMin": {
                                    "value": 22.0
                                },
                                "sum#StatiSum": {
                                    "value": 211.0
                                }
                            },
                            {
                                "key": "Quber9",
                                "doc_count": 4,
                                "avg#StatiAvg": {
                                    "value": 54.75
                                },
                                "max#StatiMax": {
                                    "value": 65.0
                                },
                                "min#StatiMin": {
                                    "value": 39.0
                                },
                                "sum#StatiSum": {
                                    "value": 219.0
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}


5.10、🍝 獲取總條數、最大、最小、求和或平均值等

使用示例(獲取符合條件的數據總條數):


//類似SQL:SELECT COUNT(1) FROM A
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetCount = _quickElasticSearch.GetCount<TestElasticSearchModel>(queryCon);

//非同步
var retGetCountAsync = await _quickElasticSearch.GetCountAsync<TestElasticSearchModel>(queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": 132
}


POST /test_index/_count
{
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    }
}


{
    "count": 75,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    }
}



使用示例(獲取某欄位的最大值、最小值、求和、平均值和總數等):


//類似SQL:SELECT COUNT(1),SUM(col1),MAX(col1),MIN(col1),AVG(col1) FROM A
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetStatiCount = _quickElasticSearch.GetStatiCount<TestElasticSearchModel>(m => m.UserAge, queryCon);

//非同步
var retGetStatiCountAsync = await _quickElasticSearch.GetStatiCountAsync<TestElasticSearchModel>(m => m.UserAge, queryCon);


{
    "Item1": true,
    "Item2": "",
    "Item3": {
        "Count": 132,
        "Sum": 6842,
        "Max": 79,
        "Min": 21,
        "Avg": 51.83
    }
}


POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiCount":{
            "value_count":{
                "field":"UserAge"
            }
        },
        "StatiSum":{
            "sum":{
                "field":"UserAge"
            }
        },
        "StatiMax":{
            "max":{
                "field":"UserAge"
            }
        },
        "StatiMin":{
            "min":{
                "field":"UserAge"
            }
        },
        "StatiAvg":{
            "avg":{
                "field":"UserAge"
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "avg#StatiAvg": {
            "value": 46.92
        },
        "max#StatiMax": {
            "value": 79.0
        },
        "value_count#StatiCount": {
            "value": 75
        },
        "min#StatiMin": {
            "value": 20.0
        },
        "sum#StatiSum": {
            "value": 3519.0
        }
    }
}



使用示例(獲取某些欄位相加的多個總和):


//類似SQL:SELECT SUM(col1)分類1,SUM(col2+col3+col4)分類2 FROM A
//統計的欄位條件
var fieldCons = new List<EsStatisticalSumMultiModel>()
{
    new() { AsName="分類1",Fields=new List<string>{ "Money" } },
    new() { AsName="分類2",Fields=new List<string>{ "Money", "MoneyOth" } }
};
//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetStatiSumMulti = _quickElasticSearch.GetStatiSumMulti<TestElasticSearchModel>(fieldCons, true, queryCon,
    (errMsg) =>
    {
        var thisErrMsg = errMsg;
    });

//非同步
var retGetStatiSumMultiAsync = await _quickElasticSearch.GetStatiSumMultiAsync<TestElasticSearchModel>(fieldCons, true, queryCon,
    async (errMsg) =>
    {
        var thisErrMsg = errMsg;

        await Task.CompletedTask;
    });


{
    "Item1": true,
    "Item2": "",
    "Item3": {
        "分類1": 754.63,
        "分類2": 1658.02
    }
}


# 獲取某些欄位相加的多個總和
# 類似SQL:SELECT SUM(Money)分類1,SUM(Money+MoneyOth)分類2 FROM A WHERE Id >= 1
# 此處只是做了一個演示,真正做到上述SQL的統計效果,需要在後台程式處理,也就是說「分類1」和「分類2」是2次請求,分類2這次的請求需要將StatiSumMoney和StatiSumMoneyOth統計的結果相加才能得到最總的總和
POST /test_index/_search?typed_keys=true
{
    "track_total_hits":true,
    "aggs":{
        "StatiSumMoney":{
            "sum":{
                "field":"Money"
            }
        },
        "StatiSumMoneyOth":{
            "sum":{
                "field":"MoneyOth"
            }
        }
    },
    "query":{
        "bool":{
            "must":[
                {
                    "range":{
                        "Id":{
                            "gte":1
                        }
                    }
                }
            ]
        }
    },
    "size":0
}


{
    "took": 0,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 75,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    },
    "aggregations": {
        "sum#StatiSumMoneyOth": {
            "value": 491.69998598098755
        },
        "sum#StatiSumMoney": {
            "value": 407.17499363422394
        }
    }
}



6、💎 Quick.ElasticSearch.Furion 方法

首先通過依賴注入在構造函數中得到IQuickElasticSearch的實例,具體可參照上述文檔中的相關示例。

6.1、📔 依賴注入方法

方法名稱 方法說明 方法參數 備註
AddElasticSearch 添加依賴注入服務 重載 1:()
重載 2:<T>()
該方法為IServiceCollection的擴展方法,目的是實現IQuickElasticSearch介面的註冊。
重載 1 代表註冊的是IQuickElasticSearch服務;
重載 2 傳入了泛型 T,代表的是註冊了IQuickElasticSearch服務的同時,也註冊了 T 這個服務(T這個泛型類中,在構造函數中實現了IQuickElasticSearch介面服務,該方法可能在控制台程式使用的情況較多)。
GetInstance 獲取某介面服務的實例 重載 1:()
重載 2:()
該方法為IServiceProviderIHost的擴展方法,目的是獲取某介面或類的實例。
重載 1 是基於IServiceProvider的擴展;
重載 2 是基於IHost的擴展。

6.2、💸 獲取 ElasticSearch 客戶端

方法名稱 方法說明 方法參數 返回值 備註
GetClient 獲取 ElasticSearch 客戶端 () 返回 ElasticClient 對象

6.3、📕 切換連接方法

方法名稱 方法說明 方法參數 返回值 備註
ChangeConn 切換連接 (int connId) 切換配置文件中某個連接配置,connId 為連接 Id

6.4、📗 獲取檢查創建刪除索引方法

方法名稱 方法說明 方法參數 返回值 備註
GetIndexs 獲取所有索引 (bool isIncludeSystemIndex = false) List<CatIndicesRecord> isIncludeSystemIndex:是否包含系統索引
GetIndexsAsync 獲取所有索引(非同步) (bool isIncludeSystemIndex = false) List<CatIndicesRecord> isIncludeSystemIndex:是否包含系統索引
IndexIsExist 檢查索引是否存在 (string indexName) 返回 bool 類型 indexName:索引名稱
IndexIsExistAsync 檢查索引是否存在(非同步) (string indexName) 返回 bool 類型 indexName:索引名稱
CreateIndex 如果同名索引不存在則創建索引 <T>(int numberOfReplicas = 0, int numberOfShards = 5) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 numberOfReplicas:默認副本數量,如果是單實例,注意改成 0
numberOfShards:默認分片數量
CreateIndexAsync 如果同名索引不存在則創建索引(非同步) <T>(int numberOfReplicas = 0, int numberOfShards = 5) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊
DeleteIndex 刪除索引 (string indexName) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 indexName:索引名稱
DeleteIndexAsync 刪除索引(非同步) (string indexName) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 indexName:索引名稱

6.5、📘 獲取索引健康狀態方法

方法名稱 方法說明 方法參數 返回值 備註
GetHealthStatus 獲取索引健康狀態 (string indexName) 返回 Health 枚舉對象(Green:0、Yellow:1、Red:2) indexName:索引名稱
GetHealthStatusAsync 獲取索引健康狀態 (string indexName) 返回 Health 枚舉對象(Green:0、Yellow:1、Red:2) indexName:索引名稱

6.6、📙 添加數據方法

方法名稱 方法說明 方法參數 返回值 備註
InsertModel 添加一條數據 <T>(T docData) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
docData:數據
InsertModelAsync 添加一條數據(非同步) <T>(T docData) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
docData:數據
InsertModels 添加多條數據 <T>(List<T> docDatas, Action<List<T>, string, List<T>>? insertError = null) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
docDatas:數據
insertError:錯誤回調方法(參數 1 為出錯的數據集合,參數 2 為出錯的提示資訊,參數 3 為修改的所有數據集合)
InsertModelsAsync 添加多條數據(非同步) <T>(List<T> docDatas, Action<List<T>, string, List<T>>? insertError = null) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
docDatas:數據
insertError:錯誤回調方法(參數 1 為出錯的數據集合,參數 2 為出錯的提示資訊,參數 3 為修改的所有數據集合)
InsertModelsBatch 添加多條數據(分批次) <T>(List<T> docDatas, Action<List<T>, string, List<T>>? insertError = null, int? batchCount = 1000, string backOffTime = “30s”, int? backOffRetries = 5) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
docDatas:數據
insertError:錯誤回調方法(參數 1 為出錯的數據集合,參數 2 為出錯的提示資訊,參數 3 為修改的所有數據集合)
batchCount:每批次添加的數據條數
backOffTime:集群繁忙,報 429 錯誤碼的時候,等待多久進行重試,默認為:30s
backOffRetries:重試次數,默認為:5 次

6.7、📚 修改數據方法

方法名稱 方法說明 方法參數 返回值 備註
UpdateModelFieldsById 根據主鍵 Id 修改一個或多個欄位的值 <T>(object id, object updateObj) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
id:主鍵 Id 值
updateObj:修改的動態對象,格式如:new { col1 = 123, col2 = 456},需要注意的是,填寫的欄位一定要和實體一致,如果修改的欄位在數據中不存在,會自動增加這個欄位的
UpdateModelFieldsByIdAsync 根據主鍵 Id 修改一個或多個欄位的值(非同步) <T>(object id, object updateObj) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
id:主鍵 Id 值
updateObj:修改的動態對象,格式如:new { col1 = 123, col2 = 456},需要注意的是,填寫的欄位一定要和實體一致,如果修改的欄位在數據中不存在,會自動增加這個欄位的
UpdateModelFieldsByCon 根據查詢條件修改一個或多個欄位的值 <T>(List<EsQueryModel<T>> esQueryList, object updateObj) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
esQueryList:動態查詢條件
updateObj:修改的動態對象,格式如:new { col1 = 123, col2 = 456},需要注意的是,填寫的欄位一定要和實體一致,如果修改的欄位在數據中不存在,會自動增加這個欄位的
UpdateModelFieldsByConAsync 根據查詢條件修改一個或多個欄位的值(非同步) <T>(List<EsQueryModel<T>> esQueryList, object updateObj) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
esQueryList:動態查詢條件
updateObj:修改的動態對象,格式如:new { col1 = 123, col2 = 456},需要注意的是,填寫的欄位一定要和實體一致,如果修改的欄位在數據中不存在,會自動增加這個欄位的
UpdateModel 修改一條數據 <T>(T docData) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
docData:數據
UpdateModelAsync 修改一條數據(非同步) <T>(T docData) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
docData:數據
UpdateModels 修改多條數據 <T>(List<T> docDatas, Action<List<T>, string, List<T>>? updateError = null) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
docDatas:數據
updateError:錯誤回調方法(參數 1 為出錯的數據集合,參數 2 為出錯的提示資訊,參數 3 為修改的所有數據集合)
UpdateModelsAsync 修改多條數據(非同步) <T>(List<T> docDatas, Action<List<T>, string, List<T>>? updateError = null) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
docDatas:數據
updateError:錯誤回調方法(參數 1 為出錯的數據集合,參數 2 為出錯的提示資訊,參數 3 為修改的所有數據集合)

6.8、📓 刪除數據方法

方法名稱 方法說明 方法參數 返回值 備註
DeleteModel 刪除數據(根據 Id 欄位) <T>(object dataId) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
dataId:要刪除的數據 ID
DeleteModelAsync 刪除數據(根據 Id 欄位,非同步) <T>(object dataId) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 T:泛型對象
dataId:要刪除的數據 ID
DeleteModelBy 刪除符合條件的所有數據 <T>(List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 params:動態查詢條件
DeleteModelByAsync 刪除符合條件的所有數據(非同步) <T>(List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 2 個值,第一個為是否成功,第二個為失敗的提示資訊 params:動態查詢條件

6.9、📒 獲取數據方法

方法名稱 方法說明 方法參數 返回值 備註
GetModelsAll 獲取所有數據 <T>(Expression<Func<T, object>>? field = null, bool isDesc = true) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<T>的集合 T:泛型對象
field:排序欄位,格式如:m => m.OptTime
isDesc:是否為降序(默認為降序)
GetModelsAllAsync 獲取所有數據(非同步) <T>(Expression<Func<T, object>>? field = null, bool isDesc = true) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<T>的集合 T:泛型對象
field:排序欄位,格式如:m => m.OptTime
isDesc:是否為降序(默認為降序)
GetModels 獲取匹配條件的所有數據 <T>(Expression<Func<T, object>>? field = null, bool isDesc = true, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<T>的集合 T:泛型對象
field:排序欄位,格式如:m => m.OptTime
isDesc:是否為降序(默認為降序)
esQueryList:動態查詢條件
GetModelsAsync 獲取匹配條件的所有數據(非同步) <T>(Expression<Func<T, object>>? field = null, bool isDesc = true, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<T>的集合 T:泛型對象
field:排序欄位,格式如:m => m.OptTime
isDesc:是否為降序(默認為降序)
esQueryList:動態查詢條件
GetModelsFields 獲取匹配條件的所有數據(返回指定欄位的值) <T>(List<string> speFields, Expression<Func<T, object>>? field = null, bool isDesc = true, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合 T:泛型對象
speFields:只返回設置欄位的值,如:new List<string> { “SampleId”, “SampleName” }
field:排序欄位,格式如:m => m.OptTime
isDesc:是否為降序(默認為降序)
esQueryList:動態查詢條件
GetModelsFieldsAsync 獲取匹配條件的所有數據(返回指定欄位的值,非同步) <T>(List<string> speFields, Expression<Func<T, object>>? field = null, bool isDesc = true, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合 T:泛型對象
speFields:只返回設置欄位的值,如:new List<string> { “SampleId”, “SampleName” }
field:排序欄位,格式如:m => m.OptTime
isDesc:是否為降序(默認為降序)
esQueryList:動態查詢條件
GetModel 獲取一條數據(根據某欄位執行等於查詢) <T>(Expression<Func<T, object>> field, object fieldVal) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 T 的數據對象 T:泛型對象
field:查詢的欄位
fieldVal:查詢的欄位對應的值
GetModelAsync 獲取一條數據(根據某欄位執行等於查詢,非同步) <T>(Expression<Func<T, object>> field, object fieldVal) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 T 的數據對象 T:泛型對象
field:查詢的欄位
fieldVal:查詢的欄位對應的值
GetPagerAsync 獲取分頁數據 <T>(EsPagerConModel esPagerCon, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 EsPagerModel<T>的數據對象 T:泛型對象
esPagerCon:分頁條件
esQueryList:動態查詢條件
GetPagerAsync 獲取分頁數據(非同步) <T>(EsPagerConModel esPagerCon, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 EsPagerModel<T>的數據對象 T:泛型對象
esPagerCon:分頁條件
esQueryList:動態查詢條件
GetPagerFields 獲取分頁數據(返回指定欄位的值) <T>(EsPagerConModel esPagerCon, List<string> speFields, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 EsPagerDicModel 的數據對象 T:泛型對象
esPagerCon:分頁條件
speFields:只返回設置欄位的值,如:new List<string> { “SampleId”, “SampleName” }
esQueryList:動態查詢條件
GetPagerFieldsAsync 獲取分頁數據(返回指定欄位的值,非同步) <T>(EsPagerConModel esPagerCon, List<string> speFields, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 EsPagerDicModel 的數據對象 T:泛型對象
esPagerCon:分頁條件
speFields:只返回設置欄位的值,如:new List<string> { “SampleId”, “SampleName” }
esQueryList:動態查詢條件

6.10、📋 分組統計查詢方法

方法名稱 方法說明 方法參數 返回值 備註
GetGroupValues 單個欄位分組,並返回該欄位的所有值集合 <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<string>的集合,格式如:[“分類 1″,”分類 2”] T:泛型對象
field:要分組的欄位,格式如:m=>m.col1
esQueryList:動態查詢條件
GetGroupValuesAsync 單個欄位分組,並返回該欄位的所有值集合,非同步 <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<string>的集合,格式如:[“分類 1″,”分類 2”] T:泛型對象
field:要分組的欄位,格式如:m=>m.col1
esQueryList:動態查詢條件
GetGroupCount 單個欄位分組,並返回該欄位的所有值和數量集合 <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”分類 1″,”Count”:100}] T:泛型對象
field:要分組的欄位,格式如:m=>m.col1
esQueryList:動態查詢條件
GetGroupCountAsync 單個欄位分組,並返回該欄位的所有值和數量集合,非同步 <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”分類 1″,”Count”:100}] T:泛型對象
field:要分組的欄位,格式如:m=>m.col1
esQueryList:動態查詢條件
GetGroupCountSmma 單個欄位分組,並返回另一個欄位的總和、最大值、最小值和平均值 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldStati, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”分類 1″,”Count”:100,”Sum”:999,”Max”:200,”Min”:10,”Avg”:105}] T:泛型對象
field:要分組的欄位,格式如:m=>m.col1
fieldStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.col2
esQueryList:動態查詢條件
GetGroupCountSmmaAsync 單個欄位分組,並返回另一個欄位的總和、最大值、最小值和平均值,非同步 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldStati, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”分類 1″,”Count”:100,”Sum”:999,”Max”:200,”Min”:10,”Avg”:105}] T:泛型對象
field:要分組的欄位,格式如:m=>m.col1
fieldStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.col2
esQueryList:動態查詢條件
GetGroupsCount 多個欄位分組 <T>(List<string> groupFields, List<EsQueryModel<T>>? esQueryList = null, string countKeyName = “GroupCount”) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“col1″:”val1″,”col2″:”val2″,”GroupCount”:100}] T:泛型對象
groupFields:要分組的欄位集合,如:new List<string> { “col1”, “col2” }
esQueryList:動態查詢條
countKeyName:返回統計數量的 Key 名稱,默認為:GroupCount 件
GetGroupsCountAsync 多個欄位分組,非同步 <T>(List<string> groupFields, List<EsQueryModel<T>>? esQueryList = null, string countKeyName = “GroupCount”) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“col1″:”val1″,”col2″:”val2″,”GroupCount”:100}] T:泛型對象
groupFields:要分組的欄位集合,如:new List<string> { “col1”, “col2” }
esQueryList:動態查詢條
countKeyName:返回統計數量的 Key 名稱,默認為:GroupCount 件
GetGroupsDateMonth 日期欄位分組,按月統計 <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
esQueryList:動態查詢條件
GetGroupsDateMonthAsync 日期欄位分組,按月統計,非同步 <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
esQueryList:動態查詢條件
GetGroupsDate 日期欄位分組,支援按年、季度、月份、星期、天、小時、分、秒等維度進行統計 <T>(Expression<Func<T, object>> field, string dateInterval = “1M”, string dateFormat = “yyyy-MM”, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
dateInterval:統計的日期間隔,默認為 1M(支援的表達式有 1y:1 年、1q:1 季度、1M:1 個月、1w:1 星期、1d:1 天、1h:1 小時、1m:1 分鐘、1s:1 秒鐘)
dateFormat:統計的日期格式化方式,默認為 yyyy-MM(注意大小寫,完整的格式化格式為 yyyy-MM-dd HH:mm:ss)
esQueryList:動態查詢條件
GetGroupsDateAsync 日期欄位分組,支援按年、季度、月份、星期、天、小時、分、秒等維度進行統計,非同步 <T>(Expression<Func<T, object>> field, string dateInterval = “1M”, string dateFormat = “yyyy-MM”, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
dateInterval:統計的日期間隔,默認為 1M(支援的表達式有 1y:1 年、1q:1 季度、1M:1 個月、1w:1 星期、1d:1 天、1h:1 小時、1m:1 分鐘、1s:1 秒鐘)
dateFormat:統計的日期格式化方式,默認為 yyyy-MM(注意大小寫,完整的格式化格式為 yyyy-MM-dd HH:mm:ss)
esQueryList:動態查詢條件
GetGroupsDateMonthSmma 日期欄位分組,按月統計,並返回另一個欄位的總和、最大值、最小值和平均值 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroupStati, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100,”Sum”:999,”Max”:200,”Min”:10,”Avg”:105}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
esQueryList:動態查詢條件
GetGroupsDateMonthSmmaAsync 日期欄位分組,按月統計,並返回另一個欄位的總和、最大值、最小值和平均值,非同步 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroupStati, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100,”Sum”:999,”Max”:200,”Min”:10,”Avg”:105}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
esQueryList:動態查詢條件
GetGroupsDateSmma 日期欄位分組,並返回另一個欄位的總和、最大值、最小值和平均值 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroupStati, string dateInterval = “1M”, string dateFormat = “yyyy-MM”, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100,”Sum”:999,”Max”:200,”Min”:10,”Avg”:105}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
dateInterval:統計的日期間隔,默認為 1M(支援的表達式有 1y:1 年、1q:1 季度、1M:1 個月、1w:1 星期、1d:1 天、1h:1 小時、1m:1 分鐘、1s:1 秒鐘)
dateFormat:統計的日期格式化方式,默認為 yyyy-MM(注意大小寫,完整的格式化格式為 yyyy-MM-dd HH:mm:ss)
esQueryList:動態查詢條件
GetGroupsDateSmmaAsync 日期欄位分組,並返回另一個欄位的總和、最大值、最小值和平均值,非同步 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroupStati, string dateInterval = “1M”, string dateFormat = “yyyy-MM”, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100,”Sum”:999,”Max”:200,”Min”:10,”Avg”:105}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
dateInterval:統計的日期間隔,默認為 1M(支援的表達式有 1y:1 年、1q:1 季度、1M:1 個月、1w:1 星期、1d:1 天、1h:1 小時、1m:1 分鐘、1s:1 秒鐘)
dateFormat:統計的日期格式化方式,默認為 yyyy-MM(注意大小寫,完整的格式化格式為 yyyy-MM-dd HH:mm:ss)
esQueryList:動態查詢條件
GetGroupsDateMonthOneYear 日期欄位分組,按某年 1~12 月統計每個月的某個欄位每個月的總和以及這一年的總和 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroup, Expression<Func<T, object>> fieldGroupStati, int year, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為分組統計數組,格式如:[[“小米”,3006,0,0,0,1100,0,0,0,1200,0,0,0,5306]],其中第一個值為 fieldGroup 的分組名稱;後面第二個到第十三個值為 1~12 月份的總和;第十四個值為 1~12 月份的總和 T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroup:另一個要分組的欄位,格式如:m=>m.TypeName
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
year:統計的年份,如 2021
esQueryList:動態查詢條件
GetGroupsDateMonthOneYearAsync 日期欄位分組,按某年 1~12 月統計每個月的某個欄位每個月的總和以及這一年的總和,非同步 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroup, Expression<Func<T, object>> fieldGroupStati, int year, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為分組統計數組,格式如:[[“小米”,3006,0,0,0,1100,0,0,0,1200,0,0,0,5306]],其中第一個值為 fieldGroup 的分組名稱;後面第二個到第十三個值為 1~12 月份的總和;第十四個值為 1~12 月份的總和 T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroup:另一個要分組的欄位,格式如:m=>m.TypeName
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
year:統計的年份,如 2021
esQueryList:動態查詢條件
GetGroupsDateMonth 日期欄位分組,按月統計,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroup, Expression<Func<T, object>> fieldGroupStati, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100,”Group”:[{“Name”:”碎石”,”Count”:3,”Sum”:1000,”Max”:100,”Min”:10,”Avg”:50}]}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroup:另一個要分組的欄位,格式如:m=>m.TypeName
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
esQueryList:動態查詢條件
GetGroupsDateMonthAsync 日期欄位分組,按月統計,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值,非同步 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroup, Expression<Func<T, object>> fieldGroupStati, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100,”Group”:[{“Name”:”碎石”,”Count”:3,”Sum”:1000,”Max”:100,”Min”:10,”Avg”:50}]}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroup:另一個要分組的欄位,格式如:m=>m.TypeName
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
esQueryList:動態查詢條件
GetGroupsDateSub 日期欄位分組,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroup, Expression<Func<T, object>> fieldGroupStati, string dateInterval = “1M”, string dateFormat = “yyyy-MM”, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100,”Group”:[{“Name”:”碎石”,”Count”:3,”Sum”:1000,”Max”:100,”Min”:10,”Avg”:50}]}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroup:另一個要分組的欄位,格式如:m=>m.TypeName
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
dateInterval:統計的日期間隔,默認為 1M(支援的表達式有 1y:1 年、1q:1 季度、1M:1 個月、1w:1 星期、1d:1 天、1h:1 小時、1m:1 分鐘、1s:1 秒鐘)
dateFormat:統計的日期格式化方式,默認為 yyyy-MM(注意大小寫,完整的格式化格式為 yyyy-MM-dd HH:mm:ss)
esQueryList:動態查詢條件
GetGroupsDateSubAsync 日期欄位分組,並按照另一個欄位進行分組,統計其條數、總和、最大值、最小值和平均值,非同步 <T>(Expression<Func<T, object>> field, Expression<Func<T, object>> fieldGroup, Expression<Func<T, object>> fieldGroupStati, string dateInterval = “1M”, string dateFormat = “yyyy-MM”, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為 List<Dictionary<string, object>>的集合,格式如:[{“Name”:”2021-01″,”Count”:100,”Group”:[{“Name”:”碎石”,”Count”:3,”Sum”:1000,”Max”:100,”Min”:10,”Avg”:50}]}] T:泛型對象
field:要分組的欄位(日期欄位),格式如:m=>m.CreateTime
fieldGroup:另一個要分組的欄位,格式如:m=>m.TypeName
fieldGroupStati:要統計總和、最大值、最小值和平均值的欄位,格式如:m=>m.Price
dateInterval:統計的日期間隔,默認為 1M(支援的表達式有 1y:1 年、1q:1 季度、1M:1 個月、1w:1 星期、1d:1 天、1h:1 小時、1m:1 分鐘、1s:1 秒鐘)
dateFormat:統計的日期格式化方式,默認為 yyyy-MM(注意大小寫,完整的格式化格式為 yyyy-MM-dd HH:mm:ss)
esQueryList:動態查詢條件

6.11、📜 獲取總條數、最大、最小、求和或平均值等方法

方法名稱 方法說明 方法參數 返回值 備註
GetCount 獲取符合條件的數據總條數 <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為總條數 T:泛型對象
esQueryList:動態查詢條件
GetCountAsync 獲取符合條件的數據總條數(非同步) <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為總條數 T:泛型對象
esQueryList:動態查詢條件
GetStatiCount 獲取某欄位的最大值、最小值、求和、平均值和總數等 <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為統計數據,格式為{“Count”:100,”Sum”:999,”Max”:150,”Min”:10,”Avg”:200} T:泛型對象
field:要求和、最大值、最小值和平均值的欄位 B,格式如:m=>m.col1
esQueryList:動態查詢條件
GetStatiCountAsync 獲取某欄位的最大值、最小值、求和、平均值和總數等(非同步) <T>(Expression<Func<T, object>> field, List<EsQueryModel<T>>? esQueryList = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為統計數據,格式為{“Count”:100,”Sum”:999,”Max”:150,”Min”:10,”Avg”:200} T:泛型對象
field:要求和、最大值、最小值和平均值的欄位 B,格式如:m=>m.col1
esQueryList:動態查詢條件
GetStatiSumMulti 獲取某些欄位相加的多個總和 <T>(List<EsStatisticalSumMultiModel> fieldCons, bool is2Dec, List<EsQueryModel<T>>? esQueryList = null, Action<string>? error = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為統計數據,格式為{“分類 1″:100,”分類 2”:200} T:泛型對象
fieldCons:統計的某些欄位多個總和的條件集合
is2Dec:是否將總和結果保留 2 為小數,false 則將保持原小數返回
esQueryList:動態查詢條件
error:錯誤回調方法(參數 1 為出錯的提示資訊)
GetStatiSumMultiAsync 獲取某些欄位相加的多個總和(非同步) <T>(List<EsStatisticalSumMultiModel> fieldCons, bool is2Dec, List<EsQueryModel<T>>? esQueryList = null, Func<string, Task>? error = null) 返回元組對象,包含 3 個值,第一個為是否成功,第二個為失敗的提示資訊,第三個為統計數據,格式為{“分類 1″:100,”分類 2”:200} T:泛型對象
fieldCons:統計的某些欄位多個總和的條件集合
is2Dec:是否將總和結果保留 2 為小數,false 則將保持原小數返回
esQueryList:動態查詢條件
error:錯誤回調方法(參數 1 為出錯的提示資訊)

6.12、💰 查詢條件

在上面的方法中,基本都涉及到了關於查詢條件(List<EsQueryModel<T>>? esQueryList = null)的參數,在此專門對查詢條件的使用進行說明。

創建查詢條件:

我們使用QuickElasticSearchExtension.CreateEsQueryModel<T>靜態方法來創建一個查詢條件集合對象,如下所示:

//創建查詢條件集合對象
//CreateEsQueryModel方法需要傳入泛型T,第一個參數代表和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)、第二個參數代表當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)、第三個參數代表當前組合查詢條件的回調函數,用於添加具體條件
//下述代表的是創建了一個查詢條件集合對象queryCon,並且設置了該對象第一個集合中的2個查詢條件,類似SQL:(Id >= 340506710462542 AND UserAge <= 60)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 340506710462542 AND UserAge <= 60)
            queryCon
                .AddGte(m => m.Id, 340506710462542)
                .AddLte(m => m.UserAge, 60);
        });

組合條件:

上述的例子,我們只聲明了一個組合條件,如果需要多個組合條件一起使用,則直接調用AddEsQueryModel方法繼續添加組合條件即可(該方法為上述創建的條件集合對象queryCon的擴展方法【鏈式操作】),該方法的參數和CreateEsQueryModel一模一樣,如下所示的程式碼代表(Id 大於或等於 340506710462542 並且 UserAge 小於或等於 60) 或者(Id 等於 340506710462539 或者 Id 等於 340506710462538)

var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 340506710462542 AND UserAge <= 60)
            queryCon
                .AddGte(m => m.Id, 340506710462542)
                .AddLte(m => m.UserAge, 60);
        })
    //添加另一個組合條件
    .AddEsQueryModel(
        EsQueryType.Or,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.Or,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id = 340506710462539 OR Id = 340506710462538)
            queryCon
                .AddEqu(m => m.Id, 340506710462539)
                .AddEqu(m => m.Id, 340506710462538);
        });

//同步
var retDeleteModelBy = _quickElasticSearch.DeleteModelBy<TestElasticSearchModel>(queryCon);

//非同步
var retDeleteModelByAsync = await _quickElasticSearch.DeleteModelByAsync<TestElasticSearchModel>(queryCon);

更多條件:

如果我們想添加更多條件,直接使用方法AddEsQueryModel追加即可,如下所示:

var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(……,……,……)

    //第二個組合條件
    .AddEsQueryModel(……,……,……)

    //第三個組合條件
    .AddEsQueryModel(……,……,……)

    //第四個組合條件
    .AddEsQueryModel(……,……,……)

    //第五個組合條件
    .AddEsQueryModel(……,……,……)

    //第n個組合條件
    .……;

EsQueryModel 說明:

  • PrevConType:和上一個 EsQueryModel 之間的連接類型(AND 或 OR 操作,默認為 AND 操作);
  • QueryType:查詢條件集合的連接類型,針對 QueryCon 而言(AND 或 OR 操作,默認為 AND 操作);
  • QueryCon:查詢條件集合。

查詢條件擴展方法:

方法名稱 方法說明 方法參數 返回值 備註
CreateEsQueryModel 創建查詢條件集合對象 (EsQueryType prevConType = EsQueryType.And, EsQueryType queryType = EsQueryType.And, Action<List<Func<QueryContainerDescriptor<T>, QueryContainer>>>? queryConAction = null) List<EsQueryModel<T>> T:泛型對象
prevConType:和上一個 EsQueryModel 之間的連接類型(AND 或 OR 操作,默認為 AND 操作)
queryType:當前組合查詢條件集合的連接類型,針對 QueryCon 而言(AND 或 OR 操作,默認為 AND 操作)
queryConAction:當前組合查詢條件的回調函數,用於添加具體條件
AddEsQueryModel 添加查詢條件 <T>(this List<EsQueryModel<T>> queryModels, EsQueryType prevConType = EsQueryType.And, EsQueryType queryType = EsQueryType.And, Action<List<Func<QueryContainerDescriptor<T>, QueryContainer>>>? queryConAction = null) List<EsQueryModel<T>> T:泛型對象
prevConType:和上一個 EsQueryModel 之間的連接類型(AND 或 OR 操作,默認為 AND 操作)
queryType:當前組合查詢條件集合的連接類型,針對 QueryCon 而言(AND 或 OR 操作,默認為 AND 操作)
queryConAction:當前組合查詢條件的回調函數,用於添加具體條件
GetSort 獲取排序條件 <T>(string field, bool isDesc = true) Func<SortDescriptor<T>, SortDescriptor<T>> T:泛型對象
field:排序欄位
isDesc:是否為降序(默認為降序)
GetSort 獲取排序條件 <T>(string field, bool isDesc = true) Func<SortDescriptor<T>, SortDescriptor<T>> T:泛型對象
field:排序欄位
isDesc:是否為降序(默認為降序)
CreateAndOrs 創建 AND 或 OR 條件集合 <T>() List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象

使用示例:var queryCon = QuickElasticSearchExtension.CreateAndOrs<T>();

AddEqu 添加等於查詢條件= <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, object value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.Id
value:查詢的具體值

使用示例:queryCon.AddEqu(m => m.Id, “7”);

AddIn 添加 IN 查詢條件 <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, List<object> values) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.Id
values:查詢的具體值

使用示例:queryCon.AddNotEqu(m => m.Id, new List<object>{“7″,”21″,”28”});

AddNotEqu 添加不等於查詢條件= <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, object value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.Id
value:查詢的具體值

使用示例:queryCon.AddNotEqu(m => m.Id, “7”);

AddNotEqu 添加不等於查詢條件= <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, List<object> values) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.Id
values:查詢的具體值

使用示例:queryCon.AddNotEqu(m => m.Id, new List<object>{“7″,”21″,”28”});

AddLike 添加模糊查詢條件 LIKE <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> fields, string value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
fields:欄位表達式,如:m => m.Id 或 m=>new [] {m.EngPart, m.TestType}
value:查詢的關鍵字

使用示例:queryCon.AddLike(m => m.EngPart, “測試的關鍵字”);

AddGt 添加大於查詢條件> <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, double value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.Id
value:查詢的具體值

使用示例:queryCon.AddGt(m => m.Id, 28);

AddGt 添加大於查詢條件> <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, DateTime value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.CreateTime
value:查詢的具體值

使用示例:queryCon.AddGt(m => m.CreateTime, “2022-10-12 14:10:26”);

AddGte 添加大於或等於查詢條件>= <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, double value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.Id
value:查詢的具體值

使用示例:queryCon.AddGte(m => m.Id, 28);

AddGte 添加大於或等於查詢條件>= <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, DateTime value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.CreateTime
value:查詢的具體值

使用示例:queryCon.AddGte(m => m.CreateTime, “2022-10-12 14:10:26”);

AddLt 添加小於查詢條件< <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, double value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.Id
value:查詢的具體值

使用示例:queryCon.AddLt(m => m.Id, 28);

AddLt 添加小於查詢條件< <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, DateTime value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.CreateTime
value:查詢的具體值

使用示例:queryCon.AddLt(m => m.CreateTime, “2022-10-12 14:10:26”);

AddLte 添加小於或等於查詢條件<= <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, double value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.Id
value:查詢的具體值

使用示例:queryCon.AddLte(m => m.Id, 28);

AddLte 添加小於或等於查詢條件<= <T>(this List<Func<QueryContainerDescriptor<T>, QueryContainer>> andOrCons, Expression<Func<T, object>> field, DateTime value) List<Func<QueryContainerDescriptor<T>, QueryContainer>> T:泛型對象
field:欄位表達式,如:m => m.CreateTime
value:查詢的具體值

使用示例:queryCon.AddLte(m => m.CreateTime, “2022-10-12 14:10:26”);

分頁查詢條件:

我們在使用分頁方法的時候,第一個參數需要傳入分頁查詢參數EsPagerConModel,如下所示:

//分頁條件
var pagerCon = new EsPagerConModel
{
    PageIndex = 1,
    PageSize = 15,
    OrderField = "Id",
    OrderType = "desc"
};

//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetPager = _quickElasticSearch.GetPager<TestElasticSearchModel>(pagerCon, queryCon);

//非同步
var retGetPagerAsync = await _quickElasticSearch.GetPagerAsync<TestElasticSearchModel>(pagerCon, queryCon);

EsPagerConModel 說明:

  • PageIndex:當前頁碼(默認為 1);
  • PageSize:當前頁數量(默認為 15);
  • OrderField:排序欄位;
  • OrderType:排序方式(默認為 desc)。

統計查詢條件:

我們在使用GetStatiSumMultiGetStatiSumMultiAsync方法獲取某些欄位相加的多個總和的統計的時候,第一個參數需要傳入EsStatisticalSumMultiModel,如下所示:

//類似SQL:SELECT SUM(col1)分類1,SUM(col2+col3+col4)分類2 FROM A

//統計的欄位條件
var fieldCons = new List<EsStatisticalSumMultiModel>()
{
    new() { AsName="分類1",Fields=new List<string>{ "Money" } },
    new() { AsName="分類2",Fields=new List<string>{ "Money", "MoneyOth" } }
};

//定義查詢條件:(Id >= 1)
var queryCon = QuickElasticSearchExtension
    //創建條件(默認創建了一個組合條件)
    .CreateEsQueryModel<TestElasticSearchModel>(
        EsQueryType.And,//和上一個EsQueryModel之間的連接類型(AND或OR操作,默認為AND操作)
        EsQueryType.And,//當前組合查詢條件集合的連接類型,針對QueryCon而言(AND或OR操作,默認為AND操作)
        (queryCon) =>
        {
            //查詢條件:(Id >= 1)
            queryCon.AddGte(m => m.Id, 1);
        });

//同步
var retGetStatiSumMulti = _quickElasticSearch.GetStatiSumMulti<TestElasticSearchModel>(fieldCons, true, queryCon,
    (errMsg) =>
    {
        var thisErrMsg = errMsg;
    });

//非同步
var retGetStatiSumMultiAsync = await _quickElasticSearch.GetStatiSumMultiAsync<TestElasticSearchModel>(fieldCons, true, queryCon,
    async (errMsg) =>
    {
        var thisErrMsg = errMsg;

        await Task.CompletedTask;
    });

EsStatisticalSumMultiModel 說明:

  • AsName(string):某幾個欄位相加總和最後返回的 Key 名稱;
  • Fields(List<string>):某幾個欄位相加的欄位集合。