use of org.elasticsearch.search.suggest.phrase.DirectCandidateGeneratorBuilder in project elasticsearch by elastic.
the class SuggestSearchIT method testPhraseSuggesterCollate.
public void testPhraseSuggesterCollate() throws InterruptedException, ExecutionException, IOException {
CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put(SETTING_NUMBER_OF_SHARDS, // A single shard will help to keep the tests repeatable.
1).put("index.analysis.analyzer.text.tokenizer", "standard").putArray("index.analysis.analyzer.text.filter", "lowercase", "my_shingle").put("index.analysis.filter.my_shingle.type", "shingle").put("index.analysis.filter.my_shingle.output_unigrams", true).put("index.analysis.filter.my_shingle.min_shingle_size", 2).put("index.analysis.filter.my_shingle.max_shingle_size", 3));
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("title").field("type", "text").field("analyzer", "text").endObject().endObject().endObject().endObject();
assertAcked(builder.addMapping("type1", mapping));
ensureGreen();
List<String> titles = new ArrayList<>();
titles.add("United States House of Representatives Elections in Washington 2006");
titles.add("United States House of Representatives Elections in Washington 2005");
titles.add("State");
titles.add("Houses of Parliament");
titles.add("Representative Government");
titles.add("Election");
List<IndexRequestBuilder> builders = new ArrayList<>();
for (String title : titles) {
builders.add(client().prepareIndex("test", "type1").setSource("title", title));
}
indexRandom(true, builders);
// suggest without collate
PhraseSuggestionBuilder suggest = phraseSuggestion("title").addCandidateGenerator(new DirectCandidateGeneratorBuilder("title").suggestMode("always").maxTermFreq(.99f).size(10).maxInspections(200)).confidence(0f).maxErrors(2f).shardSize(30000).size(10);
Suggest searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", suggest);
assertSuggestionSize(searchSuggest, 0, 10, "title");
// suggest with collate
String filterString = XContentFactory.jsonBuilder().startObject().startObject("match_phrase").field("{{field}}", "{{suggestion}}").endObject().endObject().string();
PhraseSuggestionBuilder filteredQuerySuggest = suggest.collateQuery(filterString);
filteredQuerySuggest.collateParams(Collections.singletonMap("field", "title"));
searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", filteredQuerySuggest);
assertSuggestionSize(searchSuggest, 0, 2, "title");
// collate suggest with no result (boundary case)
searchSuggest = searchSuggest("Elections of Representatives Parliament", "title", filteredQuerySuggest);
assertSuggestionSize(searchSuggest, 0, 0, "title");
NumShards numShards = getNumShards("test");
// collate suggest with bad query
String incorrectFilterString = XContentFactory.jsonBuilder().startObject().startObject("test").field("title", "{{suggestion}}").endObject().endObject().string();
PhraseSuggestionBuilder incorrectFilteredSuggest = suggest.collateQuery(incorrectFilterString);
Map<String, SuggestionBuilder<?>> namedSuggestion = new HashMap<>();
namedSuggestion.put("my_title_suggestion", incorrectFilteredSuggest);
try {
searchSuggest("united states house of representatives elections in washington 2006", numShards.numPrimaries, namedSuggestion);
fail("Post query error has been swallowed");
} catch (ElasticsearchException e) {
// expected
}
// suggest with collation
String filterStringAsFilter = XContentFactory.jsonBuilder().startObject().startObject("match_phrase").field("title", "{{suggestion}}").endObject().endObject().string();
PhraseSuggestionBuilder filteredFilterSuggest = suggest.collateQuery(filterStringAsFilter);
searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", filteredFilterSuggest);
assertSuggestionSize(searchSuggest, 0, 2, "title");
// collate suggest with bad query
String filterStr = XContentFactory.jsonBuilder().startObject().startObject("pprefix").field("title", "{{suggestion}}").endObject().endObject().string();
PhraseSuggestionBuilder in = suggest.collateQuery(filterStr);
try {
searchSuggest("united states house of representatives elections in washington 2006", numShards.numPrimaries, namedSuggestion);
fail("Post filter error has been swallowed");
} catch (ElasticsearchException e) {
//expected
}
// collate script failure due to no additional params
String collateWithParams = XContentFactory.jsonBuilder().startObject().startObject("{{query_type}}").field("{{query_field}}", "{{suggestion}}").endObject().endObject().string();
PhraseSuggestionBuilder phraseSuggestWithNoParams = suggest.collateQuery(collateWithParams);
try {
searchSuggest("united states house of representatives elections in washington 2006", numShards.numPrimaries, namedSuggestion);
fail("Malformed query (lack of additional params) should fail");
} catch (ElasticsearchException e) {
// expected
}
// collate script with additional params
Map<String, Object> params = new HashMap<>();
params.put("query_type", "match_phrase");
params.put("query_field", "title");
PhraseSuggestionBuilder phraseSuggestWithParams = suggest.collateQuery(collateWithParams).collateParams(params);
searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", phraseSuggestWithParams);
assertSuggestionSize(searchSuggest, 0, 2, "title");
// collate query request with prune set to true
PhraseSuggestionBuilder phraseSuggestWithParamsAndReturn = suggest.collateQuery(collateWithParams).collateParams(params).collatePrune(true);
searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", phraseSuggestWithParamsAndReturn);
assertSuggestionSize(searchSuggest, 0, 10, "title");
assertSuggestionPhraseCollateMatchExists(searchSuggest, "title", 2);
}
use of org.elasticsearch.search.suggest.phrase.DirectCandidateGeneratorBuilder in project elasticsearch by elastic.
the class SuggestSearchIT method testSuggestModes.
// see #3037
public void testSuggestModes() throws IOException {
CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0).put("index.analysis.analyzer.biword.tokenizer", "standard").putArray("index.analysis.analyzer.biword.filter", "shingler", "lowercase").put("index.analysis.filter.shingler.type", "shingle").put("index.analysis.filter.shingler.min_shingle_size", 2).put("index.analysis.filter.shingler.max_shingle_size", 3));
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("name").field("type", "text").startObject("fields").startObject("shingled").field("type", "text").field("analyzer", "biword").field("search_analyzer", "standard").endObject().endObject().endObject().endObject().endObject().endObject();
assertAcked(builder.addMapping("type1", mapping));
ensureGreen();
index("test", "type1", "1", "name", "I like iced tea");
index("test", "type1", "2", "name", "I like tea.");
index("test", "type1", "3", "name", "I like ice cream.");
refresh();
DirectCandidateGeneratorBuilder generator = candidateGenerator("name").prefixLength(0).minWordLength(0).suggestMode("always").maxEdits(2);
PhraseSuggestionBuilder phraseSuggestion = phraseSuggestion("name.shingled").addCandidateGenerator(generator).gramSize(3);
Suggest searchSuggest = searchSuggest("ice tea", "did_you_mean", phraseSuggestion);
assertSuggestion(searchSuggest, 0, "did_you_mean", "iced tea");
generator.suggestMode(null);
searchSuggest = searchSuggest("ice tea", "did_you_mean", phraseSuggestion);
assertSuggestionSize(searchSuggest, 0, 0, "did_you_mean");
}
Aggregations