项目实战
前面大致介绍了ES的安装和基本使用。那么,如何在项目中落地呢?
使用nuget安装Elasticsearch.Net 5.6.4
Install-Package Elasticsearch.Net -Version 5.6.4
安装完成之后,可以在项目中查看引用
基本的增删该查在项目中的实现上面已经有所介绍,这里重点讲一下查询:
笔者使用的.NET MVC5 Web框架,对于返回的结果笔者做了一个简单封装:
public class ESearchRoot<T>
{
/// <summary>
///
/// </summary>
public int took { get; set; }
/// <summary>
///
/// </summary>
public string timed_out { get; set; }
/// <summary>
///
/// </summary>
public _shards _shards { get; set; }
/// <summary>
///
/// </summary>
public Hits<T> hits { get; set; }
}
public class _shards
{
/// <summary>
///
/// </summary>
public int total { get; set; }
/// <summary>
///
/// </summary>
public int successful { get; set; }
/// <summary>
///
/// </summary>
public int skipped { get; set; }
/// <summary>
///
/// </summary>
public int failed { get; set; }
}
public class HitsItem<T>
{
/// <summary>
///
/// </summary>
public string _index { get; set; }
/// <summary>
///
/// </summary>
public string _type { get; set; }
/// <summary>
///
/// </summary>
public string _id { get; set; }
/// <summary>
///
/// </summary>
public string _score { get; set; }
/// <summary>
///
/// </summary>
public T _source { get; set; }
/// <summary>
///
/// </summary>
public List<int> sort { get; set; }
/// <summary>
///
/// </summary>
public Highlight highlight { get; set; }
}
public class Hits<T>
{
/// <summary>
///
/// </summary>
public int total { get; set; }
/// <summary>
///
/// </summary>
public string max_score { get; set; }
/// <summary>
///
/// </summary>
public List<HitsItem<T>> hits { get; set; }
}
public class Highlight
{
/// <summary>
///
/// </summary>
public List<string> Description { get; set; }
}
因为soure返回的对象是不定的,所以使用了泛型。
本项目soure对应的类,user:
///<summary>
///
/// </summary>
public class User
{
/// <summary>
///
/// </summary>
public string Account { get; set; }
/// <summary>
///
/// </summary>
public string Phone { get; set; }
/// <summary>
///
/// </summary>
public string Email { get; set; }
/// <summary>
///
/// </summary>
public string RealName { get; set; }
/// <summary>
///
/// </summary>
public string CanReview { get; set; }
/// <summary>
///
/// </summary>
public string CanExcute { get; set; }
/// <summary>
///
/// </summary>
public string Avatar { get; set; }
/// <summary>
///
/// </summary>
public string IsUse { get; set; }
/// <summary>
///
/// </summary>
public int Id { get; set; }
/// <summary>
///
/// </summary>
public string Name { get; set; }
/// <summary>
///
/// </summary>
public string Description { get; set; }
/// <summary>
///
/// </summary>
public DateTime CreateTime { get; set; }
/// <summary>
///
/// </summary>
public DateTime ModifyTime { get; set; }
}
项目使用了带条件的分页查询:
public List<AdminUser> GetBySomeWhere(string keyword, int limit, int pageSize, out int total)
{
List<AdminUser> users = new List<AdminUser>();
total = 0;
try
{
var settings = new ConnectionConfiguration(new Uri("http://localhost:9200/"))
.RequestTimeout(TimeSpan.FromMinutes(2));
var lowlevelClient = new ElasticLowLevelClient(settings);
//根据不同的参数 来构建不同的查询条件
var request = new object();
if (!String.IsNullOrEmpty(keyword))
{
request = new
{
from = limit,
size = pageSize,
query = new
{
match = new
{
Description = keyword
}
},
highlight = new
{
fields = new
{
Description = new { }
}
},
sort = new
{
Id = new
{
order = "desc"
}
}
};
}
else
{
request = new
{
from = limit,
size = pageSize,
query = new
{
match_all = new
{
}
},
highlight = new
{
fields = new
{
Description = new { }
}
},
sort = new
{
Id = new
{
order = "desc"
}
}
};
}
var searchResponse = lowlevelClient.Search<string>("user", "guest", request);
bool successful = searchResponse.Success;
var responseJson = searchResponse.Body;
if (!successful)
{
return users;
}
ESearchRoot<User> root = JsonHelper.JSONStringObject<ESearchRoot<User>>(responseJson);
if (root != null)
{
total = root.hits.total;
foreach (HitsItem<User> item in root.hits.hits)
{
if (item._source != null)
{
string highlightDescription = String.Empty;
StringBuilder sbDs = new StringBuilder();
if (item.highlight != null && item.highlight.Description.Count > 0)
{
//ighlightDescription = item.highlight.Description[0];
foreach (var d in item.highlight.Description)
{
sbDs.Append(d);
}
highlightDescription = sbDs.ToString();
}
AdminUser user = new AdminUser
{
Id = item._source.Id,
RealName = item._source.RealName,
Account = item._source.Account,
Email = item._source.Email,
Phone = item._source.Phone,
//IsUse=item._source.IsUse,
Avatar = item._source.Avatar,
Description = item._source.Description,
HighlightDescription = highlightDescription,
CreateTime = item._source.CreateTime,
ModifyTime = item._source.ModifyTime
};
users.Add(user);
}
}
}
return users;
}
catch (ElasticsearchClientException ex)
{
//Log4Helper.Error
}
return users;
}
项目最终的效果如下:
版权声明:
本文为智客工坊「楠木大叔」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。