Search in sources :

Example 16 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class SearchQueryIT method testMatchQueryWithSynonyms.

// see #3881 - for extensive description of the issue
public void testMatchQueryWithSynonyms() throws IOException {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.analysis.analyzer.index.type", "custom").put("index.analysis.analyzer.index.tokenizer", "standard").put("index.analysis.analyzer.index.filter", "lowercase").put("index.analysis.analyzer.search.type", "custom").put("index.analysis.analyzer.search.tokenizer", "standard").putArray("index.analysis.analyzer.search.filter", "lowercase", "synonym").put("index.analysis.filter.synonym.type", "synonym").putArray("index.analysis.filter.synonym.synonyms", "fast, quick"));
    assertAcked(builder.addMapping("test", "text", "type=text,analyzer=index,search_analyzer=search"));
    client().prepareIndex("test", "test", "1").setSource("text", "quick brown fox").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick").operator(Operator.AND)).get();
    assertHitCount(searchResponse, 1);
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick brown").operator(Operator.AND)).get();
    assertHitCount(searchResponse, 1);
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "fast").operator(Operator.AND)).get();
    assertHitCount(searchResponse, 1);
    client().prepareIndex("test", "test", "2").setSource("text", "fast brown fox").get();
    refresh();
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick").operator(Operator.AND)).get();
    assertHitCount(searchResponse, 2);
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "quick brown").operator(Operator.AND)).get();
    assertHitCount(searchResponse, 2);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class SearchQueryIT method testMatchQueryWithStackedStems.

public void testMatchQueryWithStackedStems() throws IOException {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.analysis.analyzer.index.type", "custom").put("index.analysis.analyzer.index.tokenizer", "standard").put("index.analysis.analyzer.index.filter", "lowercase").put("index.analysis.analyzer.search.type", "custom").put("index.analysis.analyzer.search.tokenizer", "standard").putArray("index.analysis.analyzer.search.filter", "lowercase", "keyword_repeat", "porter_stem", "unique_stem").put("index.analysis.filter.unique_stem.type", "unique").put("index.analysis.filter.unique_stem.only_on_same_position", true));
    assertAcked(builder.addMapping("test", "text", "type=text,analyzer=index,search_analyzer=search"));
    client().prepareIndex("test", "test", "1").setSource("text", "the fox runs across the street").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "fox runs").operator(Operator.AND)).get();
    assertHitCount(searchResponse, 1);
    client().prepareIndex("test", "test", "2").setSource("text", "run fox run").get();
    refresh();
    searchResponse = client().prepareSearch("test").setQuery(matchQuery("text", "fox runs").operator(Operator.AND)).get();
    assertHitCount(searchResponse, 2);
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 18 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder 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);
}
Also used : HashMap(java.util.HashMap) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) ArrayList(java.util.ArrayList) ElasticsearchException(org.elasticsearch.ElasticsearchException) DirectCandidateGeneratorBuilder(org.elasticsearch.search.suggest.phrase.DirectCandidateGeneratorBuilder) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) PhraseSuggestionBuilder(org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder) TermSuggestionBuilder(org.elasticsearch.search.suggest.term.TermSuggestionBuilder) PhraseSuggestionBuilder(org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 19 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class SuggestSearchIT method testShardFailures.

// see #3469
public void testShardFailures() throws IOException, InterruptedException {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.analysis.analyzer.suggest.tokenizer", "standard").putArray("index.analysis.analyzer.suggest.filter", "standard", "lowercase", "shingler").put("index.analysis.filter.shingler.type", "shingle").put("index.analysis.filter.shingler.min_shingle_size", 2).put("index.analysis.filter.shingler.max_shingle_size", 5).put("index.analysis.filter.shingler.output_unigrams", true));
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type2").startObject("properties").startObject("name").field("type", "text").field("analyzer", "suggest").endObject().endObject().endObject().endObject();
    assertAcked(builder.addMapping("type2", mapping));
    ensureGreen();
    index("test", "type2", "1", "foo", "bar");
    index("test", "type2", "2", "foo", "bar");
    index("test", "type2", "3", "foo", "bar");
    index("test", "type2", "4", "foo", "bar");
    index("test", "type2", "5", "foo", "bar");
    index("test", "type2", "1", "name", "Just testing the suggestions api");
    index("test", "type2", "2", "name", "An other title about equal length");
    // Note that the last document has to have about the same length as the other or cutoff rechecking will remove the useful suggestion
    refresh();
    // When searching on a shard with a non existing mapping, we should fail
    SearchRequestBuilder request = client().prepareSearch().setSize(0).suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion("fielddoesnotexist").maxErrors(5.0f)));
    assertThrows(request, SearchPhaseExecutionException.class);
    // When searching on a shard which does not hold yet any document of an existing type, we should not fail
    SearchResponse searchResponse = client().prepareSearch().setSize(0).suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion("name").maxErrors(5.0f))).get();
    ElasticsearchAssertions.assertNoFailures(searchResponse);
    ElasticsearchAssertions.assertSuggestion(searchResponse.getSuggest(), 0, 0, "did_you_mean", "testing suggestions");
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 20 with CreateIndexRequestBuilder

use of org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder in project elasticsearch by elastic.

the class SuggestSearchIT method testUnmappedField.

public void testUnmappedField() throws IOException, InterruptedException, ExecutionException {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).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();
    indexRandom(true, client().prepareIndex("test", "type1").setSource("name", "I like iced tea"), client().prepareIndex("test", "type1").setSource("name", "I like tea."), client().prepareIndex("test", "type1").setSource("name", "I like ice cream."));
    refresh();
    PhraseSuggestionBuilder phraseSuggestion = phraseSuggestion("name.shingled").addCandidateGenerator(candidateGenerator("name").prefixLength(0).minWordLength(0).suggestMode("always").maxEdits(2)).gramSize(3);
    Suggest searchSuggest = searchSuggest("ice tea", "did_you_mean", phraseSuggestion);
    assertSuggestion(searchSuggest, 0, 0, "did_you_mean", "iced tea");
    phraseSuggestion = phraseSuggestion("nosuchField").addCandidateGenerator(candidateGenerator("name").prefixLength(0).minWordLength(0).suggestMode("always").maxEdits(2)).gramSize(3);
    {
        SearchRequestBuilder searchBuilder = client().prepareSearch().setSize(0);
        searchBuilder.suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion));
        assertThrows(searchBuilder, SearchPhaseExecutionException.class);
    }
    {
        SearchRequestBuilder searchBuilder = client().prepareSearch().setSize(0);
        searchBuilder.suggest(new SuggestBuilder().setGlobalText("tetsting sugestion").addSuggestion("did_you_mean", phraseSuggestion));
        assertThrows(searchBuilder, SearchPhaseExecutionException.class);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) PhraseSuggestionBuilder(org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)30 SearchResponse (org.elasticsearch.action.search.SearchResponse)11 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)11 ArrayList (java.util.ArrayList)8 PhraseSuggestionBuilder (org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder)7 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)6 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)5 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Before (org.junit.Before)3 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)2 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)2 DirectCandidateGeneratorBuilder (org.elasticsearch.search.suggest.phrase.DirectCandidateGeneratorBuilder)2 StupidBackoff (org.elasticsearch.search.suggest.phrase.StupidBackoff)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ElasticsearchException (org.elasticsearch.ElasticsearchException)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1