Youngjae
Youngjae

Reputation: 25070

ElasticSearch.NET NEST search<T> url

My correct index path is POST: /foo/_search but below code hits 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; }
}

Above code response returns below message;

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

How can I set search path correctly?

Trial 1

When I omitted settings.DefaultIndex("foo"); line, it throws ArgumentException as below, but when I set DefaultIndex(), Search<T> uses T name as a second path.

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().

Trial 2 Refer to the documentation,

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

Above code returns same result in response.

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

Upvotes: 8

Views: 10759

Answers (3)

Russ Cam
Russ Cam

Reputation: 125518

A large proportion of the Elasticsearch API exposed through NEST is in a strongly typed fashion, including .Search<T>(); with this endpoint, both "index" and "type" will be inferred from T, but sometimes you might want to set a different value to that which is inferred. In these cases, you can call additional methods on the search fluent API (or search object, if using the object initializer syntax) to override the inferred values

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

Upvotes: 13

Youngjae
Youngjae

Reputation: 25070

I was newbie in ElasticSearch and didn't know about _type.

I set identical _type name to POCO class name, and it works as I expected. So we can say, {index}/{type} is what path expression is.

Upvotes: 0

tottishi05
tottishi05

Reputation: 577

You may add other parameters in your search lambda expression var response = client.Search<Bar>(s => s.Index("indexName").Query(q => q.Term(o => o.userName, "test")));

Upvotes: 7

Related Questions