Search in sources :

Example 36 with Text

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;
}
Also used : CompletionSuggestion(org.elasticsearch.search.suggest.completion.CompletionSuggestion) Set(java.util.Set) HashSet(java.util.HashSet) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) Text(org.elasticsearch.common.text.Text) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) HashSet(java.util.HashSet)

Example 37 with Text

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());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Option(org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option) Text(org.elasticsearch.common.text.Text)

Example 38 with Text

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);
}
Also used : Text(org.elasticsearch.common.text.Text) Option(org.elasticsearch.search.suggest.term.TermSuggestion.Entry.Option)

Example 39 with Text

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());
        }
    }
}
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 40 with Text

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());
}
Also used : Text(org.elasticsearch.common.text.Text) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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)5 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 Entry (org.elasticsearch.search.suggest.Suggest.Suggestion.Entry)4