8

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

2
  • I have the same code and it works well. could you try this snippet of the code: var settings = new ConnectionSettings(new Uri("elasticsearch-server.com:9200")).DefaultIndex("foo&qu… client = new ElasticClient(settings); Commented Jan 8, 2017 at 8:30
  • @Soren // Thanks for your comment. Hmm... still has same issue. It uses POCO name as a second path. I need to investigate more. Commented Jan 8, 2017 at 8:54

3 Answers 3

13

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

7

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

2 Comments

Hmm...either Index("indexName") or DefaultIndex("indexName") gives identical path.
.DefaultIndex("defaultIndex") will be used if no index name is specified in the request, and no index name is set to be inferred for a given type T. See Index Name Inference for more details: elastic.co/guide/en/elasticsearch/client/net-api/5.x/…
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.