use of org.elasticsearch.search.suggest.phrase.PhraseSuggestion in project elasticsearch by elastic.
the class SuggestTests method testFilter.
public void testFilter() throws Exception {
List<Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>>> suggestions;
CompletionSuggestion completionSuggestion = new CompletionSuggestion(randomAsciiOfLength(10), 2);
PhraseSuggestion phraseSuggestion = new PhraseSuggestion(randomAsciiOfLength(10), 2);
TermSuggestion termSuggestion = new TermSuggestion(randomAsciiOfLength(10), 2, SortBy.SCORE);
suggestions = Arrays.asList(completionSuggestion, phraseSuggestion, termSuggestion);
Suggest suggest = new Suggest(suggestions);
List<PhraseSuggestion> phraseSuggestions = suggest.filter(PhraseSuggestion.class);
assertThat(phraseSuggestions.size(), equalTo(1));
assertThat(phraseSuggestions.get(0), equalTo(phraseSuggestion));
List<TermSuggestion> termSuggestions = suggest.filter(TermSuggestion.class);
assertThat(termSuggestions.size(), equalTo(1));
assertThat(termSuggestions.get(0), equalTo(termSuggestion));
List<CompletionSuggestion> completionSuggestions = suggest.filter(CompletionSuggestion.class);
assertThat(completionSuggestions.size(), equalTo(1));
assertThat(completionSuggestions.get(0), equalTo(completionSuggestion));
}
use of org.elasticsearch.search.suggest.phrase.PhraseSuggestion in project elasticsearch by elastic.
the class SuggestionTests method createTestItem.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static Suggestion<? extends Entry<? extends Option>> createTestItem(Class<? extends Suggestion> type) {
String name = randomAsciiOfLengthBetween(5, 10);
// note: size will not be rendered via "toXContent", only passed on internally on transport layer
int size = randomInt();
Supplier<Entry> entrySupplier = null;
Suggestion suggestion = null;
if (type == TermSuggestion.class) {
suggestion = new TermSuggestion(name, size, randomFrom(SortBy.values()));
entrySupplier = () -> SuggestionEntryTests.createTestItem(TermSuggestion.Entry.class);
} else if (type == PhraseSuggestion.class) {
suggestion = new PhraseSuggestion(name, size);
entrySupplier = () -> SuggestionEntryTests.createTestItem(PhraseSuggestion.Entry.class);
} else if (type == CompletionSuggestion.class) {
suggestion = new CompletionSuggestion(name, size);
entrySupplier = () -> SuggestionEntryTests.createTestItem(CompletionSuggestion.Entry.class);
} else {
throw new UnsupportedOperationException("type not supported [" + type + "]");
}
int numEntries;
if (frequently()) {
if (type == CompletionSuggestion.class) {
// CompletionSuggestion can have max. one entry
numEntries = 1;
} else {
numEntries = randomIntBetween(1, 5);
}
} else {
// also occasionally test zero entries
numEntries = 0;
}
for (int i = 0; i < numEntries; i++) {
suggestion.addTerm(entrySupplier.get());
}
return suggestion;
}
use of org.elasticsearch.search.suggest.phrase.PhraseSuggestion in project elasticsearch by elastic.
the class Suggest method readFrom.
@Override
public void readFrom(StreamInput in) throws IOException {
final int size = in.readVInt();
suggestions = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
// TODO: remove these complicated generics
Suggestion<? extends Entry<? extends Option>> suggestion;
final int type = in.readVInt();
switch(type) {
case TermSuggestion.TYPE:
suggestion = new TermSuggestion();
break;
case CompletionSuggestion.TYPE:
suggestion = new CompletionSuggestion();
break;
case // CompletionSuggestion.TYPE
2:
throw new IllegalArgumentException("Completion suggester 2.x is not supported anymore");
case PhraseSuggestion.TYPE:
suggestion = new PhraseSuggestion();
break;
default:
suggestion = new Suggestion();
break;
}
suggestion.readFrom(in);
suggestions.add(suggestion);
}
hasScoreDocs = filter(CompletionSuggestion.class).stream().anyMatch(CompletionSuggestion::hasScoreDocs);
}
Aggregations