【问题标题】:How to reuse same Nest client Elastic Search, but have all indices injected when declaring the client如何重用相同的 Nest 客户端 Elastic Search,但在声明客户端时注入所有索引
【发布时间】:2022-10-07 14:27:39
【问题描述】:

我在 Elastic Search 中有 3 个索引,我将一次查询一个(意思是 - 我想要任何时候仅来自 1 个索引的结果)。如何声明 ElasticSearch 客户端并重用?

在 SearchRequest 中添加索引名称看起来不像是一个选项,因为当我在启动客户端时没有提供任何默认索引名称时,它会给出异常。在下面添加代码,任何帮助表示赞赏。

string cloudid = \"something\";
            var credentials = new BasicAuthenticationCredentials(\"something\", \"something\");
            var connectionPool = new CloudConnectionPool(cloudid, credentials);
            var settings = new ConnectionSettings(connectionPool);
            var client = new ElasticClient(settings); //EXCEPTION HERE THAT - Index Name is NULL

ISearchRequest searchRequest = new SearchRequest(\"indexname\")
            {
                Query = new TermQuery { Field = Infer.Field<Doctor>(d => d.FirstName), Value = \"FirstName73069\" }, 
                Size = 10000
            };

            var secondSearchResponse = await client.SearchAsync<Doctor>(searchRequest);

这是我拥有的代码,它在第 5 行中断(添加注释)。 注意:我的用例必须使用 SearchRequest 对象。请相应地提出建议。

使用 Nest 7.17.4 版本。

    标签: c# .net elasticsearch nest


    【解决方案1】:

    当无法为请求解析其他索引名称时,使用默认索引名称。

    如果要在创建连接时将不同的 CLR 类型映射到不同的索引,可以使用 DefaultMappingFor

    new ConnectionSettings()
    .DefaultIndex("defaultindex")
    .DefaultMappingFor<Index1>(m => m.IndexName("index1"))
    .DefaultMappingFor<Index2>(m => m.IndexName("index2"))
    

    上面的查询将默认索引定义为“defaultindex”以及模型 Index1 和 Index2 的相应索引

    使用 new SearchRequest() 时,将针对 "index1" 查询请求,从设置中推断。

    您还可以将索引名称指定为 SearchRequest("index1")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-23
      • 1970-01-01
      • 1970-01-01
      • 2022-11-07
      • 2020-03-11
      • 1970-01-01
      相关资源
      最近更新 更多