/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Licensed to Elasticsearch under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ package org.opensearch.search.suggest; import org.opensearch.common.Nullable; import org.opensearch.core.common.ParsingException; import org.opensearch.common.Strings; import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.common.io.stream.Writeable; import org.opensearch.common.lucene.BytesRefs; import org.opensearch.common.xcontent.XContentType; import org.opensearch.core.ParseField; import org.opensearch.core.xcontent.ToXContentObject; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentParser; import org.opensearch.index.query.QueryShardContext; import org.opensearch.search.suggest.SuggestionSearchContext.SuggestionContext; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; /** * Defines how to perform suggesting. This builders allows a number of global options to be specified and * an arbitrary number of {@link SuggestionBuilder} instances. *
* Suggesting works by suggesting terms/phrases that appear in the suggest text that are similar compared
* to the terms in provided text. These suggestions are based on several options described in this class.
*
* @opensearch.internal
*/
public class SuggestBuilder implements Writeable, ToXContentObject {
protected static final ParseField GLOBAL_TEXT_FIELD = new ParseField("text");
private String globalText;
private final Map
* The suggest text gets analyzed by the suggest analyzer or the suggest field search analyzer.
* For each analyzed token, suggested terms are suggested if possible.
*/
public SuggestBuilder setGlobalText(@Nullable String globalText) {
this.globalText = globalText;
return this;
}
/**
* Gets the global suggest text
*/
@Nullable
public String getGlobalText() {
return globalText;
}
/**
* Adds an {@link org.opensearch.search.suggest.SuggestionBuilder} instance under a user defined name.
* The order in which the Suggestions
are added, is the same as in the response.
* @throws IllegalArgumentException if two suggestions added have the same name
*/
public SuggestBuilder addSuggestion(String name, SuggestionBuilder> suggestion) {
Objects.requireNonNull(name, "every suggestion needs a name");
if (suggestions.get(name) == null) {
suggestions.put(name, suggestion);
} else {
throw new IllegalArgumentException("already added another suggestion with name [" + name + "]");
}
return this;
}
/**
* Get all the Suggestions
that were added to the global {@link SuggestBuilder},
* together with their names
*/
public Map