org.graylog2.indexer.results.SearchResult Maven / Gradle / Ivy
/**
* 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 .
*/
package org.graylog2.indexer.results;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Sets;
import org.graylog2.indexer.ranges.IndexRange;
import org.graylog2.plugin.Message;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class SearchResult extends IndexQueryResult {
private final long totalResults;
private final List results;
private final Set fields;
private final Set usedIndices;
public SearchResult(List hits, long totalResults, Set usedIndices, String originalQuery, String builtQuery, long tookMs) {
super(originalQuery, builtQuery, tookMs);
this.results = hits;
this.fields = extractFields(hits);
this.totalResults = totalResults;
this.usedIndices = usedIndices;
}
private SearchResult(String query, String originalQuery) {
super(query, originalQuery, 0);
this.results = Collections.emptyList();
this.fields = Collections.emptySet();
this.usedIndices = Collections.emptySet();
this.totalResults = 0;
}
public long getTotalResults() {
return totalResults;
}
public List getResults() {
return results;
}
public Set getFields() {
return fields;
}
@VisibleForTesting
Set extractFields(List hits) {
Set filteredFields = Sets.newHashSet();
hits.forEach(hit -> {
final Message message = hit.getMessage();
for (String field : message.getFieldNames()) {
if (!Message.FILTERED_FIELDS.contains(field)) {
filteredFields.add(field);
}
}
});
return filteredFields;
}
public Set getUsedIndices() {
return usedIndices;
}
public static SearchResult empty(String query, String originalQuery) {
return new SearchResult(query, originalQuery);
}
}