org.elasticsearch.search.suggest.Suggest Java Examples

The following examples show how to use org.elasticsearch.search.suggest.Suggest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static SearchResponse createEmptySearchResponse() throws IOException {
    return new SearchResponse(
        new InternalSearchResponse(
            new SearchHits(new SearchHit[0], new TotalHits(0, TotalHits.Relation.EQUAL_TO), 1.0f),
            new InternalAggregations(Collections.emptyList()),
            new Suggest(Collections.emptyList()),
            new SearchProfileShardResults(Collections.emptyMap()),
            false,
            false,
            1
        ),
        "",
        5,
        5,
        0,
        100,
        ShardSearchFailure.EMPTY_ARRAY,
        SearchResponse.Clusters.EMPTY
    );
}
 
Example #2
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static SearchResponse createSearchResponse(ToXContentObject o) throws IOException {
    XContentBuilder content = o.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);

    SearchHit[] hits = new SearchHit[1];
    hits[0] = new SearchHit(0).sourceRef(BytesReference.bytes(content));

    return new SearchResponse(
        new InternalSearchResponse(
            new SearchHits(hits, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f),
            new InternalAggregations(Collections.emptyList()),
            new Suggest(Collections.emptyList()),
            new SearchProfileShardResults(Collections.emptyMap()),
            false,
            false,
            1
        ),
        "",
        5,
        5,
        0,
        100,
        ShardSearchFailure.EMPTY_ARRAY,
        SearchResponse.Clusters.EMPTY
    );
}
 
Example #3
Source File: InternalSearchResponse.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    hits = readSearchHits(in);
    if (in.readBoolean()) {
        aggregations = InternalAggregations.readAggregations(in);
    }
    if (in.readBoolean()) {
        suggest = Suggest.readSuggest(Suggest.Fields.SUGGEST, in);
    }
    timedOut = in.readBoolean();

    terminatedEarly = in.readOptionalBoolean();

    if (in.getVersion().onOrAfter(Version.V_2_2_0) && in.readBoolean()) {
        profileResults = new InternalProfileShardResults(in);
    } else {
        profileResults = null;
    }
}
 
Example #4
Source File: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
private static SuggestionEntry mapToSuggestionEntry(Suggest.Suggestion.Entry<?> esSuggestionEntry) {
    final SuggestionEntry suggestionEntry = new SuggestionEntry()
            .setLength(esSuggestionEntry.getLength())
            .setOffset(esSuggestionEntry.getOffset())
            .setOptions(
                    esSuggestionEntry.getOptions()
                            .stream()
                            .map(e -> mapToSuggestionEntryOption(e))
                            .collect(Collectors.toList())
            );

    if (esSuggestionEntry.getText() != null) {
        suggestionEntry.setText(esSuggestionEntry.getText().string());
    }


    return suggestionEntry;
}
 
Example #5
Source File: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
private static Suggestion mapToSuggestion(Suggest.Suggestion<?> esSuggestion) {
    final Suggestion suggestion = new Suggestion()
            .setName(esSuggestion.getName())
            .setSize(esSuggestion.getEntries().size())
            .setEntries(
                    esSuggestion.getEntries().stream()
                            .map(e -> mapToSuggestionEntry(e))
                            .collect(Collectors.toList())
            );

    if (esSuggestion instanceof CompletionSuggestion) {
        suggestion.setSuggestionType(SuggestionType.COMPLETION);
    } else if (esSuggestion instanceof PhraseSuggestion) {
        suggestion.setSuggestionType(SuggestionType.PHRASE);
    } else if (esSuggestion instanceof TermSuggestion) {
        suggestion.setSuggestionType(SuggestionType.TERM);
    } else {
        throw new RuntimeException("SuggestionType " + esSuggestion.getClass().getCanonicalName() + " unknown");
    }

    return suggestion;
}
 
Example #6
Source File: SuggestRequestBuilder.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
@Override
protected XContentBuilder toXContent(SuggestRequest request, SuggestResponse response, XContentBuilder builder) throws IOException {
    builder.startObject();
    buildBroadcastShardsHeader(builder, response);
    Suggest suggest = response.getSuggest();
    if (suggest != null) {
        suggest.toXContent(builder, ToXContent.EMPTY_PARAMS);
    }
    builder.endObject();
    return builder;
}
 
Example #7
Source File: HttpSearchAction.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
private InternalSearchResponse parseInternalSearchResponse(Map<String,?> map) {
    InternalSearchHits internalSearchHits = parseInternalSearchHits(map);
    InternalAggregations internalAggregations = parseInternalAggregations(map);
    Suggest suggest = parseSuggest(map);
    InternalProfileShardResults internalProfileShardResults = null;
    Boolean timeout = false;
    Boolean terminatedEarly = false;
    return new InternalSearchResponse(internalSearchHits,
            internalAggregations,
            suggest,
            internalProfileShardResults,
            timeout,
            terminatedEarly);
}
 
Example #8
Source File: ElasticSearchService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public SearchResponse search(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInfromation) {
    return new SearchResponse(
            new InternalSearchResponse(new InternalSearchHits(new InternalSearchHit[0], 0, 0.0f), new InternalFacets(Collections.EMPTY_LIST), new InternalAggregations(Collections.EMPTY_LIST), new Suggest(), false, false),
            "no-op",
            1,
            1,
            1,
            new ShardSearchFailure[0]
    );
}
 
Example #9
Source File: ElasticSearchService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public SearchResponse search(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInfromation) {
    return new SearchResponse(
            new InternalSearchResponse(new InternalSearchHits(new InternalSearchHit[0], 0, 0.0f), new InternalFacets(Collections.EMPTY_LIST), new InternalAggregations(Collections.EMPTY_LIST), new Suggest(), false, false),
            "no-op",
            1,
            1,
            1,
            new ShardSearchFailure[0]
    );
}
 
Example #10
Source File: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 5 votes vote down vote up
private static SuggestionEntryOption mapToSuggestionEntryOption(Suggest.Suggestion.Entry.Option esSuggestionEntryOption) {
    final SuggestionEntryOption suggestionEntryOption = new SuggestionEntryOption()
            .setScore(esSuggestionEntryOption.getScore());

    if (esSuggestionEntryOption.getText() != null) {
        suggestionEntryOption.setText(esSuggestionEntryOption.getText().string());
    }

    if (esSuggestionEntryOption.getHighlighted() != null) {
        suggestionEntryOption.setHighlight(esSuggestionEntryOption.getHighlighted().string());
    }

    return suggestionEntryOption;
}
 
Example #11
Source File: ElasticSearchServiceMapper.java    From vertx-elasticsearch-service with Apache License 2.0 5 votes vote down vote up
public static com.hubrick.vertx.elasticsearch.model.SearchResponse mapToSearchResponse(SearchResponse esSearchResponse) {
    final com.hubrick.vertx.elasticsearch.model.SearchResponse searchResponse = new com.hubrick.vertx.elasticsearch.model.SearchResponse();

    searchResponse.setRawResponse(readResponse(esSearchResponse));
    searchResponse.setTook(esSearchResponse.getTook().getMillis());
    searchResponse.setTimedOut(esSearchResponse.isTimedOut());
    searchResponse.setShards(mapToShards(esSearchResponse));
    searchResponse.setHits(mapToHits(esSearchResponse.getHits()));
    searchResponse.setScrollId(esSearchResponse.getScrollId());

    if (esSearchResponse.getSuggest() != null) {
        for (Suggest.Suggestion<?> entries : esSearchResponse.getSuggest()) {
            searchResponse.addSuggestion(entries.getName(), mapToSuggestion(entries));
        }
    }

    if (esSearchResponse.getAggregations() != null) {
        searchResponse.setAggregations(
                esSearchResponse.getAggregations().asMap()
                        .entrySet()
                        .stream()
                        .collect(Collectors.toMap(e -> e.getKey(), e -> readResponse((InternalAggregation) e.getValue())))
        );
    }

    return searchResponse;
}
 
Example #12
Source File: TransportSuggestAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected ShardSuggestResponse shardOperation(ShardSuggestRequest request) {
    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());
    ShardSuggestMetric suggestMetric = indexShard.getSuggestMetric();
    suggestMetric.preSuggest();
    long startTime = System.nanoTime();
    XContentParser parser = null;
    try (Engine.Searcher searcher = indexShard.acquireSearcher("suggest")) {
        BytesReference suggest = request.suggest();
        if (suggest != null && suggest.length() > 0) {
            parser = XContentFactory.xContent(suggest).createParser(suggest);
            if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
                throw new IllegalArgumentException("suggest content missing");
            }
            final SuggestionSearchContext context = suggestPhase.parseElement().parseInternal(parser, indexService.mapperService(),
                    indexService.queryParserService(), request.shardId().getIndex(), request.shardId().id(), request);
            final Suggest result = suggestPhase.execute(context, searcher.searcher());
            return new ShardSuggestResponse(request.shardId(), result);
        }
        return new ShardSuggestResponse(request.shardId(), new Suggest());
    } catch (Throwable ex) {
        throw new ElasticsearchException("failed to execute suggest", ex);
    } finally {
        if (parser != null) {
            parser.close();
        }
        suggestMetric.postSuggest(System.nanoTime() - startTime);
    }
}
 
Example #13
Source File: TransportSuggestAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected SuggestResponse newResponse(SuggestRequest request, AtomicReferenceArray shardsResponses, ClusterState clusterState) {
    int successfulShards = 0;
    int failedShards = 0;

    final Map<String, List<Suggest.Suggestion>> groupedSuggestions = new HashMap<>();

    List<ShardOperationFailedException> shardFailures = null;
    for (int i = 0; i < shardsResponses.length(); i++) {
        Object shardResponse = shardsResponses.get(i);
        if (shardResponse == null) {
            // simply ignore non active shards
        } else if (shardResponse instanceof BroadcastShardOperationFailedException) {
            failedShards++;
            if (shardFailures == null) {
                shardFailures = new ArrayList<>();
            }
            shardFailures.add(new DefaultShardOperationFailedException((BroadcastShardOperationFailedException) shardResponse));
        } else {
            Suggest suggest = ((ShardSuggestResponse) shardResponse).getSuggest();
            Suggest.group(groupedSuggestions, suggest);
            successfulShards++;
        }
    }

    return new SuggestResponse(new Suggest(Suggest.reduce(groupedSuggestions)), shardsResponses.length(), successfulShards, failedShards, shardFailures);
}
 
Example #14
Source File: InternalSearchResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public InternalSearchResponse(InternalSearchHits hits, InternalAggregations aggregations, Suggest suggest,
                              InternalProfileShardResults profileResults, boolean timedOut, Boolean terminatedEarly) {
    this.hits = hits;
    this.aggregations = aggregations;
    this.suggest = suggest;
    this.profileResults = profileResults;
    this.timedOut = timedOut;
    this.terminatedEarly = terminatedEarly;
}
 
Example #15
Source File: TestQueryUtil.java    From blue-marlin with Apache License 2.0 5 votes vote down vote up
@Test
public void extractAggregationValue() {
    int docId = 0, docId1 = 1, docId2 = 2, docId3 = 3, docId4 = 400, docId5 = 5000;
    SearchHit[] hit = new SearchHit[6];
    hit[0] = new SearchHit(docId);
    hit[1] = new SearchHit(docId1);
    hit[2] = new SearchHit(docId2);
    hit[3] = new SearchHit(docId3);
    hit[4] = new SearchHit(docId4);
    hit[5] = new SearchHit(docId5);
    SearchHits hits = new SearchHits(hit, 30000, 450);

    Aggregations aggregations = new Aggregations(new ArrayList());

    List<Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>> suggestions = new ArrayList();
    suggestions.add(new Suggest.Suggestion("sug1", 1));
    suggestions.add(new Suggest.Suggestion("sug2", 2));
    suggestions.add(new Suggest.Suggestion("sug3", 3));
    suggestions.add(new Suggest.Suggestion("sug4", 4));
    suggestions.add(new Suggest.Suggestion("sug5", 50));

    Suggest suggest = new Suggest(suggestions);

    SearchProfileShardResults profileResults = new SearchProfileShardResults(Collections.emptyMap());

    SearchResponseSections internalResponse = new SearchResponseSections(hits, aggregations, suggest, false, false, profileResults, 0);

    ShardSearchFailure[] shardFailures = new ShardSearchFailure[0];

    SearchResponse.Clusters clusters = SearchResponse.Clusters.EMPTY;

    SearchResponse sRes = new SearchResponse(internalResponse, "id1", 1, 1, 1, 1, shardFailures, clusters);

    JSONObject res = QueryUtil.extractAggregationValue(sRes);

    assertNotNull(res.get("suggest"));
    assertNotNull(res.get("hits"));
}
 
Example #16
Source File: SuggestResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
SuggestResponse(Suggest suggest, int totalShards, int successfulShards, int failedShards, List<ShardOperationFailedException> shardFailures) {
    super(totalShards, successfulShards, failedShards, shardFailures);
    this.suggest = suggest;
}
 
Example #17
Source File: SuggestResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * The Suggestions of the phrase.
 */
public Suggest getSuggest() {
    return suggest;
}
 
Example #18
Source File: SuggestAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public SuggestResponse newResponse() {
    return new SuggestResponse(new Suggest());
}
 
Example #19
Source File: ShardSuggestResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
ShardSuggestResponse() {
    this.suggest = new Suggest();
}
 
Example #20
Source File: ShardSuggestResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
ShardSuggestResponse(ShardId shardId, Suggest suggest) {
    super(shardId);
    this.suggest = suggest;
}
 
Example #21
Source File: ShardSuggestResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Suggest getSuggest() {
    return this.suggest;
}
 
Example #22
Source File: SuggestResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
SuggestResponse(Suggest suggest) {
    this.suggest = suggest;
}
 
Example #23
Source File: SearchResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Suggest getSuggest() {
    return internalResponse.suggest();
}
 
Example #24
Source File: CoordinateSearchResponse.java    From siren-join with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public Suggest getSuggest() {
  return searchResponse.getSuggest();
}
 
Example #25
Source File: InternalSearchResponse.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Suggest suggest() {
    return suggest;
}
 
Example #26
Source File: QuerySearchResult.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Suggest suggest() {
    return suggest;
}
 
Example #27
Source File: HttpSearchAction.java    From elasticsearch-helper with Apache License 2.0 4 votes vote down vote up
private Suggest parseSuggest(Map<String,?> map) {
    return null;
}