Created
December 8, 2016 07:56
-
-
Save hhyyg/cc8fcf5d8c50d7a8bf066d76e1dd442c to your computer and use it in GitHub Desktop.
Azure Search の Blob Indexer のサンプルです
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Azure.Search; | |
using System.Configuration; | |
namespace Miso.AzureSearchSample.Models | |
{ | |
public class AzureSearchHelper | |
{ | |
public static SearchServiceClient CreateSearchServiceClient() | |
{ | |
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"]; | |
string adminApiKey = ConfigurationManager.AppSettings["SearchServiceAdminApiKey"]; | |
SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey)); | |
return serviceClient; | |
} | |
public static SearchIndexClient CreateSearchIndexClient(string indexName) | |
{ | |
string searchServiceName = ConfigurationManager.AppSettings["SearchServiceName"]; | |
string queryApiKey = ConfigurationManager.AppSettings["SearchServiceQueryApiKey"]; | |
SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, indexName, new SearchCredentials(queryApiKey)); | |
return indexClient; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Azure.Search; | |
using Microsoft.Azure.Search.Models; | |
using Miso.AzureSearchSample.Models; | |
using System.Threading.Tasks; | |
using System.Web.Mvc; | |
namespace Miso.AzureSearchSample.Controllers | |
{ | |
public class HomeController : Controller | |
{ | |
public async Task<ActionResult> Search(SearchFormModel model) | |
{ | |
if (model == null) | |
{ | |
model = new SearchFormModel(); | |
} | |
model.SearchResultLucene = await SearchIndex(indexName: "booksindex", model: model); | |
return View(model); | |
} | |
private Task<DocumentSearchResult> SearchIndex(string indexName, SearchFormModel model) | |
{ | |
SearchIndexClient indexClient = AzureSearchHelper.CreateSearchIndexClient(indexName: indexName); | |
SearchParameters searchParameter = CreateSearchParameters(); | |
if (!string.IsNullOrEmpty(model.Filter)) | |
searchParameter.Filter = model.Filter; | |
return indexClient.Documents.SearchAsync( | |
searchText: model.SearchText, | |
searchParameters: searchParameter, | |
searchRequestOptions: null); | |
} | |
private static SearchParameters CreateSearchParameters() | |
{ | |
return new SearchParameters() | |
{ | |
SearchMode = SearchMode.Any, | |
IncludeTotalResultCount = true, | |
HighlightFields = new string[] { "content" }, | |
Top = 20 | |
}; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using Microsoft.Azure.Search.Models | |
@model Miso.AzureSearchSample.Models.SearchFormModel | |
@{ | |
ViewBag.Title = "Search"; | |
var result = Model.SearchResultLucene; | |
} | |
<form class="form-inline" role="search" id="submitform"> | |
<div class="form-group"> | |
<input type="search" name="@Html.NameFor(x => x.SearchText)" | |
autocomplete="off" size="80" placeholder="検索ワードを入力します" | |
class="form-control" value="@Html.ValueFor(x => x.SearchText)" | |
id="searchText" data-provide="typeahead"/> | |
</div> | |
<button type="submit" id="search" class="btn btn-primary">Search</button> | |
</form> | |
<div class="col-md-5"> | |
<h2>Results:</h2> | |
<p style="padding-top:20px">1 - <span class="text-primary">@result.Count</span> of <span class="text-primary">@result.Count</span></p> | |
<table> | |
<tr> | |
<th>Score</th> | |
<th>Name</th> | |
</tr> | |
@foreach (var item in result.Results) | |
{ | |
<tr> | |
<td>@item.Score.ToString("0.000")</td> | |
<td>@item.Document["metadata_storage_path"]</td> | |
</tr> | |
} | |
</table> | |
<!-- Cycle through the search results --> | |
@foreach (var item in result.Results) | |
{ | |
<h3>@item.Document["metadata_storage_name"]</h3> | |
<p><label class="label-info">Score: @item.Score</label></p> | |
<table class="table"> | |
<tr> | |
<td>metadata_storage_content_type</td> | |
<td>@item.Document["metadata_storage_content_type"]</td> | |
</tr> | |
<tr> | |
<td>metadata_storage_size</td> | |
<td>@item.Document["metadata_storage_size"]</td> | |
</tr> | |
<tr> | |
<td>metadata_storage_content_md5</td> | |
<td>@(item.Document["metadata_storage_content_md5"])</td> | |
</tr> | |
<tr> | |
<td>metadata_storage_name</td> | |
<td>@item.Document["metadata_storage_name"]</td> | |
</tr> | |
<tr> | |
<td>metadata_storage_path</td> | |
<td><a href="@OutputBase64(item.Document["metadata_storage_path"])">URL to download</a></td> | |
</tr> | |
<tr> | |
<td>metadata_content_type</td> | |
<td>@item.Document["metadata_content_type"]</td> | |
</tr> | |
<tr> | |
<td>metadata_language</td> | |
<td>@item.Document["metadata_language"]</td> | |
</tr> | |
</table> | |
<p>@OutputHilightOrDocument(item, "content")</p> | |
} | |
</div> | |
@helper OutputBase64(object value) | |
{ | |
@System.Text.Encoding.UTF8.GetString(HttpServerUtility.UrlTokenDecode(value.ToString())); | |
} | |
@helper OutputHilightOrDocument(SearchResult item, string fieldName) | |
{ | |
if (item.Highlights != null && item.Highlights.ContainsKey(fieldName)) | |
{ | |
<ul> | |
@foreach (var hilight in item.Highlights[fieldName]) | |
{ | |
<li>@Html.Raw(hilight)</li> | |
} | |
</ul> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Azure.Search.Models; | |
namespace Miso.AzureSearchSample.Models | |
{ | |
public class SearchFormModel | |
{ | |
public DocumentSearchResult SearchResultLucene { get; set; } | |
/// <summary> | |
/// ユーザーが入力する検索テキスト | |
/// </summary> | |
public string SearchText { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment