Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using consistent collection of non displayable fields to filter again…
…st. (#3585) Before this change, the "source" field was suppressed from being returned (and therefore) displayed in decorated results. This was caused by some fields included in the `RESERVED_FIELDS` set, which were not added back from the decorator processor before returning the list of fields included in the search result. This change could have extracted the method used in `SearchResult.java` into a commonly used helper method used in both code points. This would have involved a lot of casting/conversion between different (`ResultMessage`/`ResultMessageSummary`) data types for each returned message which is decorated, so the impact of deduplication would be bigger than its benefits. Instead, I cleaned up and (hopefully) made the sets of fields more expressive and easier to use. Fixes #3584 (cherry picked from commit a724a27)
- Loading branch information
1 parent
448d032
commit eb0878b
Showing
4 changed files
with
149 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
graylog2-server/src/test/java/org/graylog2/indexer/results/SearchResultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/** | ||
* This file is part of Graylog. | ||
* | ||
* Graylog is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Graylog is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Graylog. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.graylog2.indexer.results; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableSet; | ||
import org.elasticsearch.search.SearchHits; | ||
import org.graylog2.plugin.Message; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class SearchResultTest { | ||
private SearchResult searchResult; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
final SearchHits searchHits = mock(SearchHits.class); | ||
when(searchHits.iterator()).thenReturn(Collections.emptyIterator()); | ||
this.searchResult = new SearchResult(searchHits, null, null, null, null); | ||
} | ||
|
||
@Test | ||
public void extractFieldsForEmptyResult() throws Exception { | ||
final Set<String> result = searchResult.extractFields(Collections.emptyList()); | ||
|
||
assertThat(result) | ||
.isNotNull() | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void extractFieldsForTwoMessagesContainingDifferentFields() throws Exception { | ||
final ResultMessage r1 = mock(ResultMessage.class); | ||
final Message m1 = mock(Message.class); | ||
when(m1.getFieldNames()).thenReturn(ImmutableSet.of( | ||
"message", | ||
"source", | ||
"timestamp", | ||
"http_response", | ||
"gl2_source_node", | ||
"_index" | ||
)); | ||
when(r1.getMessage()).thenReturn(m1); | ||
|
||
final ResultMessage r2 = mock(ResultMessage.class); | ||
final Message m2 = mock(Message.class); | ||
when(m2.getFieldNames()).thenReturn(ImmutableSet.of( | ||
"message", | ||
"source", | ||
"timestamp", | ||
"took_ms", | ||
"gl2_source_collector" | ||
)); | ||
when(r2.getMessage()).thenReturn(m2); | ||
|
||
final Set<String> result = searchResult.extractFields(ImmutableList.of(r1, r2)); | ||
|
||
assertThat(result) | ||
.isNotNull() | ||
.isNotEmpty() | ||
.hasSize(5) | ||
.containsExactlyInAnyOrder( | ||
"message", | ||
"source", | ||
"timestamp", | ||
"http_response", | ||
"took_ms" | ||
); | ||
} | ||
} |