use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project hazelcast by hazelcast.
the class ElasticSourcePTest method given_singleHit_when_runProcessor_then_produceSingleHit.
@Test
public void given_singleHit_when_runProcessor_then_produceSingleHit() throws Exception {
SearchHit hit = new SearchHit(0, "id-0", new Text("ignored"), emptyMap(), emptyMap());
hit.sourceRef(new BytesArray(HIT_SOURCE));
when(response.getHits()).thenReturn(new SearchHits(new SearchHit[] { hit }, new TotalHits(1, EQUAL_TO), Float.NaN));
SearchResponse response2 = mock(SearchResponse.class);
when(response2.getHits()).thenReturn(new SearchHits(new SearchHit[] {}, new TotalHits(1, EQUAL_TO), Float.NaN));
when(mockClient.scroll(any(), any())).thenReturn(response2);
TestSupport testSupport = runProcessor();
testSupport.expectOutput(newArrayList(HIT_SOURCE));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project vertigo by KleeGroup.
the class ESFacetedQueryResultBuilder method createHighlight.
private static Map<DtField, String> createHighlight(final SearchHit searchHit, final DtDefinition resultDtDefinition) {
final Map<DtField, String> highlights = new HashMap<>();
final Map<String, HighlightField> highlightsMap = searchHit.getHighlightFields();
for (final Map.Entry<String, HighlightField> entry : highlightsMap.entrySet()) {
final String fieldName = entry.getKey();
if (resultDtDefinition.contains(fieldName)) {
// We only keep highlighs match on result's fields
final DtField dtField = resultDtDefinition.getField(fieldName);
final StringBuilder sb = new StringBuilder();
for (final Text fragment : entry.getValue().getFragments()) {
sb.append("<hlfrag>").append(fragment).append("</hlfrag>");
}
highlights.put(dtField, sb.toString());
}
}
return highlights;
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project stash-codesearch-plugin by palantir.
the class SourceSearch method search.
/**
* Performs a SourceSearch for all lines within contextLines distance of a matching line.
*/
public static SourceSearch search(// source string to search
String source, // ES highlighted fragments
HighlightField highlightField, // number of surrounding lines to show for each match
int contextLines, // number of lines to show for file previews (files with no matches)
int previewLines, int lineLimit) {
// maximum number of lines to display
// Find matching snippets from fragment array
ImmutableSet.Builder<String> matchSetBuilder = new ImmutableSet.Builder<String>();
if (highlightField != null) {
for (Text fragment : highlightField.getFragments()) {
Matcher m = FRAGMENT_REGEX.matcher(fragment.toString());
while (m.find()) {
matchSetBuilder.add(m.group(1));
}
}
}
ImmutableSet<String> matchSet = matchSetBuilder.build();
// Find matching lines
String[] sourceLines = source.split("\r?\n|\r");
boolean[] includeLine = new boolean[sourceLines.length];
boolean[] ellipsisLine = new boolean[sourceLines.length];
int numMatches = 0;
for (int i = 0; i < sourceLines.length; ++i) {
if (lineMatches(sourceLines[i], matchSet)) {
if (numMatches > 0) {
int ellipsisIndex = i - contextLines - 1;
if (ellipsisIndex >= 0 && !includeLine[ellipsisIndex]) {
includeLine[ellipsisIndex] = ellipsisLine[ellipsisIndex] = true;
++numMatches;
}
}
for (int j = i - contextLines; j <= i + contextLines; ++j) {
if (j >= 0 && j < sourceLines.length && !includeLine[j]) {
includeLine[j] = true;
++numMatches;
}
}
}
}
// If no matches found, initialize preview
int linesToShow = Math.min(numMatches, lineLimit);
boolean preview = numMatches == 0;
if (preview) {
numMatches = sourceLines.length;
linesToShow = Math.min(numMatches, previewLines);
if (numMatches > 0) {
Arrays.fill(includeLine, 0, linesToShow, true);
}
}
// Build line and linenum arrays
int excess = numMatches - linesToShow;
String[] matchingLines = new String[linesToShow];
int[] lineNums = new int[linesToShow];
for (int i = 0, curCount = 0; i < sourceLines.length && curCount < linesToShow; ++i) {
if (includeLine[i]) {
matchingLines[curCount] = ellipsisLine[i] ? "" : sourceLines[i];
lineNums[curCount] = ellipsisLine[i] ? -1 : i + 1;
++curCount;
}
}
return new SourceSearch(preview, matchingLines, lineNums, excess);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project graylog2-server by Graylog2.
the class ElasticsearchFilterDeprecationWarningsInterceptorTest method testInterceptorMultipleHeaderFilteredWarning2.
@Test
public void testInterceptorMultipleHeaderFilteredWarning2() throws IOException, HttpException {
ElasticsearchFilterDeprecationWarningsInterceptor interceptor = new ElasticsearchFilterDeprecationWarningsInterceptor();
HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 0, 0), 0, null));
response.addHeader("Test", "This header should not trigger the interceptor.");
response.addHeader("Warning", "This warning should not trigger the interceptor.");
response.addHeader("Warning", "This text contains the trigger: but in a future major version, direct access to system indices and their aliases will not be allowed - and should be filtered out");
assertThat(response.getAllHeaders()).as("Number of Headers should be 3 before start.").hasSize(3);
interceptor.process(response, null);
assertThat(response.getAllHeaders()).as("Number of Headers should be 1 less after running the interceptor.").hasSize(2);
}
Aggregations