开发随笔

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  25 随笔 :: 2 文章 :: 2 评论 :: 22329 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using Nest;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq.Expressions;
using System.Threading.Tasks;
 
namespace Demo
{
    public class ESHelp
    {
        private static ElasticClient client;
        static ESHelp()
        {
            string esConnectionString = ConfigurationManager.AppSettings["ElasticsearchConnectionString"].ToString();
            ConnectionSettings settings = new ConnectionSettings(new Uri(esConnectionString)).DefaultIndex("eventinfo");
            client = new ElasticClient(settings);
        }
 
 
        /// <summary>
        /// 写入接口  异步
        /// </summary>
        /// <param name="data">要写入的文档</param>
        /// <param name="index">写入的索引</param>
        /// <param name="type">写入的type</param>
        /// <returns></returns>
        public static async Task<bool> InsertAsync(object data, string index, string type)
        {
            var response = await client.IndexAsync(data, m => m.Index(index).Type(type));
 
            return true;
        }
 
        /// <summary>
        /// 写入接口 同步写入
        /// </summary>
        /// <param name="data">要写入的文档</param>
        /// <param name="index">写入的索引</param>
        /// <param name="type">写入的type</param>
        public static void Insert(object data, string index, string type)
        {
            var response = client.Index(data, m => m.Index(index.ToLower()).Type(type));
 
            if (!response.Created)
            {
                throw response.OriginalException;
            }
        }
 
 
 
        /// <summary>
        /// 文档更新(部分更新)
        /// </summary>
        /// <param name="data">需要更新的部分</param>
        /// <param name="index">es的索引</param>
        /// <param name="type">es的Type</param>
        /// <param name="id">文档的id 非实体id</param>
        /// <returns></returns>
        public static bool Update(object data, string index, string type, string id)
        {
            var result = client.Update<object, object>(id, m => m.Index(index).Type(type).Doc(data));
 
            //写入失败,返回错误信息
            if (!result.IsValid)
            {
                throw result.OriginalException;
            }
 
            return result.IsValid;
        }
 
        public static async Task<bool> UpdateAsync(object data, string index, string type, string id)
        {
            var result = await client.UpdateAsync<object, object>(id, m => m.Index(index).Type(type).Doc(data));
 
            //写入失败,返回错误信息
            if (!result.IsValid)
            {
                throw result.OriginalException;
            }
 
            return result.IsValid;
        }
 
 
        /// <summary>
        /// ES查询
        /// </summary>
        /// <typeparam name="T">Entity</typeparam>
        /// <param name="func"></param>
        /// <param name="index">索引</param>
        /// <param name="type">类型</param>
        /// <param name="page">第几页</param>
        /// <param name="rows">取多少行</param>
        /// <returns></returns>
        public static ISearchResponse<T> Seacrh<T>(IEnumerable<Func<QueryContainerDescriptor<T>, QueryContainer>> func, string index, string type, int page, int rows) where T : class
        {
            var result = client.Search<T>(s => s.Index(index).Type(type).Query(q => q.Bool(
                    m => m.Must(func)
                    ))
                  .From((page - 1) * rows)
                  .Size(rows));
 
            return result;
        }
 
 
        public static ISearchResponse<object> SearchRequery()
        {
 
            //HttpClient http = new HttpClient();
            //http.BaseAddress =new Uri(ConfigurationManager.AppSettings["ElasticsearchConnectionString"].ToString());
 
 
 
            //var result = http.PostAsync(http.BaseAddress,"");
 
            return null;
        }
 
 
 
        public static ISearchResponse<T> SeacrhEventInfo<T>(IEnumerable<Func<QueryContainerDescriptor<T>, QueryContainer>> func, Expression<Func<T, DateTime?>> sort, SearchEventDto dto, int from) where T : class
        {
            var result = client.Search<T>(s => s.Index(dto.Index).Type(dto.Type).Query(q => q.Bool(
            m => m.Must(func)
            ))
            .Sort(st => st.Ascending(sort))
            .From(from)
            .Size(1).Source(d => d.Excludes(f => f.Field("*$id"))));
            return result;
        }
 
        public static ISearchResponse<T> SeacrhEventInfoQuerys<T>(IEnumerable<Func<QueryContainerDescriptor<T>, QueryContainer>> func, Expression<Func<T, DateTime?>> sort, SearchEventDto dto) where T : class
        {
            var result = client.Search<T>(s => s.Index(dto.Index).Type(dto.Type).Query(q => q.Bool(
             m => m.Must(func)
            ))
            .Sort(st => st.Ascending(sort))
            .From(dto.From)
            .Size(dto.Size).Source(d=>d.Excludes(f=>f.Field("*$id"))));
            return result;
        }
 
        public static ISearchResponse<T> SearchInfo<T>(IEnumerable<Func<QueryContainerDescriptor<T>, QueryContainer>> func, string index, string type, int page, int rows, Func<SortDescriptor<T>,IPromise<IList<ISort>>> sort) where T : class
        {
            var result = client.Search<T>(s => s.Index(index).Type(type).Query(q => q.Bool(
                   m => m.Must(func)
                   ))
                 .Sort(sort)
                 .From((page - 1) * rows)
                 .Size(rows));
 
            return result;
        }
    }
}

  

posted on   SkyLine。  阅读(33)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示