Search in sources :

Example 6 with CreateIndexRequestBuilder

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

the class SimpleQueryStringIT method testSimpleQueryStringAnalyzeWildcard.

public void testSimpleQueryStringAnalyzeWildcard() throws ExecutionException, InterruptedException, IOException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("location").field("type", "text").field("analyzer", "german").endObject().endObject().endObject().endObject().string();
    CreateIndexRequestBuilder mappingRequest = client().admin().indices().prepareCreate("test1").addMapping("type1", mapping, XContentType.JSON);
    mappingRequest.execute().actionGet();
    indexRandom(true, client().prepareIndex("test1", "type1", "1").setSource("location", "Köln"));
    refresh();
    SearchResponse searchResponse = client().prepareSearch().setQuery(simpleQueryStringQuery("Köln*").field("location")).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 1L);
    assertSearchHits(searchResponse, "1");
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with CreateIndexRequestBuilder

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

the class SuggestSearchIT method testSuggestWithManyCandidates.

public void testSuggestWithManyCandidates() 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<>();
    // We're going to be searching for:
    //   united states house of representatives elections in washington 2006
    // But we need to make sure we generate a ton of suggestions so we add a bunch of candidates.
    // Many of these candidates are drawn from page names on English Wikipedia.
    // Tons of different options very near the exact query term
    titles.add("United States House of Representatives Elections in Washington 1789");
    for (int year = 1790; year < 2014; year += 2) {
        titles.add("United States House of Representatives Elections in Washington " + year);
    }
    // since 0.  Why not?
    for (int year = 0; year < 2015; year++) {
        titles.add(Integer.toString(year));
    }
    // That ought to provide more less good candidates for the last term
    // Now remove or add plural copies of every term we can
    titles.add("State");
    titles.add("Houses of Parliament");
    titles.add("Representative Government");
    titles.add("Election");
    // Now some possessive
    titles.add("Washington's Birthday");
    // And some conjugation
    titles.add("Unified Modeling Language");
    titles.add("Unite Against Fascism");
    titles.add("Stated Income Tax");
    titles.add("Media organizations housed within colleges");
    // And other stuff
    titles.add("Untied shoelaces");
    titles.add("Unit circle");
    titles.add("Untitled");
    titles.add("Unicef");
    titles.add("Unrated");
    titles.add("UniRed");
    // Highway in Malaysia
    titles.add("Jalan Uniten–Dengkil");
    titles.add("UNITAS");
    titles.add("UNITER");
    titles.add("Un-Led-Ed");
    titles.add("STATS LLC");
    titles.add("Staples");
    titles.add("Skates");
    titles.add("Statues of the Liberators");
    titles.add("Staten Island");
    titles.add("Statens Museum for Kunst");
    // The last name or the German word, whichever.
    titles.add("Hause");
    titles.add("Hose");
    titles.add("Hoses");
    titles.add("Howse Peak");
    titles.add("The Hoose-Gow");
    titles.add("Hooser");
    titles.add("Electron");
    titles.add("Electors");
    titles.add("Evictions");
    titles.add("Coronal mass ejection");
    // A film?
    titles.add("Wasington");
    // A town in England
    titles.add("Warrington");
    // Lots of places have this name
    titles.add("Waddington");
    // Ditto
    titles.add("Watlington");
    // Yup, also a town
    titles.add("Waplington");
    // Book
    titles.add("Washing of the Spears");
    for (char c = 'A'; c <= 'Z'; c++) {
        // Can't forget lists, glorious lists!
        titles.add("List of former members of the United States House of Representatives (" + c + ")");
        // Lots of people are named Washington <Middle Initial>. LastName
        titles.add("Washington " + c + ". Lastname");
        // Lets just add some more to be evil
        titles.add("United " + c);
        titles.add("States " + c);
        titles.add("House " + c);
        titles.add("Elections " + c);
        titles.add("2006 " + c);
        titles.add(c + " United");
        titles.add(c + " States");
        titles.add(c + " House");
        titles.add(c + " Elections");
        titles.add(c + " 2006");
    }
    List<IndexRequestBuilder> builders = new ArrayList<>();
    for (String title : titles) {
        builders.add(client().prepareIndex("test", "type1").setSource("title", title));
    }
    indexRandom(true, builders);
    PhraseSuggestionBuilder suggest = phraseSuggestion("title").addCandidateGenerator(candidateGenerator("title").suggestMode("always").maxTermFreq(.99f).size(// Setting a silly high size helps of generate a larger list of candidates for testing.
    1000).maxInspections(// This too
    1000)).confidence(0f).maxErrors(2f).shardSize(30000).size(30000);
    Suggest searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", suggest);
    assertSuggestion(searchSuggest, 0, 0, "title", "united states house of representatives elections in washington 2006");
    // Just to prove that we've run through a ton of options
    assertSuggestionSize(searchSuggest, 0, 25480, "title");
    suggest.size(1);
    long start = System.currentTimeMillis();
    searchSuggest = searchSuggest("united states house of representatives elections in washington 2006", "title", suggest);
    long total = System.currentTimeMillis() - start;
    assertSuggestion(searchSuggest, 0, 0, "title", "united states house of representatives elections in washington 2006");
// assertThat(total, lessThan(1000L)); // Takes many seconds without fix - just for debugging
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) ArrayList(java.util.ArrayList) PhraseSuggestionBuilder(org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 8 with CreateIndexRequestBuilder

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

the class SuggestSearchIT method testSizeParam.

public void testSizeParam() throws IOException {
    CreateIndexRequestBuilder builder = prepareCreate("test").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put("index.analysis.analyzer.reverse.tokenizer", "standard").putArray("index.analysis.analyzer.reverse.filter", "lowercase", "reverse").put("index.analysis.analyzer.body.tokenizer", "standard").putArray("index.analysis.analyzer.body.filter", "lowercase").put("index.analysis.analyzer.bigram.tokenizer", "standard").putArray("index.analysis.analyzer.bigram.filter", "my_shingle", "lowercase").put("index.analysis.filter.my_shingle.type", "shingle").put("index.analysis.filter.my_shingle.output_unigrams", false).put("index.analysis.filter.my_shingle.min_shingle_size", 2).put("index.analysis.filter.my_shingle.max_shingle_size", 2));
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("body").field("type", "text").field("analyzer", "body").endObject().startObject("body_reverse").field("type", "text").field("analyzer", "reverse").endObject().startObject("bigram").field("type", "text").field("analyzer", "bigram").endObject().endObject().endObject().endObject();
    assertAcked(builder.addMapping("type1", mapping));
    ensureGreen();
    String line = "xorr the god jewel";
    index("test", "type1", "1", "body", line, "body_reverse", line, "bigram", line);
    line = "I got it this time";
    index("test", "type1", "2", "body", line, "body_reverse", line, "bigram", line);
    refresh();
    PhraseSuggestionBuilder phraseSuggestion = phraseSuggestion("bigram").realWordErrorLikelihood(0.95f).gramSize(2).analyzer("body").addCandidateGenerator(candidateGenerator("body").minWordLength(1).prefixLength(1).suggestMode("always").size(1).accuracy(0.1f)).smoothingModel(new StupidBackoff(0.1)).maxErrors(1.0f).size(5);
    Suggest searchSuggest = searchSuggest("Xorr the Gut-Jewel", "simple_phrase", phraseSuggestion);
    assertSuggestionSize(searchSuggest, 0, 0, "simple_phrase");
    // we allow a size of 2 now on the shard generator level so "god" will be found since it's LD2
    phraseSuggestion.clearCandidateGenerators().addCandidateGenerator(candidateGenerator("body").minWordLength(1).prefixLength(1).suggestMode("always").size(2).accuracy(0.1f));
    searchSuggest = searchSuggest("Xorr the Gut-Jewel", "simple_phrase", phraseSuggestion);
    assertSuggestion(searchSuggest, 0, "simple_phrase", "xorr the god jewel");
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) PhraseSuggestionBuilder(org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) StupidBackoff(org.elasticsearch.search.suggest.phrase.StupidBackoff)

Example 9 with CreateIndexRequestBuilder

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

the class MatchQueryIT method setUp.

/**
     * Test setup.
     */
@Before
public void setUp() throws Exception {
    super.setUp();
    CreateIndexRequestBuilder builder = prepareCreate(INDEX).setSettings(Settings.builder().put(indexSettings()).put("index.analysis.filter.syns.type", "synonym").putArray("index.analysis.filter.syns.synonyms", "wtf, what the fudge", "foo, bar baz").put("index.analysis.analyzer.lower_syns.type", "custom").put("index.analysis.analyzer.lower_syns.tokenizer", "standard").putArray("index.analysis.analyzer.lower_syns.filter", "lowercase", "syns").put("index.analysis.filter.graphsyns.type", "synonym_graph").putArray("index.analysis.filter.graphsyns.synonyms", "wtf, what the fudge", "foo, bar baz").put("index.analysis.analyzer.lower_graphsyns.type", "custom").put("index.analysis.analyzer.lower_graphsyns.tokenizer", "standard").putArray("index.analysis.analyzer.lower_graphsyns.filter", "lowercase", "graphsyns"));
    assertAcked(builder.addMapping(INDEX, createMapping()));
    ensureGreen();
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) Before(org.junit.Before)

Example 10 with CreateIndexRequestBuilder

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

the class IndexAliasesIT method testCreateIndexWithAliasesFilterNotValid.

public void testCreateIndexWithAliasesFilterNotValid() {
    //non valid filter, invalid json
    CreateIndexRequestBuilder createIndexRequestBuilder = prepareCreate("test").addAlias(new Alias("alias2").filter("f"));
    try {
        createIndexRequestBuilder.get();
        fail("create index should have failed due to invalid alias filter");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("failed to parse filter for alias [alias2]"));
    }
    //valid json but non valid filter
    createIndexRequestBuilder = prepareCreate("test").addAlias(new Alias("alias2").filter("{ \"test\": {} }"));
    try {
        createIndexRequestBuilder.get();
        fail("create index should have failed due to invalid alias filter");
    } catch (IllegalArgumentException e) {
        assertThat(e.getMessage(), equalTo("failed to parse filter for alias [alias2]"));
    }
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)

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