Search in sources :

Example 51 with Text

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));
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) BytesArray(org.elasticsearch.common.bytes.BytesArray) SearchHit(org.elasticsearch.search.SearchHit) TestSupport(com.hazelcast.jet.core.test.TestSupport) Text(org.elasticsearch.common.text.Text) SearchHits(org.elasticsearch.search.SearchHits) SearchResponse(org.elasticsearch.action.search.SearchResponse) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 52 with Text

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;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Text(org.elasticsearch.common.text.Text) MessageText(io.vertigo.core.locale.MessageText) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DtField(io.vertigo.dynamo.domain.metamodel.DtField)

Example 53 with Text

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);
}
Also used : Text(org.elasticsearch.common.text.Text) ImmutableSet(com.google.common.collect.ImmutableSet)

Example 54 with Text

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);
}
Also used : BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse) HttpResponse(org.graylog.shaded.elasticsearch7.org.apache.http.HttpResponse) ProtocolVersion(org.graylog.shaded.elasticsearch7.org.apache.http.ProtocolVersion) BasicStatusLine(org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicStatusLine) Test(org.junit.Test)

Aggregations

Text (org.elasticsearch.common.text.Text)50 SearchHit (org.elasticsearch.search.SearchHit)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)12 Map (java.util.Map)10 SearchHits (org.elasticsearch.search.SearchHits)10 IOException (java.io.IOException)9 BytesReference (org.elasticsearch.common.bytes.BytesReference)9 BytesArray (org.elasticsearch.common.bytes.BytesArray)7 CompletionSuggestion (org.elasticsearch.search.suggest.completion.CompletionSuggestion)7 List (java.util.List)6 Test (org.junit.Test)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 SearchHitField (org.elasticsearch.search.SearchHitField)5 HighlightField (org.elasticsearch.search.fetch.subphase.highlight.HighlightField)5 Option (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 BytesRef (org.apache.lucene.util.BytesRef)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 InternalSearchResponse (org.elasticsearch.search.internal.InternalSearchResponse)4