ElasticSearch.NET NEST 搜索<T> url

ElasticSearch.NET NEST search<T> url

我正确的索引路径是 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; }
}

以上代码responsereturns下方留言;

Valid NEST response built from a successful low level call on POST: /foo/bar/_search

如何正确设置搜索路径?

试用 1

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

ArgumentException: Index name is null for the given type and no default index is set. Map an index name using ConnectionSettings.MapDefaultTypeIndices() or set a default index using ConnectionSettings.DefaultIndex().

试用 2 参考documentation

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

以上代码 returns 响应结果相同。

Valid NEST response built from a successful low level call on POST: /foo/bar/_search

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

我是 ElasticSearch 的新手,不知道 _type

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

通过 NEST 公开的大部分 Elasticsearch API 都是强类型的,包括 .Search<T>();使用此端点,"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; }
}