use of org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option in project OpenSearch by opensearch-project.
the class Suggest method reduce.
public static List<Suggestion<? extends Entry<? extends Option>>> reduce(Map<String, List<Suggest.Suggestion>> groupedSuggestions) {
List<Suggestion<? extends Entry<? extends Option>>> reduced = new ArrayList<>(groupedSuggestions.size());
for (Map.Entry<String, List<Suggestion>> unmergedResults : groupedSuggestions.entrySet()) {
List<Suggestion> value = unmergedResults.getValue();
Class<? extends Suggestion> suggestionClass = null;
for (Suggestion suggestion : value) {
if (suggestionClass == null) {
suggestionClass = suggestion.getClass();
} else if (suggestionClass != suggestion.getClass()) {
throw new IllegalArgumentException("detected mixed suggestion results, due to querying on old and new completion suggester," + " query on a single completion suggester version");
}
}
Suggestion reduce = value.get(0).reduce(value);
reduce.trim();
reduced.add(reduce);
}
return reduced;
}
use of org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option in project OpenSearch by opensearch-project.
the class SuggestionTests method doTestFromXContent.
@SuppressWarnings({ "rawtypes" })
private void doTestFromXContent(boolean addRandomFields) throws IOException {
ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
for (Class<Suggestion<? extends Entry<? extends Option>>> type : SUGGESTION_TYPES) {
Suggestion suggestion = createTestItem(type);
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toShuffledXContent(suggestion, xContentType, params, humanReadable);
BytesReference mutated;
if (addRandomFields) {
// - "contexts" is an object consisting of key/array pairs, we shouldn't add anything random there
// - there can be inner search hits fields inside this option where we cannot add random stuff
// - the root object should be excluded since it contains the named suggestion arrays
// We also exclude options that contain SearchHits, as all unknown fields
// on a root level of SearchHit are interpreted as meta-fields and will be kept.
Predicate<String> excludeFilter = path -> path.isEmpty() || path.endsWith(CompletionSuggestion.Entry.Option.CONTEXTS.getPreferredName()) || path.endsWith("highlight") || path.contains("fields") || path.contains("_source") || path.contains("inner_hits") || path.contains("options");
mutated = insertRandomFields(xContentType, originalBytes, excludeFilter, random());
} else {
mutated = originalBytes;
}
Suggestion parsed;
try (XContentParser parser = createParser(xContentType.xContent(), mutated)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser);
ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.nextToken(), parser);
parsed = Suggestion.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertEquals(suggestion.getName(), parsed.getName());
assertEquals(suggestion.getEntries().size(), parsed.getEntries().size());
// We don't parse size via xContent, instead we set it to -1 on the client side
assertEquals(-1, parsed.getSize());
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, params, humanReadable), xContentType);
}
}
use of org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option in project OpenSearch by opensearch-project.
the class SuggestionOptionTests method testToXContent.
public void testToXContent() throws IOException {
Option option = new PhraseSuggestion.Entry.Option(new Text("someText"), new Text("somethingHighlighted"), 1.3f, true);
BytesReference xContent = toXContent(option, XContentType.JSON, randomBoolean());
assertEquals(("{" + " \"text\": \"someText\"," + " \"highlighted\": \"somethingHighlighted\"," + " \"score\": 1.3," + " \"collate_match\": true" + "}").replaceAll("\\s+", ""), xContent.utf8ToString());
}
use of org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option in project OpenSearch by opensearch-project.
the class SuggestPhase method execute.
public void execute(SearchContext context) {
final SuggestionSearchContext suggest = context.suggest();
if (suggest == null) {
return;
}
try {
CharsRefBuilder spare = new CharsRefBuilder();
final List<Suggestion<? extends Entry<? extends Option>>> suggestions = new ArrayList<>(suggest.suggestions().size());
for (Map.Entry<String, SuggestionSearchContext.SuggestionContext> entry : suggest.suggestions().entrySet()) {
SuggestionSearchContext.SuggestionContext suggestion = entry.getValue();
Suggester<SuggestionContext> suggester = suggestion.getSuggester();
Suggestion<? extends Entry<? extends Option>> result = suggester.execute(entry.getKey(), suggestion, context.searcher(), spare);
if (result != null) {
assert entry.getKey().equals(result.name);
suggestions.add(result);
}
}
context.queryResult().suggest(new Suggest(suggestions));
} catch (IOException e) {
throw new OpenSearchException("I/O exception during suggest phase", e);
}
}
use of org.opensearch.search.suggest.Suggest.Suggestion.Entry.Option in project OpenSearch by opensearch-project.
the class SuggestTests method testToXContent.
public void testToXContent() throws IOException {
PhraseSuggestion.Entry.Option option = new PhraseSuggestion.Entry.Option(new Text("someText"), new Text("somethingHighlighted"), 1.3f, true);
PhraseSuggestion.Entry entry = new PhraseSuggestion.Entry(new Text("entryText"), 42, 313);
entry.addOption(option);
PhraseSuggestion suggestion = new PhraseSuggestion("suggestionName", 5);
suggestion.addTerm(entry);
Suggest suggest = new Suggest(Collections.singletonList(suggestion));
BytesReference xContent = toXContent(suggest, XContentType.JSON, randomBoolean());
assertEquals(stripWhitespace("{" + " \"suggest\": {" + " \"suggestionName\": [" + " {" + " \"text\": \"entryText\"," + " \"offset\": 42," + " \"length\": 313," + " \"options\": [" + " {" + " \"text\": \"someText\"," + " \"highlighted\": \"somethingHighlighted\"," + " \"score\": 1.3," + " \"collate_match\": true" + " }" + " ]" + " }" + " ]" + " }" + "}"), xContent.utf8ToString());
}
Aggregations