|
16 | 16 |
|
17 | 17 | import static org.hamcrest.Matchers.contains; |
18 | 18 | import static org.hamcrest.Matchers.containsInAnyOrder; |
| 19 | +import static org.hamcrest.Matchers.containsString; |
19 | 20 | import static org.hamcrest.Matchers.empty; |
20 | 21 | import static org.hamcrest.Matchers.equalTo; |
21 | 22 | import static org.hamcrest.Matchers.is; |
@@ -740,6 +741,73 @@ public void testIgnoreDynamicBeyondLimit() throws Exception { |
740 | 741 | assertThat(ignored.stream().filter(i -> i.startsWith("field") == false).toList(), empty()); |
741 | 742 | } |
742 | 743 |
|
| 744 | + @SuppressWarnings("unchecked") |
| 745 | + public void testFailureStoreWithInvalidFieldType() throws Exception { |
| 746 | + String dataStreamName = "logs-app-with-failure-store"; |
| 747 | + createDataStream(client, dataStreamName); |
| 748 | + |
| 749 | + indexDoc(client, dataStreamName, """ |
| 750 | + { |
| 751 | + "@timestamp": "2023-11-30T12:00:00Z", |
| 752 | + "message": "This is a valid message" |
| 753 | + } |
| 754 | + """); |
| 755 | + |
| 756 | + // invalid document (message as an object instead of string) |
| 757 | + indexDoc(client, dataStreamName, """ |
| 758 | + { |
| 759 | + "@timestamp": "2023-11-30T12:01:00Z", |
| 760 | + "message": { |
| 761 | + "nested": "This should fail because message should be a string" |
| 762 | + } |
| 763 | + } |
| 764 | + """); |
| 765 | + |
| 766 | + refreshAllIndices(); |
| 767 | + |
| 768 | + Request dsInfoRequest = new Request("GET", "/_data_stream/" + dataStreamName); |
| 769 | + Map<String, Object> dsInfoResponse = entityAsMap(client.performRequest(dsInfoRequest)); |
| 770 | + List<Map<String, Object>> dataStreams = (List<Map<String, Object>>) dsInfoResponse.get("data_streams"); |
| 771 | + Map<String, Object> dataStream = dataStreams.getFirst(); |
| 772 | + Map<String, Object> failureStoreInfo = (Map<String, Object>) dataStream.get("failure_store"); |
| 773 | + assertNotNull(failureStoreInfo); |
| 774 | + assertThat(failureStoreInfo.get("enabled"), is(true)); |
| 775 | + List<Map<String, Object>> failureIndices = (List<Map<String, Object>>) failureStoreInfo.get("indices"); |
| 776 | + |
| 777 | + assertThat(failureIndices, not(empty())); |
| 778 | + String failureIndex = (String) failureIndices.getFirst().get("index_name"); |
| 779 | + assertThat(failureIndex, matchesRegex("\\.fs-" + dataStreamName + "-.*")); |
| 780 | + |
| 781 | + // query the failure store index |
| 782 | + Request failureStoreQuery = new Request("GET", "/" + failureIndex + "/_search"); |
| 783 | + failureStoreQuery.setJsonEntity(""" |
| 784 | + { |
| 785 | + "query": { |
| 786 | + "match_all": {} |
| 787 | + } |
| 788 | + } |
| 789 | + """); |
| 790 | + Map<String, Object> failureStoreResponse = entityAsMap(client.performRequest(failureStoreQuery)); |
| 791 | + Map<String, Object> hits = (Map<String, Object>) failureStoreResponse.get("hits"); |
| 792 | + List<Map<String, Object>> hitsList = (List<Map<String, Object>>) hits.get("hits"); |
| 793 | + |
| 794 | + // Verify the failed document is in the failure store |
| 795 | + assertThat(hitsList.size(), is(1)); |
| 796 | + Map<String, Object> failedDoc = (Map<String, Object>) hitsList.getFirst().get("_source"); |
| 797 | + Map<String, Object> document = (Map<String, Object>) failedDoc.get("document"); |
| 798 | + assertNotNull(document); |
| 799 | + Map<String, Object> source = (Map<String, Object>) document.get("source"); |
| 800 | + assertNotNull(source); |
| 801 | + Map<String, Object> message = (Map<String, Object>) source.get("message"); |
| 802 | + assertNotNull(message); |
| 803 | + assertThat(message.get("nested"), equalTo("This should fail because message should be a string")); |
| 804 | + Map<String, Object> error = (Map<String, Object>) failedDoc.get("error"); |
| 805 | + assertNotNull(error); |
| 806 | + assertEquals("document_parsing_exception", error.get("type")); |
| 807 | + String errorMessage = (String) error.get("message"); |
| 808 | + assertThat(errorMessage, containsString("failed to parse field [message] of type [match_only_text] in document with id")); |
| 809 | + } |
| 810 | + |
743 | 811 | @Override |
744 | 812 | protected String indexTemplateName() { |
745 | 813 | return "logs"; |
|
0 commit comments