public async Task Search_request_must_transform_to_correct_ES_request() { var es = new ElasticsearchClient(); await es.DeleteIndexAsync("test"); var cir = await es.CreateIndexAsync("test"); Assert.IsTrue(cir.IsSuccess); Assert.IsTrue(cir.Index == "test"); var documents = new ElasticsearchDocument[] { new ElasticsearchDocument("1", new JsonObject().Add("id", 1).Add("name", "YM"), "test"), new ElasticsearchDocument("2", new JsonObject().Add("id", 2).Add("name", "SV"), "test") }; var br = await es.BulkDocumentsAsync(new BulkRequest(true).Index(documents)); Assert.IsTrue(br.IsSuccess); var search = new SearchRequest("test") .SetQuery(new MatchQuery("name", "YM")); var hits = await es.SearchAsync(search); Assert.IsTrue(hits.Hits.Total == 1); var hit = hits.Hits.Hits[0]; Assert.IsTrue(hit.Id == "1"); Assert.IsTrue(hit.Source.Property <int>("id") == 1); search = new SearchRequest("test") .SetQuery(new TermQuery("id", 2)); hits = await es.SearchAsync(search); Assert.IsTrue(hits.Hits.Total == 1); hit = hits.Hits.Hits[0]; Assert.IsTrue(hit.Id == "2"); Assert.IsTrue(hit.Source.Property <string>("name") == "SV"); search = new SearchRequest("test") .SetQuery(new ConstantScoreQuery(new RangeQuery("id", 1, null))) .SetFrom(1) .SetSize(1) .HideSource(); hits = await es.SearchAsync(search); Assert.IsTrue(hits.Hits.Total == 2); Assert.IsTrue(hits.Hits.Hits.Length == 1); hit = hits.Hits.Hits[0]; Assert.IsTrue(hit.Score == 1); Assert.IsNull(hit.Source); }
public async Task Bulk_document_crud_must_process_all_documents_correctly() { var es = new ElasticsearchClient(); await es.DeleteIndexAsync("test"); var cir = await es.CreateIndexAsync("test"); Assert.IsTrue(cir.IsSuccess); Assert.IsTrue(cir.Index == "test"); var documents = new ElasticsearchDocument[] { new ElasticsearchDocument("1", new JsonObject().Add("name", "YM"), "test"), new ElasticsearchDocument("2", new JsonObject().Add("name", "SV"), "test") }; var br = await es.BulkIndexDocumentsAsync(documents); Assert.IsTrue(br.IsSuccess); var bulk = new BulkRequest(true).Add(documents.Select(e => new BulkRequestItem("index", e.Id, e.Index, e.Type, e.Source))); br = await es.BulkDocumentsAsync(bulk); Assert.IsTrue(br.IsSuccess); var gdr = await es.GetDocumentAsync(new GetDocumentRequest("1", "test")); Assert.IsTrue(gdr.IsSuccess); Assert.IsTrue(gdr.Document.Id == "1"); Assert.IsTrue(gdr.Document.Index == "test"); Assert.IsTrue(gdr.Document.Version == 2); bulk = new BulkRequest(true).Add(documents.Select(e => new BulkRequestItem("delete", e.Id, e.Index, e.Type, null))); br = await es.BulkDocumentsAsync(bulk); Assert.IsTrue(br.IsSuccess); gdr = await es.GetDocumentAsync(new GetDocumentRequest("1", "test")); Assert.IsFalse(gdr.IsSuccess); Assert.IsTrue(gdr.Document.Id == "1"); Assert.IsTrue(gdr.Document.Index == "test"); }
public async Task Scroll_request_must_scroll_correctly() { var es = new ElasticsearchClient(); await es.DeleteIndexAsync("test"); var cir = await es.CreateIndexAsync("test"); Assert.IsTrue(cir.IsSuccess); Assert.IsTrue(cir.Index == "test"); var documents = new ElasticsearchDocument[] { new ElasticsearchDocument("1", new JsonObject().Add("id", 1).Add("name", "YM"), "test"), new ElasticsearchDocument("2", new JsonObject().Add("id", 2).Add("name", "SV"), "test") }; var br = await es.BulkDocumentsAsync(new BulkRequest(true).Index(documents)); Assert.IsTrue(br.IsSuccess); var request = new SearchRequest("test") .SetSort("_doc") .SetSize(1); var scroll = new ScrollRequest(request); var response = await es.ScrollAsync(scroll); Assert.IsFalse(response.IsEmpty); Assert.IsTrue(response.Hits.Total == 2); Assert.IsTrue(response.Hits.Hits.Length == 1); Assert.IsNotNull(response.ScrollId); scroll = new ScrollRequest(response.ScrollId); response = await es.ScrollAsync(scroll); Assert.IsFalse(response.IsEmpty); Assert.IsTrue(response.Hits.Total == 2); Assert.IsTrue(response.Hits.Hits.Length == 1); Assert.IsNotNull(response.ScrollId); scroll = new ScrollRequest(response.ScrollId); response = await es.ScrollAsync(scroll); Assert.IsTrue(response.IsEmpty); Assert.IsTrue(response.Hits.Total == 2); Assert.IsNotNull(response.ScrollId); }