欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Elasticsearch.Net使用教程 MVC4图书管理系统(2)

程序员文章站 2023-11-18 17:17:58
本文实例为大家分享了mvc4图书管理系统的制作教程,供大家参考,具体内容如下 首先项目结构图: model层的相关代码如下: book.cs代码如下:...

本文实例为大家分享了mvc4图书管理系统的制作教程,供大家参考,具体内容如下

首先项目结构图:

Elasticsearch.Net使用教程 MVC4图书管理系统(2)

model层的相关代码如下:
book.cs代码如下:

public class book 
 { 
 [key] 
 [databasegenerated(databasegeneratedoption.identity)] 
 public guid id { get; set; } 
 
 [maxlength(500)] 
 
 [display(name = "标题")] 
 public string title { get; set; } 
 
 [maxlength(5000)] 
 [display(name = "前言")] 
 public string foreword { get; set; } 
 
 [display(name = "总页数")] 
 public int pages { get; set; } 
 
 [display(name = "作者")] 
 public string author { get; set; } 
 } 
public class appcontext:dbcontext 
 { 
 public appcontext() 
 { 
  
 } 
 public dbset<book> books { get; set; } 
 } 

viewmodels的相关:

public class searchviewmodel 
 { 
 public string query { get; set; } 
 
 public ienumerable<ihit<book>> results { get; set; } 
 
 public idictionary<string, suggest[]> suggestions { get; set; } 
 
 public long elapsed { get; set; } 
 
 } 

接下来就homecontroller.cs和bookscontroller.cs的代码:

public class homecontroller : controller 
 { 
 private searchservice _searchservice; 
 public homecontroller() 
 { 
  _searchservice = new searchservice(); 
 } 
 public actionresult index() 
 { 
 
  return view(); 
 } 
 
 public actionresult search(string query, int page = 0, int pagesize = 10) 
 { 
 
  var result = _searchservice.find(query, page, pagesize); 
  var suggestion = _searchservice.findphrasesuggestion(query, 0, 3); 
 
  var viewmodel = new searchviewmodel { query = query, results = result.item1,elapsed = result.item2, suggestions = suggestion }; 
 
 
  return view("index", viewmodel); 
 } 
 
 } 
public class bookscontroller : controller 
 { 
 private appcontext db = new appcontext(); 
 
 public actionresult index() 
 { 
  return view(db.books.tolist()); 
 } 
 
 public actionresult details(guid? id) 
 { 
  if (id == null) 
  { 
  return new httpstatuscoderesult(httpstatuscode.badrequest); 
  } 
  book book = db.books.find(id); 
  if (book == null) 
  { 
  return httpnotfound(); 
  } 
  return view(book); 
 } 
 
 public actionresult create() 
 { 
  return view(); 
 } 
 
  
 [httppost] 
 [validateantiforgerytoken] 
 public actionresult create([bind(include="id,title,foreword,pages,author")] book book) 
 { 
  if (modelstate.isvalid) 
  { 
  book.id = guid.newguid(); 
  db.books.add(book); 
  db.savechanges(); 
 
  //添加书 
  elasticsearch.elasticsearch.client.index<book>(book); 
 
 
 
  return redirecttoaction("index"); 
  } 
 
  return view(book); 
 } 
 
 public actionresult edit(guid? id) 
 { 
  if (id == null) 
  { 
  return new httpstatuscoderesult(httpstatuscode.badrequest); 
  } 
  book book = db.books.find(id); 
  if (book == null) 
  { 
  return httpnotfound(); 
  } 
  return view(book); 
 } 
 
 
 [httppost] 
 [validateantiforgerytoken] 
 public actionresult edit([bind(include="id,title,foreword,pages,author")] book book) 
 { 
  if (modelstate.isvalid) 
  { 
  db.entry(book).state = entitystate.modified; 
  db.savechanges(); 
  return redirecttoaction("index"); 
  } 
  return view(book); 
 } 
 
 public actionresult delete(guid? id) 
 { 
  if (id == null) 
  { 
  return new httpstatuscoderesult(httpstatuscode.badrequest); 
  } 
  book book = db.books.find(id); 
  if (book == null) 
  { 
  return httpnotfound(); 
  } 
  return view(book); 
 } 
 
 [httppost, actionname("delete")] 
 [validateantiforgerytoken] 
 public actionresult deleteconfirmed(guid id) 
 { 
  book book = db.books.find(id); 
  db.books.remove(book); 
  db.savechanges(); 
  return redirecttoaction("index"); 
 } 
 
 public jsonresult reindex() 
 { 
  foreach (var book in db.books) 
  { 
  //indexing book 
  elasticsearch.elasticsearch.client.index<book>(book); 
  } 
  return json("ok",jsonrequestbehavior.allowget); 
 } 
 
 protected override void dispose(bool disposing) 
 { 
  if (disposing) 
  { 
  db.dispose(); 
  } 
  base.dispose(disposing); 
 } 
 } 

elasticsearch辅助类:
首先是elasticsearch.cs

public class elasticsearch 
 { 
 private static elasticclient _client; 
 public static elasticclient client 
 { 
  get 
  { 
  if (_client == null) 
  { 
   //连接配置 
   var setting = new connectionsettings(elasticsearchconfiguration.connection,elasticsearchconfiguration.defaultindex); 
   _client = new elasticclient(setting); 
  } 
  return _client; 
  } 
 
 } 
 } 

elasticsearchconfiguration.cs类

public static class elasticsearchconfiguration 
 { 
 public static string host { get { return "http://localhost"; } } 
 
 public static long port { get { return 9200; } } 
 
 public static uri connection 
 { 
  get { return new uri(string.format("{0}:{1}", host, port)); } 
 } 
 
 public static string defaultindex 
 { 
  get { return "library"; } 
 } 
 } 

searchservice.cs代码:

public class searchservice 
 { 
 public double minscore { get {return 0.0005; }} 
 
 //高亮标记前缀 
 public string prehighlighttag 
 { 
  get { return @"<strong>"; } 
 } 
 
 //高亮标记后缀 
 public string posthighlighttag 
 { 
  get { return @"</strong>"; } 
 } 
 
 
 public tuple< ienumerable<ihit<book>>,long> find(string query, int page = 0, int pagesize = 10) 
 { 
  var result = elasticsearch.elasticsearch.client.search<book>(s => s 
  .from(page * pagesize) 
  .size(pagesize) 
  .minscore(minscore) 
  .highlight(h => h 
   .pretags(prehighlighttag) 
   .posttags(posthighlighttag) 
   .onfields( 
   f => f.onfield(b => b.foreword), 
   f => f.onfield(b => b.title) 
   )) 
  .query(q => q.querystring(qs => qs.query(query).usedismax()))); 
 
  return new tuple<ienumerable<ihit<book>>, long>(result.hits,result.elapsedmilliseconds); 
 } 
 
 //查找短语建议 
 public idictionary<string, suggest[]> findphrasesuggestion(string phrase, int page = 0, int pagesize = 5) 
 { 
  var result = elasticsearch.elasticsearch.client.search<book>(s => s 
  .from(page*pagesize) 
  .size(pagesize) 
  .suggestphrase("did-you-mean", ps => ps 
   .text(phrase) 
   .onfield(f => f.foreword)) 
  .query(q => q.matchall())); 
  
  return result.suggest; 
 } 
 
 
 public ienumerable<ihit<book>> findall() 
 { 
  var result = elasticsearch.elasticsearch.client.search<book>(s => s.allindices()); 
  return result.hits; 
 } 
 
 } 

views视图
books文件夹下:
index.cshtml:

@model ienumerable<library.web.models.book> 
 
@{ 
 viewbag.title = "index"; 
 layout = "~/views/shared/_layout.cshtml"; 
} 
 
<h2>index</h2> 
 
<p> 
 @html.actionlink("创建新书", "create") 
</p> 
<table class="table"> 
 <tr> 
 <th> 
  @html.displaynamefor(model => model.title) 
 </th> 
 <th> 
  @html.displaynamefor(model => model.foreword) 
 </th> 
 <th> 
  @html.displaynamefor(model => model.pages) 
 </th> 
 <th> 
  @html.displaynamefor(model => model.author) 
 </th> 
 <th></th> 
 </tr> 
 
@foreach (var item in model) { 
 <tr> 
 <td> 
  @html.displayfor(modelitem => item.title) 
 </td> 
 <td> 
  @html.displayfor(modelitem => item.foreword) 
 </td> 
 <td> 
  @html.displayfor(modelitem => item.pages) 
 </td> 
 <td> 
  @html.displayfor(modelitem => item.author) 
 </td> 
 <td> 
  @html.actionlink("编辑", "edit", new { id=item.id }) | 
  @html.actionlink("详细", "details", new { id=item.id }) | 
  @html.actionlink("删除", "delete", new { id=item.id }) 
 </td> 
 </tr> 
} 
 
</table> 

edit.cshtml:

@model library.web.models.book 
 
@{ 
 viewbag.title = "edit"; 
 layout = "~/views/shared/_layout.cshtml"; 
} 
 
<h2>edit</h2> 
 
 
@using (html.beginform()) 
{ 
 @html.antiforgerytoken() 
 
 <div class="form-horizontal"> 
 <h4>book</h4> 
 <hr /> 
 @html.validationsummary(true) 
 @html.hiddenfor(model => model.id) 
 
 <div class="form-group"> 
  @html.labelfor(model => model.title, new { @class = "control-label col-md-2" }) 
  <div class="col-md-10"> 
  @html.editorfor(model => model.title) 
  @html.validationmessagefor(model => model.title) 
  </div> 
 </div> 
 
 <div class="form-group"> 
  @html.labelfor(model => model.foreword, new { @class = "control-label col-md-2" }) 
  <div class="col-md-10"> 
  @html.textareafor(model => model.foreword) 
  @html.validationmessagefor(model => model.foreword) 
  </div> 
 </div> 
 
 <div class="form-group"> 
  @html.labelfor(model => model.pages, new { @class = "control-label col-md-2" }) 
  <div class="col-md-10"> 
  @html.editorfor(model => model.pages) 
  @html.validationmessagefor(model => model.pages) 
  </div> 
 </div> 
 
 <div class="form-group"> 
  @html.labelfor(model => model.author, new { @class = "control-label col-md-2" }) 
  <div class="col-md-10"> 
  @html.editorfor(model => model.author) 
  @html.validationmessagefor(model => model.author) 
  </div> 
 </div> 
 
 <div class="form-group"> 
  <div class="col-md-offset-2 col-md-10"> 
  <input type="submit" value="save" class="btn btn-default" /> 
  </div> 
 </div> 
 </div> 
} 
 
<div> 
 @html.actionlink("返回列表", "index") 
</div> 
 
@section scripts { 
 @scripts.render("~/bundles/jqueryval") 
} 

details.cshtml:

@model library.web.models.book 
 
@{ 
 viewbag.title = "details"; 
 layout = "~/views/shared/_layout.cshtml"; 
} 
 
<h2>details</h2> 
 
<div> 
 <h4>book</h4> 
 <hr /> 
 <dl class="dl-horizontal"> 
 <dt> 
  @html.displaynamefor(model => model.title) 
 </dt> 
 
 <dd> 
  @html.displayfor(model => model.title) 
 </dd> 
 
 <dt> 
  @html.displaynamefor(model => model.foreword) 
 </dt> 
 
 <dd> 
  @html.displayfor(model => model.foreword) 
 </dd> 
 
 <dt> 
  @html.displaynamefor(model => model.pages) 
 </dt> 
 
 <dd> 
  @html.displayfor(model => model.pages) 
 </dd> 
 
 <dt> 
  @html.displaynamefor(model => model.author) 
 </dt> 
 
 <dd> 
  @html.displayfor(model => model.author) 
 </dd> 
 
 </dl> 
</div> 
<p> 
 @html.actionlink("编辑", "edit", new { id = model.id }) | 
 @html.actionlink("返回列表", "index") 
</p> 

delete.cshtml:

@model library.web.models.book 
 
@{ 
 viewbag.title = "delete"; 
 layout = "~/views/shared/_layout.cshtml"; 
} 
 
<h2>delete</h2> 
 
<h3>are you sure you want to delete this?</h3> 
<div> 
 <h4>book</h4> 
 <hr /> 
 <dl class="dl-horizontal"> 
 <dt> 
  @html.displaynamefor(model => model.title) 
 </dt> 
 
 <dd> 
  @html.displayfor(model => model.title) 
 </dd> 
 
 <dt> 
  @html.displaynamefor(model => model.foreword) 
 </dt> 
 
 <dd> 
  @html.displayfor(model => model.foreword) 
 </dd> 
 
 <dt> 
  @html.displaynamefor(model => model.pages) 
 </dt> 
 
 <dd> 
  @html.displayfor(model => model.pages) 
 </dd> 
 
 <dt> 
  @html.displaynamefor(model => model.author) 
 </dt> 
 
 <dd> 
  @html.displayfor(model => model.author) 
 </dd> 
 
 </dl> 
 
 @using (html.beginform()) { 
 @html.antiforgerytoken() 
 
 <div class="form-actions no-color"> 
  <input type="submit" value="delete" class="btn btn-default" /> | 
  @html.actionlink("返回列表", "index") 
 </div> 
 } 
</div> 

create.cshtml:

@model library.web.models.book 
 
@{ 
 viewbag.title = "create"; 
 layout = "~/views/shared/_layout.cshtml"; 
} 
 
<h2>创建</h2> 
 
 
@using (html.beginform()) 
{ 
 @html.antiforgerytoken() 
 
 <div class="form-horizontal"> 
 <h4>book</h4> 
 <hr /> 
 @html.validationsummary(true) 
 
 <div class="form-group"> 
  @html.labelfor(model => model.title, new { @class = "control-label col-md-2" }) 
  <div class="col-md-10"> 
  @html.editorfor(model => model.title) 
  @html.validationmessagefor(model => model.title) 
  </div> 
 </div> 
 
 <div class="form-group"> 
  @html.labelfor(model => model.foreword, new { @class = "control-label col-md-2" }) 
  <div class="col-md-10"> 
  @html.textareafor(model => model.foreword) 
  @html.validationmessagefor(model => model.foreword) 
  </div> 
 </div> 
 
 <div class="form-group"> 
  @html.labelfor(model => model.pages, new { @class = "control-label col-md-2" }) 
  <div class="col-md-10"> 
  @html.editorfor(model => model.pages) 
  @html.validationmessagefor(model => model.pages) 
  </div> 
 </div> 
 
 <div class="form-group"> 
  @html.labelfor(model => model.author, new { @class = "control-label col-md-2" }) 
  <div class="col-md-10"> 
  @html.editorfor(model => model.author) 
  @html.validationmessagefor(model => model.author) 
  </div> 
 </div> 
 
 <div class="form-group"> 
  <div class="col-md-offset-2 col-md-10"> 
  <input type="submit" value="创建" class="btn btn-default" /> 
  </div> 
 </div> 
 </div> 
} 
 
<div> 
 @html.actionlink("回到列表", "index") 
</div> 
 
@section scripts { 
 @scripts.render("~/bundles/jqueryval") 
} 

home->index.cshtml

@model library.web.viewmodels.searchviewmodel 
@{ 
 viewbag.title = "elasticsearch"; 
} 
 
<div class="jumbotron"> 
 <h1>elasticsearch入门</h1> 
 <p class="lead">安装和配置群集</p> 
 <ol> 
 <li> 
  <a href="http://www.oracle.com/technetwork/java/ 
javase/downloads/index.html">安装java</a> 
 </li> 
 <li> 
  <a href="http://www.elasticsearch.org/ 
download/">安装elasticsearch</a> 
 </li> 
 <li>运行elasticsearch</li> 
 <li><a href="/books/create">增加一些书籍</a></li> 
 </ol> 
</div> 
 
 
@if (model == null) 
{ 
 return; 
} 
<div style="margin-top: 30px;"> 
 @if (model.suggestions.any(x => x.key == "did-you-mean")) 
 { 
 <span>你的意思是: </span> 
 foreach (var suggestions in model.suggestions["did-you-mean"]) 
 { 
  var count = 0; 
  foreach (var suggestion in suggestions.options) 
  { 
  <a href="/home/search?query=@suggestion.text"><strong>@suggestion.text </strong> </a> 
  count++; 
  } 
  if (count == 0) 
  { 
  <span class="alert-danger">没有建议!</span> 
  } 
 
 } 
 } 
</div> 
 
<h3><strong>results for:</strong> @model.query</h3> 
 
@if (model != null) 
{ 
 <table class="table table-condensed"> 
 <thead> 
  <tr><th>文档的分数(排名相关度)</th><th>title</th><th>content</th><th>author</th></tr> 
 </thead> 
 
 <tbody> 
  @foreach (var result in model.results) 
  { 
  <tr> 
   <td>@result.score</td> 
   <td> 
   <a href="/books/details/@result.id"> 
    @if (result.highlights != null && result.highlights.any(x => x.key == "title")) 
    { 
    var hl = result.highlights.firstordefault(x => x.key == "title"); 
    foreach (var h in hl.value.highlights) 
    { 
     writeliteral(h); 
    } 
    } 
    else 
    { 
    writeliteral(result.source.title); 
    } 
   </a> 
   </td> 
 
   <td> 
   @if (result.highlights != null && result.highlights.any(x => x.key == "foreword")) 
   { 
    var hl = result.highlights.firstordefault(x => x.key == "foreword"); 
    foreach (var h in hl.value.highlights) 
    { 
    writeliteral(h + "..."); 
    } 
   } 
 
   </td> 
 
   <td>@result.source.author</td> 
  </tr> 
 
  } 
  @if (!model.results.any()) 
  { 
  <tr> 
   <td colspan="4" class="alert alert-danger" style="text-align:center;">没有结果发现:(</td> 
  </tr> 
  } 
 </tbody> 
 
 </table> 
 <h4><span class="label label-default">@model.results.count()</span>搜索结果用了 @model.elapsed 毫秒</h4> 
} 

_layout.cshtml

<!doctype html> 
<html> 
<head> 
 <meta charset="utf-8" /> 
 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
 <title>@viewbag.title</title> 
 @styles.render("~/content/css") 
 @scripts.render("~/bundles/modernizr") 
 
</head> 
<body> 
 <div class="navbar navbar-inverse navbar-fixed-top"> 
 <div class="container"> 
  <div class="navbar-header"> 
  <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> 
   <span class="icon-bar"></span> 
   <span class="icon-bar"></span> 
   <span class="icon-bar"></span> 
  </button> 
  @html.actionlink("elasticsearch mvc示例", "index", "home", null, new { @class = "navbar-brand" }) 
  </div> 
  <div class="navbar-collapse collapse"> 
  <ul class="nav navbar-nav"> 
   <li>@html.actionlink("home", "index", "home")</li> 
   <li>@html.actionlink("books", "index", "books")</li> 
  </ul> 
 
 
  @using (html.beginform("search", "home", formmethod.get,new {@class = "navbar-form navbar-left"})) 
  { 
   <div class="form-group"> 
   <input class="form-control" type="text" placeholder="搜索" name="query" /> 
   </div> 
   <button type="submit" class="btn btn-default">提交</button> 
  } 
 
 
 
  </div> 
 
 </div> 
 </div> 
 <div class="container body-content"> 
 @renderbody() 
 <hr /> 
 <footer> 
  <p>© @datetime.now.year - elasticsearch, nest, asp.net 应用</p> 
 </footer> 
 </div> 
 
 @scripts.render("~/bundles/jquery") 
 @scripts.render("~/bundles/bootstrap") 
 @rendersection("scripts", required: false) 
</body> 
</html> 

结果如图:

Elasticsearch.Net使用教程 MVC4图书管理系统(2)

列表页

Elasticsearch.Net使用教程 MVC4图书管理系统(2)

创建页:

Elasticsearch.Net使用教程 MVC4图书管理系统(2)

搜索结果页:

Elasticsearch.Net使用教程 MVC4图书管理系统(2)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。