public void Setup() { _sut = new ElasticBand(new DefaultHttpClientFactory(), new ElasticQueryBuilder()); _sut.SetElasticsearchUrl("https://localhost:9200"); _sut.SetElasticsearchAuthentication("B0pGRnABUbqCwsoaMxi0:gexHoph8SnK_HpTuLkOgtw"); _sut.GetClient().DeleteAsync(_index).Wait(); }
public async Task ElasticBand_methods_should_index_get_query_and_delete_documents_in_elasticsearch_for_type_TestObject2() { // Decide the index name and instantiate an ElasticBand... var index = "testindex2"; var elasticBand = new ElasticBand(new DefaultHttpClientFactory(), new ElasticQueryBuilder()); elasticBand.SetElasticsearchUrl("https://localhost:9200"); elasticBand.SetElasticsearchAuthentication("B0pGRnABUbqCwsoaMxi0:gexHoph8SnK_HpTuLkOgtw"); elasticBand.GetClient().DeleteAsync(index).Wait(); // Create some objects to index var object1 = new TestObject2 { Name = "Andrew", Birthday = new DateTime(2019, 2, 7) }; var object2 = new TestObject2 { Name = "James", Birthday = new DateTime(2019, 7, 27) }; var object3 = new TestObject2 { Name = "Drew", Birthday = new DateTime(2019, 1, 9) }; // Index the objects, optionally pass in an id, if omitted a new Guid will be generated... var indexResponse1 = await elasticBand.Index <TestObject2>(index, object1, "id1"); var indexResponse2 = await elasticBand.Index <TestObject2>(index, object2, "id2"); var indexResponse3 = await elasticBand.Index <TestObject2>(index, object3, "id3"); // Speedy lookup if you know an id... var getResponse1 = await elasticBand.GetDocumentByID <TestObject2>(index, "id2"); // Update a property and re-index... getResponse1.Data.Name = "Jamie"; await elasticBand.Index <TestObject2>(index, getResponse1.Data, "id2"); await Task.Delay(1000); // Give ES a chance to internally update the indicies // Query in various ways... var queryResult1 = await elasticBand.Query <TestObject2>(index, "Jamie"); var queryResult2 = await elasticBand.Query <TestObject2>(index, "name:Drew"); var queryResult3 = await elasticBand.Query <TestObject2>(index, "birthday>2019-03-01"); var queryResult4 = await elasticBand.Query <TestObject2>(index, "birthday<2019-03-01T09:05:00"); var queryResult5 = await elasticBand.Query <TestObject2>(index, "*rew"); // Delete... await elasticBand.Delete(index, "id2"); Assert.That(queryResult1.Data.Count, Is.EqualTo(1)); Assert.That(queryResult2.Data.Count, Is.EqualTo(1)); Assert.That(queryResult3.Data.Count, Is.EqualTo(1)); Assert.That(queryResult4.Data.Count, Is.EqualTo(2)); Assert.That(queryResult5.Data.Count, Is.EqualTo(2)); }
public void Delete_should_return_an_ElasticBandResponse_given_the_expected_http_response_string() { var content = "{\"_index\":\"test_index\",\"_type\":\"_doc\",\"_id\":\"ae3f81be-fa22-4777-984d-17855357b156\",\"_version\":3,\"result\":\"deleted\",\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},\"_seq_no\":3,\"_primary_term\":1}"; ConfigureClientFactory(content); var sut = new ElasticBand(_fakeHttpClientFactory, new ElasticQueryBuilder()); var result = sut.Delete("testIndex", "testId").Result; Assert.That(result.Ok, Is.EqualTo(true)); Assert.That(result.Result, Is.EqualTo("deleted")); Assert.That(result.Id, Is.EqualTo("testId")); }
public void Query_should_return_an_ElasticBandResponse_given_the_expected_http_response_string() { var content = "{\"took\":1,\"timed_out\":false,\"_shards\":{\"total\":1,\"successful\":1,\"skipped\":0,\"failed\":0},\"hits\":{\"total\":{\"value\":2,\"relation\":\"eq\"},\"max_score\":0.13353139,\"hits\":[{\"_index\":\"test_index\",\"_type\":\"_doc\",\"_id\":\"196f168d-37ef-4160-982d-afe15d7670eb\",\"_version\":1,\"_score\":0.13353139,\"_source\":{\r\n \"name\": \"bbb\",\r\n \"ebDataType\": \"TestObject\",\r\n \"created\": \"2020-01-19T20:33:12.5661565+00:00\"\r\n}},{\"_index\":\"test_index\",\"_type\":\"_doc\",\"_id\":\"3dc0de1e-a187-488d-84b4-80f2323cfed9\",\"_version\":2,\"_score\":0.13353139,\"_source\":{\r\n \"name\": \"ccc\",\r\n \"ebDataType\": \"TestObject\",\r\n \"created\": \"2020-01-19T20:33:12.065047+00:00\"\r\n}}]}}"; ConfigureClientFactory(content); var sut = new ElasticBand(_fakeHttpClientFactory, new ElasticQueryBuilder()); var testObject = new TestObject { Name = "aaa", ebDataType = "TestObject" }; var result = sut.Query <TestObject>("testIndex", "").Result; Assert.That(result.Ok, Is.EqualTo(true)); Assert.That(result.Result, Is.EqualTo("found")); Assert.That(result.Data.FirstOrDefault().ebDataType, Is.EqualTo(testObject.ebDataType)); }
public void Index_should_return_an_ElasticBandResponse_given_the_expected_http_response_string() { var content = "{\"_index\":\"test_index\",\"_type\":\"_doc\",\"_id\":\"5c506881-37bb-4525-ba8c-09980e74a056\",\"_version\":1,\"result\":\"created\",\"_shards\":{\"total\":2,\"successful\":1,\"failed\":0},\"_seq_no\":0,\"_primary_term\":1}"; ConfigureClientFactory(content); var sut = new ElasticBand(_fakeHttpClientFactory, new ElasticQueryBuilder()); var testObject = new TestObject { Name = "aaa", ebDataType = "TestObject" }; var result = sut.Index("testIndex", testObject, "testId").Result; Assert.That(result.Ok, Is.EqualTo(true)); Assert.That(result.Result, Is.EqualTo("created")); Assert.That(result.Id, Is.EqualTo("testId")); }
public void GetDocumentById_should_return_an_ElasticBandResponse_given_the_expected_http_response_string() { var content = "{\"_index\":\"test_index\",\"_type\":\"_doc\",\"_id\":\"cb2b1bed-fea4-41b4-b526-8eb607205583\",\"_version\":1,\"_seq_no\":0,\"_primary_term\":1,\"found\":true,\"_source\":{\r\n \"name\": \"aaa\",\r\n \"ebDataType\": \"TestObject\",\r\n \"created\": \"2020-01-19T20:09:48.1433274+00:00\"\r\n}}"; ConfigureClientFactory(content); var sut = new ElasticBand(_fakeHttpClientFactory, new ElasticQueryBuilder()); var testObject = new TestObject { Name = "aaa", ebDataType = "TestObject" }; var result = sut.GetDocumentByID <TestObject>("testIndex", "testId").Result; Assert.That(result.Ok, Is.EqualTo(true)); Assert.That(result.Result, Is.EqualTo("found")); Assert.That(result.Id, Is.EqualTo("testId")); Assert.That(result.Data.ebDataType, Is.EqualTo(testObject.ebDataType)); }