微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – ElasticSearch.NET NEST搜索网址

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

上面的代码响应返回以下消息;

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"));

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

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

解决方法

通过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; }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐