c# - 如何在另一个应用程序中使用一个应用程序中使用的 Elasticsearch 索引

标签 c# asp.net .net elasticsearch nest

我有一个名为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/

相关文章:

c# - 尝试将 MySQL 表显示为 cshtml

c# - 为什么 .NET 为我的应用程序保留这么多内存?

c# - 从文件中读取多个字节数组

Javascript 函数调用在 gridview 中不起作用

c# - 从隐藏列中获取单元格值

.net - 配置 .NET Core 以使用 x86 SDK

c# - 无法从本地访问 WCF 服务终结点

c# - Linq 和 SQL 之间的不同结果

c# - 尝试在 C# 中将枚举 system.array 转换为枚举 List<T>

c# - MySQL 连接器 .NET 最大数据包大小