Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 926
Youngjae Avatar asked Jan 08 '17 08:01

Youngjae


2 Answers

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; }
}
like image 164
Russ Cam Avatar answered Oct 31 '22 01:10

Russ Cam


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

like image 40
tottishi05 Avatar answered Oct 31 '22 03:10

tottishi05