var connection = new InMemoryConnection(); var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); /* 1.通过 IConnectionPool 和 IConnection 重载创建一个 ConnectionSettings 2.connection 使用的是 InMemoryConnection 的无参构造函数,表示为所有相应内容返回 200 状态码,并不真实执行任何 IO 操作。 */ var settings = new ConnectionSettings(connectionPool, connection); var client = new ElasticClient(settings);
var response = new { took = 1, timed_out = false, _shards = new { total = 2, successful = 2, failed = 0 }, hits = new { total = new { value = 25 }, max_score = 1.0, hits = Enumerable.Range(1, 25).Select(i => (object)new { _index = "project", _type = "project", _id = $"Project {i}", _score = 1.0, _source = new { name = $"Project {i}" } }).ToArray() } };
var responseBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(response)); // InMemoryConnection 配置为始终返回responseBytes 以及 HTTP状态代码为 200 var connection = new InMemoryConnection(responseBytes, 200); var connectionPool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); var settings = new ConnectionSettings(connectionPool, connection).DefaultIndex("project"); var client = new ElasticClient(settings);
var searchResponse = client.Search<Project>(s => s.MatchAll());