【ElasticSearch】我在C#中尝试使用bulkAPI进行数据注册和搜索
首先
我想尝试使用ElasticSearch!从对它的兴趣开始,我进行了ElasticSearch的引入,并在Web应用程序中实施了搜索。
由于我几乎是个初学者,所以请温柔些。
※这次开发只是为了搜索而不进行索引定义和映射定义。
引入Elastic Search技术
请参考下面的链接来安装ElasticSearch和Kibana。
https://qiita.com/azumabashi/items/a6d989f02bf0e369023a
尝试安装 Elasticsearch 和 Kibana 7.5.1 版本的 Windows 版本。

因为在Kibana中逐个创建数据条目很麻烦,所以我打算使用Visual Studio开发C#控制台应用程序。
我将使用以下URL中提供的bulkAPI进行数据登记:
https://qiita.com/mino_s2000/items/191817f9d1d16320c478
NEST技巧- Elasticsearch .NET客户端
制定发展策略
我们按照以下方针进行开发:
前提:创建一个工具,可以从标题中获取URL。
1. 通过控制台应用程序从CSV文件中获取要注册的数据,并使用bulkAPI将其投入到索引中。
2. 在MVC Web应用程序中使用ElasticSearch进行搜索,并将搜索结果显示在屏幕上。
以下是我实际编写的代码。
批处理
// 検索用(登録用のバッチのため不要)
//ElasticSearch.SearchData("API");
// 登録用
// CSVからデータ取得
var list = FileOperationService.GetCsvFileForElasticSearchData();
ElasticSearch.RegistData(list);
static class ElasticSearch
{
public static string ApiUrl = "http://localhost:9200";
public static string IndexName = "testindex";
public static ConnectionSettings settings = new ConnectionSettings(new Uri(ApiUrl));
public static ElasticClient client = new ElasticClient(settings.DefaultIndex(IndexName));
public static void SearchData(string searchWord)
{
IList<IndexModel> hits = new List<IndexModel>();
try
{
var response = client.Search<IndexModel>(s => s.Size(10)
.Query(q => q.MatchPhrasePrefix(m => m.Field(f => f.title).Query(searchWord))));
foreach (var hit in response.Hits)
{
hits.Add(hit.Source);
Console.WriteLine("id:" + hit.Source.id);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("検索結果:" + hits.Count);
Console.WriteLine("何かキーを押してください");
Console.ReadKey();
}
public static void RegistData(List<IndexModel> indexModel)
{
var docList = new List<IndexModel>();
foreach (var index in indexModel)
{
docList.Add(new IndexModel
{
id = index.id,
title = index.title,
path = index.path,
description = index.description
}
);
}
var res = client.Bulk(e => e
.Index(IndexName)
.IndexMany(docList));
}
}
namespace ConsoleElasticSearchSample.Model
{
class IndexModel
{
public string id { get; set; }
public string title { get; set; }
public string path { get; set; }
public string description { get; set; }
}
}
static class FileOperationService
{
public static string FilePath = @"C:\work\elasticsearch\";
public static List<IndexModel> GetCsvFileForElasticSearchData()
{
// 配列からリストに格納する
var indexList = new List<IndexModel>();
// 読み込みたいCSVファイルのパスを指定して開く
using (var sr = new StreamReader(FilePath + "elasticsearch.csv"))
{
// 末尾まで繰り返す
while (!sr.EndOfStream)
{
// CSVファイルの一行を読み込む
string line = sr.ReadLine();
// 読み込んだ一行をカンマ毎に分けて配列に格納する
string[] values = line.Split(',');
var index = new IndexModel();
index.id = values[0];
index.title = values[1];
index.path = values[2];
index.description = values[3];
indexList.Add(index);
}
}
return indexList;
}
}
在线
共通处理参考批处理
public ActionResult About()
{
ViewBag.Message = "ElasticSearchの結果を表示します";
var list = ElasticSearch.SearchData("ela");
ViewBag.list = list;
return View();
}
@{
ViewBag.Title = "About";
}
<h2>@ViewBag.Title.</h2>
<h3>@ViewBag.Message</h3>
<p>
<table class="table table-responsive">
<tr>
<th>id</th>
<th>title</th>
<th>path</th>
<th>description</th>
</tr>
@for (var i = 0; i < ViewBag.list.Count; i++)
{
<tr>
@{
<th>@ViewBag.list[i].id</th>
<th>@ViewBag.list[i].title</th>
<th><a href="@ViewBag.list[i].path" target="_blank">@ViewBag.list[i].path</th>
<th>@ViewBag.list[i].description</th>
}
</tr>
}
</table>
</p>
CSV文件
id,title,path,description
1,Elasticsearch と Kibana 7.5.1 Windows をインストールしてみる,https://qiita.com/azumabashi/items/a6d989f02bf0e369023a,https://db-engines.com/en/ranking によると、サーチエンジンのElasticsearchはかなりいいところ(2019年12月付で全体の8位、サーチエンジンではトップ)につけています。
2,NEST Tips - Elasticsearch .NET Client,https://qiita.com/mino_s2000/items/191817f9d1d16320c478,川崎フロンターレ、J1初優勝おめでとうございます!等々力に行きたかったです。
3,Python Elasticsearch 基本的な使い方まとめ,https://qiita.com/satto_sann/items/8a63761bbfd6542bb9a2,PythonでElasticsearchを使う機会があったため情報を収集していましたが、サイトで使われているElasticsearchのバージョンが古かったり、そもそも、情報が少なかったりしたので、今回、メモとして簡単な例と共に基本的な使い方をまとめました。
开发备忘录


临时版画面

ElasticSearch的备忘录
这次在搜索中,即使使用”elastic”等关键词进行搜索,也出现了无法找到结果的现象。
GET testindex/_search
{
"query" : {
"match": {
"title": "elastic"
}
}
}
在ElasticSearch中,”match”仅用于进行完全匹配搜索,因此如果要进行部分匹配搜索,则必须使用”match_phrase_prefix”。我不知道…
GET testindex/_search
{
"query" : {
"match_phrase_prefix": {
"title": "elastic"
}
}
}
如果上述的话可进行搜索。
最终 / 最后
未来希望能够认真进行索引、映射定义,并尝试进行ElasticSearch的性能验证。
还有可能试试整理搜索查询吧。