【问题标题】:How to use elastic search index used in one application in another application如何在另一个应用程序中使用一个应用程序中使用的弹性搜索索引
【发布时间】:2016-08-24 11:01:33
【问题描述】:

我有一个索引为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

【问题讨论】:

  • 您不必再次创建默认索引。只需检查它是否存在。
  • 您的方法是否有任何错误
  • @PandiyanCool 没有错误.. 但它需要一些其他索引和搜索..
  • @ASN 然后尝试通过指向特定索引来运行您的搜索查询
  • @PandiyanCool 我只是按照下面的答案并指向一个特定的索引并尝试过。它仍在使用另一个索引进行搜索。您可以在 Russ Cam 提供的答案下方阅读我的评论以获取更多信息。 TIA :)

标签: c# asp.net .net elasticsearch nest


【解决方案1】:

你至少需要

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.

【讨论】:

  • 嗨,Russ,它仍在使用旧索引并获取数据。我对此感到困惑。我在服务器上有一个索引postk,其中包含30000 个文档。我创建了另一个索引zzz,其中索引了3000 个文档。在另一个应用程序中,我必须检查这个(zzz)索引是否存在,如果存在,我必须填充所有这 3000 个文档的列表。但它仍在从postk 索引中搜索数据。有没有办法打印出客户和索引详细信息并进行相应检查?当我在本地主机上尝试但在创建索引的服务器上尝试时它工作正常。TIA
  • @ASN,有很多方法可以检查请求和响应。一种方法是使用OnRequestCompleted (elastic.co/guide/en/elasticsearch/client/net-api/current/…) 将它们注销。另一种方法是使用 Fiddler 之类的 Web 调试代理从本地机器(telerik.com/fiddler)捕获流量
  • 嗨 Russ.. 感谢您的信息。当我在 Visual Studio 中以调试模式运行应用程序时,一切都显示得很好,例如索引 (zzz)、uri(https:/192.168.x.x/zzz),但数据是从其他索引 (postk) 检索的。我观察到的另一个有趣的事情是,当我将string configvalue1 = ConfigurationManager.AppSettings[https:/192.168.x.x]; 更改为string configvalue1 = ConfigurationManager.AppSettings[https:/192.168.x.x/zzz]; 时,它正在从正确的索引(zzz)中获取详细信息。如果服务器数据错误或我的代码错误,我更难以调查
  • 我删除了包含 30000 个文档的索引 (postk),然后运行了应用程序。在这种情况下它工作正常.. 它现在从 zzz 索引中检索数据并且只显示 3000 个文件
猜你喜欢
  • 2018-01-06
  • 2016-11-29
  • 2014-01-06
  • 1970-01-01
  • 2018-03-24
  • 2011-08-11
  • 2013-12-27
  • 2019-07-10
  • 2022-12-21
相关资源
最近更新 更多