use of org.elasticsearch.common.text.Text in project elasticsearch by elastic.
the class CompletionSuggestionOptionTests method createTestItem.
public static Option createTestItem() {
Text text = new Text(randomAsciiOfLengthBetween(5, 15));
int docId = randomInt();
int numberOfContexts = randomIntBetween(0, 3);
Map<String, Set<CharSequence>> contexts = new HashMap<>();
for (int i = 0; i < numberOfContexts; i++) {
int numberOfValues = randomIntBetween(0, 3);
Set<CharSequence> values = new HashSet<>();
for (int v = 0; v < numberOfValues; v++) {
values.add(randomAsciiOfLengthBetween(5, 15));
}
contexts.put(randomAsciiOfLengthBetween(5, 15), values);
}
SearchHit hit = null;
float score = randomFloat();
if (randomBoolean()) {
hit = SearchHitTests.createTestItem(false);
score = hit.getScore();
}
Option option = new CompletionSuggestion.Entry.Option(docId, text, score, contexts);
option.setHit(hit);
return option;
}
use of org.elasticsearch.common.text.Text in project elasticsearch by elastic.
the class TermSuggestionOptionTests method testToXContent.
public void testToXContent() throws IOException {
Option option = new Option(new Text("someText"), 100, 1.3f);
BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean());
assertEquals("{\"text\":\"someText\",\"score\":1.3,\"freq\":100}", xContent.utf8ToString());
}
use of 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.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.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());
}
Aggregations