我有一个名为zzz
的索引应用程序,并且将几个文档编入了索引。
string configvalue1 = ConfigurationManager.AppSettings["http://localhost:9200/"];
var pool = new SingleNodeConnectionPool(new Uri(configvalue1));
var defaultIndex = "zzz";
**settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex)
.MapDefaultTypeNames(m => m.Add(typeof(Class1), "type"))
.PrettyJson()
.DisableDirectStreaming();
client = new ElasticClient(settings);**
if (client.IndexExists(defaultIndex).Exists && ConfigurationManager.AppSettings["syncMode"] == "Full")
{
client.DeleteIndex(defaultIndex);
client.CreateIndex(defaultIndex);
}
return client;
现在,在一个全新的应用程序中,我必须检查
zzz
是否存在,并将其用于某些搜索操作。我是否仍需要编写上述代码中**
之间的所有内容,还是仅连接到池并检查索引?这是我的看法:
configvalue1 = ConfigurationManager.AppSettings["http://localhost:9200/"];
var pool = new SingleNodeConnectionPool(new Uri(configvalue1));
settings = new ConnectionSettings(pool);
client = new ElasticClient(settings);
// to check if the index exists and return if exist
if (client.IndexExists("zzz").Exists)
{
return client;
}
只是添加到上面的问题:
我想在索引之前实现这样的条件:
Index doesnt exist && sync mode == full --> Create index
Index exist && sync mode==full --> Delete old index and create a new
Index doesnt exist && sync mode == new --> Create index
Index exist && sync mode==new --> Use the existing index
TIA
最佳答案
您至少需要
string configvalue1 = ConfigurationManager.AppSettings["http://localhost:9200/"];
var pool = new SingleNodeConnectionPool(new Uri(configvalue1));
var defaultIndex = "zzz";
var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
if (!client.IndexExists(defaultIndex).Exists &&
ConfigurationManager.AppSettings["syncMode"] == "Full")
{
// do something when the index is not there. Maybe create it?
}
如果您打算在此应用程序中使用
Class1
,并且不想在所有请求中为其指定类型,则添加.MapDefaultTypeNames(m => m.Add(typeof(Class1), "type"))
连接设置。
用
.PrettyJson()
这对于开发目的很有用,但是我不建议在生产环境中使用,因为所有请求和响应都将更大,因为json会缩进。
同样地,
.DisableDirectStreaming()
同样,除非您需要记录应用程序端的所有请求和响应,否则我不建议在生产环境中使用它。此设置将所有请求和响应字节都缓存在
MemoryStream
中,以便可以在客户端外部(例如,在 OnRequestCompleted
中)读取它们。More details on the settings can be found in the documentation.
关于c# - 如何在另一个应用程序中使用一个应用程序中使用的 Elasticsearch 索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36935703/