加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ElasticSearch.NET NEST搜索网址

发布时间:2020-12-15 23:49:02 所属栏目:百科 来源:网络整理
导读:我正确的索引路径是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 Elas
我正确的索引路径是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; }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读