【问题标题】:ElasticSearch.NET NEST search<T> urlElasticSearch.NET NEST 搜索<T> url
【发布时间】:2017-01-08 08:10:20
【问题描述】:

我的正确索引路径是POST: /foo/_search,但下面的代码命中POST: /foo/bar/_search

var node = new Uri("http://elasticsearch-server.com:9200");
var settings = new ConnectionSettings(node);
settings.DefaultIndex("foo");
var client = new ElasticClient(settings);
var response = client.Search<Bar>(s => s
.Query(q => q.Term(o => o.userName, "test"))
);

// POCO for response fields
public class Bar
{
    public int userId { get; set; }
    public string userName { get; set; }
    public DateTime createdTime { get; set; }
}

上面的代码response返回下面的消息;

通过对 POST 的成功低级调用构建的有效 NEST 响应:/foo/bar/_search

如何正确设置搜索路径?

试用 1

当我省略settings.DefaultIndex("foo"); 行时,它会抛出ArgumentException 如下,但是当我设置DefaultIndex() 时,Search&lt;T&gt; 使用T 名称作为第二个路径。

ArgumentException:给定类型的索引名称为空,并且未设置默认索引。使用 ConnectionSettings.MapDefaultTypeIndices() 映射索引名称或使用 ConnectionSettings.DefaultIndex() 设置默认索引。

试用 2 参考documentation

var settings = new ConnectionSettings(node)
.MapDefaultTypeIndices(m => m.Add(typeof(Bar), "foo"));

以上代码在响应中返回相同的结果。

通过对 POST 的成功低级调用构建的有效 NEST 响应:/foo/bar/_search

【问题讨论】:

  • 我有相同的代码,而且效果很好。你能试试这个sn-p的代码吗: var settings = new ConnectionSettings(new Uri("elasticsearch-server.com:9200")).DefaultIndex("foo&qu… client = new ElasticClient(settings);
  • @Soren // 感谢您的评论。嗯......仍然有同样的问题。它使用 POCO 名称作为第二条路径。我需要进一步调查。

标签: c# elasticsearch nest


【解决方案1】:

通过 NEST 公开的大部分 Elasticsearch API 都是强类型的,包括.Search&lt;T&gt;();使用此端点,"index""type" 都将从 T 推断,但有时您可能希望设置与推断的值不同的值。在这些情况下,您可以在搜索流式 API(或搜索对象,如果使用对象初始化器语法)上调用其他方法来覆盖推断值

void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var connectionSettings = new ConnectionSettings(pool)
            .DefaultIndex("foo");

    var client = new ElasticClient(connectionSettings);

    // POST http://localhost:9200/foo/bar/_search
    // Will try to deserialize all _source to instances of Bar
    client.Search<Bar>(s => s
        .MatchAll()
    );

    // POST http://localhost:9200/foo/_search
    // Will try to deserialize all _source to instances of Bar
    client.Search<Bar>(s => s
        .AllTypes()
        .MatchAll()
    );

    // POST http://localhost:9200/_search
    // Will try to deserialize all _source to instances of Bar
    client.Search<Bar>(s => s
        .AllTypes()
        .AllIndices()
        .MatchAll()
    );

    connectionSettings = new ConnectionSettings(pool)
            .InferMappingFor<Bar>(m => m
                .IndexName("bars")
                .TypeName("barbar")
            );

    client = new ElasticClient(connectionSettings);

    // POST http://localhost:9200/bars/barbar/_search
    // Will try to deserialize all _source to instances of Bar
    client.Search<Bar>(s => s
        .MatchAll()
    );

    // POST http://localhost:9200/bars/_search
    // Will try to deserialize all _source to instances of Bar
    client.Search<Bar>(s => s
        .AllTypes()
        .MatchAll()
    );

    // POST http://localhost:9200/_all/barbar/_search
    // Will try to deserialize all _source to instances of Bar
    client.Search<Bar>(s => s
        .AllIndices()
        .MatchAll()
    );

    // POST http://localhost:9200/_search
    // Will try to deserialize all _source to instances of Bar
    client.Search<Bar>(s => s
        .AllIndices()
        .AllTypes()
        .MatchAll()
    );
}


public class Bar
{
    public int userId { get; set; }
    public string userName { get; set; }
    public DateTime createdTime { get; set; }
}

【讨论】:

    【解决方案2】:

    您可以在搜索 lambda 表达式中添加 其他参数 var response = client.Search&lt;Bar&gt;(s =&gt; s.Index("indexName").Query(q =&gt; q.Term(o =&gt; o.userName, "test")));

    【讨论】:

    • 嗯...Index("indexName")DefaultIndex("indexName") 给出相同的路径。
    • .DefaultIndex("defaultIndex") 如果在请求中没有指定索引名称,并且没有设置为给定类型T 推断索引名称,则将使用。有关详细信息,请参阅索引名称推断:elastic.co/guide/en/elasticsearch/client/net-api/5.x/…
    【解决方案3】:

    我是 ElasticSearch 的新手,不知道 _type

    我将相同的_type 名称设置为 POCO 类名称,它按我的预期工作。 所以我们可以说,{index}/{type} 就是路径表达式。

    【讨论】:

      猜你喜欢
      • 2016-10-26
      • 2015-03-11
      • 2018-11-07
      • 2015-08-01
      • 1970-01-01
      • 2020-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多