.Net Core3.1 MVC + EF Core+ AutoFac+LayUI+Sqlserver的框架搭建--------ElasticSearch


    ES数据库配置和搭建以前也已经写过了,感兴趣的可以看我以前写的。现在我就是直接把它集成到我的架构中了。就是在以前的基础上又做了封装。

   ES数据库索引的创建和数据的插入类:

using Nest;
using System;
using System.Collections.Generic;
using System.Text;

namespace Core.Net.Common.Core.Net.Core.ES
{
    /// 
    /// ES数据库连接和创建索引,插入数据
    /// 
    /// 
  public  class ESHelper where T:class
    {
        public static readonly string url = "http://ip:port/";
        /// 
        /// 批量插入
        /// 
        /// 出入的数据
        /// 索引
        public static void ManyInsert(List obj, string index)
        {
            //设置连接字符串,DefaultIndex中的表名要小写
            var settings = new ConnectionSettings(new Uri(url)).DefaultIndex(index);
            var client = new ElasticClient(settings);
            try
            {
                var ndexResponse = client.IndexMany(obj);
                Core.FileHelper.AddLog("插入ES数据库成功" + ndexResponse.ToString(), "Log");
            }
            catch (Exception e)
            {

                Core.FileHelper.AddLog("插入ES数据库异常" + e.ToString(), "Log");
            }

        }
        /// 
        /// 单行插入
        /// 
        /// 插入的数据
        /// 索引
        public static void insert(object t, string index)
        {
            //设置连接字符串,DefaultIndex中的表名要小写
            var settings = new ConnectionSettings(new Uri(url)).DefaultIndex(index);
            var client = new ElasticClient(settings);
            //  client.IndexMany<>
            var doc = t;
            //通过 IndexDocument() 方法插入数据
            var ndexResponse = client.IndexDocument(doc);
        }
        /// 
        /// 连接ES
        /// 
        /// 索引
        /// 
        public static ElasticClient Connect_Index(string index)
        {

            //设置连接字符串,DefaultIndex中的表名要小写
            var settings = new ConnectionSettings(new Uri(url)).DefaultIndex(index);
            var client = new ElasticClient(settings);
            return client;

        }
    }
}

ES数据库查询类封装:

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace Core.Net.Common.Core.Net.Core.ES
{
        /// 
    /// ES数据库查询通用类
    /// 
    /// 
   public class ESSearchHelper where T:class,new()
    {
        /// 
        /// 带有一个查询条件的查询
        /// 
        /// ES数据库索引
        /// 查询条件表达式
        /// 查询条件字段
        /// 排序
        /// 开始页
        /// 结束页
        /// 
        public static List GetOneESList(string index, Expressionbool>> onewhere,string onefiled, Expressionbool>> orderby,int startPage,int endPage)
        {
            var client = ESHelper.Connect_Index(index);
            var result = client.Search
                (
                    x => x.Index(index)
                    .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled)))
                    .Sort(q=>q.Descending(orderby))
                    .From(startPage)
                    .Size(endPage)
                ).Documents.ToJson();
            return result.ToList();
        }
        /// 
        /// 带有两个查询条件的查询
        /// 
        /// ES数据库索引
        /// 第一个查询条件表达式
        /// 第一个查询条件字段
        /// 第二个查询条件表达式
        /// 第二个查询条件字段
        /// 排序
        /// 开始页数
        /// 结束页数
        /// 
        public static List GetTwoESList(string index, Expressionbool>> onewhere, string onefiled, Expressionbool>> twowhere,string twofiled, Expressionbool>> orderby, int startPage, int endPage)
        {
            var client = ESHelper.Connect_Index(index);
            var result = client.Search
                (
                    x => x.Index(index)
                    .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled)))
                    .Query(q => q.Match(m => m.Field(twowhere).Query(twofiled)))
                    .Sort(q => q.Descending(orderby))
                    .From(startPage)
                    .Size(endPage)
                ).Documents.ToJson();
            return result.ToList();
        }
        /// 
        /// 三个查询条件查询ES数据库
        /// 
        /// ES数据库索引
        /// 第一个查询条件表达式
        /// 第一个查询条件字段
        /// 第二个查询条件表达式
        /// 第二个查询条件字段
        /// 第三个查询条件表达式
        /// 第三个查询条件字段
        /// 排序
        /// 开始页数
        /// 结束页数
        /// 
        public static List GetThreeESList(string index, Expressionbool>> onewhere, string onefiled, Expressionbool>> twowhere, string twofiled, Expressionbool>> threewhere, string threefiled, Expressionbool>> orderby, int startPage, int endPage)
        {
            var client = ESHelper.Connect_Index(index);
            var result = client.Search
                (
                    x => x.Index(index)
                    .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled)))
                    .Query(q => q.Match(m => m.Field(twowhere).Query(twofiled)))
                    .Query(q => q.Match(m => m.Field(threefiled).Query(threefiled)))
                    .Sort(q => q.Descending(orderby))
                    .From(startPage)
                    .Size(endPage)
                ).Documents.ToJson();
            return result.ToList();
        }    
        /// 
        /// 除时间外含有一个查询条件
        /// 
        /// ES数据库索引
        /// 查询条件表达式
        /// 查询条件字段
        /// 时间字段表达式
        /// 开始时间
        /// 结束时间
        /// 排序
        /// 开始页
        /// 结束页
        /// 
        public static List GetOneTimeESList(string index, Expressionbool>> onewhere, string onefiled, Expressionbool>> timewhere,DateTime startTime,DateTime endTime, Expressionbool>> orderby, int startPage, int endPage)
        {
            var client = ESHelper.Connect_Index(index);
            var result = client.Search
                (
                    x => x.Index(index)
                    .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled)))
                    //注意:这个按时间段查询我试了好多此,好像只要添加这个时间段,就查不出来任何数据,非常奇怪,我百度了一下,好像都是这样干的。
                    .Query(q=>q.DateRange(c=>c.Field(timewhere).LessThanOrEquals(startTime).GreaterThanOrEquals(endTime)))
                    .Sort(q => q.Descending(orderby))
                    .From(startPage)
                    .Size(endPage)
                ).Documents.ToJson();
            return result.ToList();
        }
        /// 
        /// 除时间外含有一个查询条件
        /// 
        /// ES数据库索引
        /// 第一个查询条件表达式
        /// 第一个查询条件字段
        /// 第二个查询条件表达式
        /// 第二个查询条件字段
        /// 时间字段表达式
        /// 开始时间
        /// 结束时间
        /// 排序
        /// 开始页
        /// 结束页
        /// 
        public static List GetTwoTimeESList(string index, Expressionbool>> onewhere, string onefiled, Expressionbool>> twowhere, string twofiled, Expressionbool>> timewhere, DateTime startTime, DateTime endTime, Expressionbool>> orderby, int startPage, int endPage)
        {
            var client = ESHelper.Connect_Index(index);
            var result = client.Search
                (
                    x => x.Index(index)
                    .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled)))
                    .Query(q => q.Match(m => m.Field(twowhere).Query(twofiled)))
                    .Query(q => q.DateRange(c => c.Field(timewhere).LessThanOrEquals(startTime).GreaterThanOrEquals(endTime)))
                    .Sort(q => q.Descending(orderby))
                    .From(startPage)
                    .Size(endPage)
                ).Documents.ToJson();
            return result.ToList();
        }
        /// 
        /// 除时间外含有一个查询条件
        /// 
        /// ES数据库索引
        /// 第一个查询条件表达式
        /// 第一个查询条件字段
        /// 第二个查询条件表达式
        /// 第二个查询条件字段
        /// 第三个查询条件表达式
        /// 第三个查询条件字段
        /// 时间字段表达式
        /// 开始时间
        /// 结束时间
        /// 排序
        /// 开始页
        /// 结束页
        /// 
        public static List GetThreeTimeESList(string index, Expressionbool>> onewhere, string onefiled, Expressionbool>> twowhere, string twofiled, Expressionbool>> timewhere, DateTime startTime, DateTime endTime, Expressionbool>> threewhere, string threefiled, Expressionbool>> orderby, int startPage, int endPage)
        {
            var client = ESHelper.Connect_Index(index);
            var result = client.Search
                (
                    x => x.Index(index)
                    .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled)))
                    .Query(q => q.Match(m => m.Field(twowhere).Query(twofiled)))
                    .Query(q => q.Match(m => m.Field(threefiled).Query(threefiled)))
                    .Query(q => q.DateRange(c => c.Field(timewhere).LessThanOrEquals(startTime).GreaterThanOrEquals(endTime)))
                    .Sort(q => q.Descending(orderby))
                    .From(startPage)
                    .Size(endPage)
                ).Documents.ToJson();
            return result.ToList();
        }
    }
}

相关