Search in sources :

Example 41 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.

the class TermSuggestionOptionTests method createTestItem.

public static Option createTestItem() {
    Text text = new Text(randomAsciiOfLengthBetween(5, 15));
    float score = randomFloat();
    int freq = randomInt();
    return new Option(text, freq, score);
}
Also used : Text(org.elasticsearch.common.text.Text) Option(org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option)

Example 42 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.

the class PercolatorHighlightSubFetchPhase method hitsExecute.

@Override
public void hitsExecute(SearchContext context, SearchHit[] hits) {
    if (hitsExecutionNeeded(context) == false) {
        return;
    }
    PercolateQuery percolateQuery = locatePercolatorQuery(context.query());
    if (percolateQuery == null) {
        // shouldn't happen as we checked for the existence of a percolator query in hitsExecutionNeeded(...)
        throw new IllegalStateException("couldn't locate percolator query");
    }
    List<LeafReaderContext> ctxs = context.searcher().getIndexReader().leaves();
    IndexSearcher percolatorIndexSearcher = percolateQuery.getPercolatorIndexSearcher();
    PercolateQuery.QueryStore queryStore = percolateQuery.getQueryStore();
    LeafReaderContext percolatorLeafReaderContext = percolatorIndexSearcher.getIndexReader().leaves().get(0);
    FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
    SubSearchContext subSearchContext = createSubSearchContext(context, percolatorLeafReaderContext, percolateQuery.getDocumentSource());
    for (SearchHit hit : hits) {
        final Query query;
        try {
            LeafReaderContext ctx = ctxs.get(ReaderUtil.subIndex(hit.docId(), ctxs));
            int segmentDocId = hit.docId() - ctx.docBase;
            query = queryStore.getQueries(ctx).apply(segmentDocId);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        if (query != null) {
            subSearchContext.parsedQuery(new ParsedQuery(query));
            hitContext.reset(new SearchHit(0, "unknown", new Text(percolateQuery.getDocumentType()), Collections.emptyMap()), percolatorLeafReaderContext, 0, percolatorIndexSearcher);
            hitContext.cache().clear();
            super.hitExecute(subSearchContext, hitContext);
            hit.getHighlightFields().putAll(hitContext.hit().getHighlightFields());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SearchHit(org.elasticsearch.search.SearchHit) Query(org.apache.lucene.search.Query) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) FunctionScoreQuery(org.elasticsearch.common.lucene.search.function.FunctionScoreQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) Text(org.elasticsearch.common.text.Text) IOException(java.io.IOException) SubSearchContext(org.elasticsearch.search.internal.SubSearchContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) FetchSubPhase(org.elasticsearch.search.fetch.FetchSubPhase)

Example 43 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.

the class HighlightFieldTests method testToXContent.

public void testToXContent() throws IOException {
    HighlightField field = new HighlightField("foo", new Text[] { new Text("bar"), new Text("baz") });
    XContentBuilder builder = JsonXContent.contentBuilder();
    builder.prettyPrint();
    builder.startObject();
    field.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    assertEquals("{\n" + "  \"foo\" : [\n" + "    \"bar\",\n" + "    \"baz\"\n" + "  ]\n" + "}", builder.string());
    field = new HighlightField("foo", null);
    builder = JsonXContent.contentBuilder();
    builder.prettyPrint();
    builder.startObject();
    field.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    assertEquals("{\n" + "  \"foo\" : null\n" + "}", builder.string());
}
Also used : Text(org.elasticsearch.common.text.Text) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 44 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.

the class HighlightFieldTests method createTestItem.

public static HighlightField createTestItem() {
    String name = frequently() ? randomAsciiOfLengthBetween(5, 20) : randomRealisticUnicodeOfCodepointLengthBetween(5, 20);
    Text[] fragments = null;
    if (frequently()) {
        int size = randomIntBetween(0, 5);
        fragments = new Text[size];
        for (int i = 0; i < size; i++) {
            fragments[i] = new Text(frequently() ? randomAsciiOfLengthBetween(10, 30) : randomRealisticUnicodeOfCodepointLengthBetween(10, 30));
        }
    }
    return new HighlightField(name, fragments);
}
Also used : Text(org.elasticsearch.common.text.Text)

Example 45 with Text

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.text.Text in project elasticsearch by elastic.

the class CustomHighlighter method highlight.

@Override
public HighlightField highlight(HighlighterContext highlighterContext) {
    SearchContextHighlight.Field field = highlighterContext.field;
    CacheEntry cacheEntry = (CacheEntry) highlighterContext.hitContext.cache().get("test-custom");
    final int docId = highlighterContext.hitContext.readerContext().docBase + highlighterContext.hitContext.docId();
    if (cacheEntry == null) {
        cacheEntry = new CacheEntry();
        highlighterContext.hitContext.cache().put("test-custom", cacheEntry);
        cacheEntry.docId = docId;
        cacheEntry.position = 1;
    } else {
        if (cacheEntry.docId == docId) {
            cacheEntry.position++;
        } else {
            cacheEntry.docId = docId;
            cacheEntry.position = 1;
        }
    }
    List<Text> responses = new ArrayList<>();
    responses.add(new Text(String.format(Locale.ENGLISH, "standard response for %s at position %s", field.field(), cacheEntry.position)));
    if (field.fieldOptions().options() != null) {
        for (Map.Entry<String, Object> entry : field.fieldOptions().options().entrySet()) {
            responses.add(new Text("field:" + entry.getKey() + ":" + entry.getValue()));
        }
    }
    return new HighlightField(highlighterContext.fieldName, responses.toArray(new Text[] {}));
}
Also used : ArrayList(java.util.ArrayList) SearchContextHighlight(org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight) Text(org.elasticsearch.common.text.Text) HighlightField(org.elasticsearch.search.fetch.subphase.highlight.HighlightField) Map(java.util.Map)

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