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);
}
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());
}
}
}
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());
}
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);
}
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[] {}));
}
Aggregations