using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elasticsearch.Net;
using GxPress.Common.Page;
using Nest;
namespace GxPress.Common.Tools
{
public class ElasticSearchHelper
{
private static ElasticSearchHelper _elasticSearchHelper;
// 默认索引
public static string DefaultIndex = "tededata";
private ElasticClient Client()
{
var nodes = new Uri[]
{
new Uri("http://127.0.0.1:9200")
};
var pool = new StaticConnectionPool(nodes);
var settings = new ConnectionSettings(pool)
.DefaultIndex(DefaultIndex)
.PrettyJson();
return new ElasticClient(settings);
}
public static ElasticClient GetInstance()
{
if (_elasticSearchHelper == null)
{
_elasticSearchHelper = new ElasticSearchHelper();
}
return _elasticSearchHelper.Client();
}
///
/// 根据关键字查询
///
///
///
/// 文件夹类型 1 通知 2话题 3 收藏 4笔记 5 普通站内信 6匿名站内信 7小组 10聊天 100 通讯录 0全部
///
public async Task> SearchAsync(string key, int typeId, int userId, int page, int perPage)
{
var result = new PagedList();
var client = GetInstance();
var query = new Nest.SearchDescriptor();
var listTedeData = new List();
var total = 0;
var queryResult = query;
//全部
if (typeId == 0)
{
query.Query(q =>
q.Bool(b =>
b.Must(m =>
m.MultiMatch(t => t.Fields(f => f.Field(obj => obj.Title).Field(obj => obj.Content).Field(obj => obj.Name)).Query(key))
)
)).PostFilter(n => n.Term(n => n.Field(f => f.UserId).Value(userId)));
}
else
{
//通讯录
if (typeId == 100)
{
query.Query(q =>
q.Bool(b =>
b.Must(m =>
m.MultiMatch(t => t.Fields(f => f.Field(obj => obj.Title).Field(obj => obj.Content).Field(obj => obj.Name)).Query(key))
)
)).PostFilter(n => n.Term(n => n.Field(f => f.SearchType).Value(typeId)));
}
else
{
query.Query(q =>
q.Bool(b =>
b.Must(m =>
m.MultiMatch(t => t.Fields(f => f.Field(obj => obj.Title).Field(obj => obj.Content).Field(obj => obj.Name)).Query(key))
)
)).PostFilter(n => n.Term(z => z.Field(f => f.UserId).Value(userId)) && n.Term(k => k.Field(m => m.SearchType).Value(typeId)));
}
}
//排序
Func, IPromise>> sortDesc = sd =>
{
//根据分值排序
sd.Descending(n => n.CreatedDate);
return sd;
};
var resultData = client.Search(x => queryResult.From((page - 1) * perPage).Size(perPage).Sort(sortDesc));
var list = resultData.Documents.ToList();
total += Convert.ToInt32(resultData.Total);
listTedeData.AddRange(list);
foreach (var item in listTedeData)
{
item.AvatarUrl = StringUtils.AddDomainMin(item.AvatarUrl);
}
result.Items = listTedeData;
result.Total = total;
return result;
}
///
/// 根据ID查询
///
///
///
public async Task Get(string id)
{
var client = GetInstance();
var response = await client.GetAsync(id, idx => idx.Index(DefaultIndex));
return response.Source;
}
///
/// 按 id 删除单条数据
///
///
///
public async Task Delete(string id)
{
var client = GetInstance();
var response = await client.DeleteAsync(id, idx => idx.Index(DefaultIndex));
return response.Result;
}
///
/// 插入数据
///
///
public async Task InsetData(TedeData data)
{
var client = GetInstance();
//成功
var result = await client.IndexAsync(data, idx => idx.Index(DefaultIndex));
if (result.ApiCall.Success)
return true;
return false;
}
}
[ElasticsearchType(RelationName = "TedeData", IdProperty = "GuId")]
public class TedeData
{
///
/// 存放guid
///
[Keyword]
public string GuId { get; set; }
///
/// 存放guid
///
[Keyword]
public string Id { get; set; }
///
/// 姓名 群名称 聊天对应用户名称
///
[Keyword]
public string Name { get; set; }
///
/// 头像地址
///
[Text]
public string AvatarUrl { get; set; }
///
/// 文件夹类型 1 通知 2话题 5 站内信 7小组 10 聊天 100通讯录
///
[Text]
public int SearchType { get; set; }
///
/// 环信ID
///
[Text]
public string ImId { get; set; }
///
/// 群聊用到对应哪个用户
///
[Text]
public int ToUserId { get; set; }
///
/// 用户ID
///
[Text]
public int UserId { get; set; }
///
/// 标题
///
[Keyword]
public string Title { get; set; }
///
/// 内容
///
[Text]
public string Content { get; set; }
///
/// 日期
///
[Text]
public DateTime? CreatedDate { get; set; }
///
/// 1 单聊 2 群聊
///
[Text]
public int ChatType { get; set; }
}
}