Search in sources :

Example 16 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class FieldSortIT method testSortDuelBetweenSingleShardAndMultiShardIndex.

public void testSortDuelBetweenSingleShardAndMultiShardIndex() throws Exception {
    String sortField = "sortField";
    assertAcked(prepareCreate("test1").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, between(2, maximumNumberOfShards())).addMapping("type", sortField, "type=long").get());
    assertAcked(prepareCreate("test2").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).addMapping("type", sortField, "type=long").get());
    for (String index : new String[] { "test1", "test2" }) {
        List<IndexRequestBuilder> docs = new ArrayList<>();
        for (int i = 0; i < 256; i++) {
            docs.add(client().prepareIndex(index, "type", Integer.toString(i)).setSource(sortField, i));
        }
        indexRandom(true, docs);
    }
    ensureSearchable("test1", "test2");
    SortOrder order = randomBoolean() ? SortOrder.ASC : SortOrder.DESC;
    int from = between(0, 256);
    int size = between(0, 256);
    SearchResponse multiShardResponse = client().prepareSearch("test1").setFrom(from).setSize(size).addSort(sortField, order).get();
    assertNoFailures(multiShardResponse);
    SearchResponse singleShardResponse = client().prepareSearch("test2").setFrom(from).setSize(size).addSort(sortField, order).get();
    assertNoFailures(singleShardResponse);
    assertThat(multiShardResponse.getHits().getTotalHits(), equalTo(singleShardResponse.getHits().getTotalHits()));
    assertThat(multiShardResponse.getHits().getHits().length, equalTo(singleShardResponse.getHits().getHits().length));
    for (int i = 0; i < multiShardResponse.getHits().getHits().length; i++) {
        assertThat(multiShardResponse.getHits().getAt(i).getSortValues()[0], equalTo(singleShardResponse.getHits().getAt(i).getSortValues()[0]));
        assertThat(multiShardResponse.getHits().getAt(i).getId(), equalTo(singleShardResponse.getHits().getAt(i).getId()));
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method testMissingContextValue.

public void testMissingContextValue() throws Exception {
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
    map.put("cat", ContextBuilder.category("cat").field("cat").build());
    map.put("type", ContextBuilder.category("type").field("type").build());
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject();
        if (randomBoolean()) {
            source.field("cat", "cat" + i % 2);
        }
        if (randomBoolean()) {
            source.field("type", "type" + i % 4);
        }
        source.endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 18 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder 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 19 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method testSeveralContexts.

public void testSeveralContexts() throws Exception {
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
    final int numContexts = randomIntBetween(2, 5);
    for (int i = 0; i < numContexts; i++) {
        map.put("type" + i, ContextBuilder.category("type" + i).field("type" + i).build());
    }
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = randomIntBetween(10, 200);
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", numDocs - i).endObject();
        for (int c = 0; c < numContexts; c++) {
            source.field("type" + c, "type" + c + i % 4);
        }
        source.endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    assertSuggestions("foo", prefix, "suggestion0", "suggestion1", "suggestion2", "suggestion3", "suggestion4");
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Example 20 with IndexRequestBuilder

use of org.elasticsearch.action.index.IndexRequestBuilder in project elasticsearch by elastic.

the class ContextCompletionSuggestSearchIT method testContextPrefix.

public void testContextPrefix() throws Exception {
    LinkedHashMap<String, ContextMapping> map = new LinkedHashMap<>();
    map.put("cat", ContextBuilder.category("cat").field("cat").build());
    boolean addAnotherContext = randomBoolean();
    if (addAnotherContext) {
        map.put("type", ContextBuilder.category("type").field("type").build());
    }
    final CompletionMappingBuilder mapping = new CompletionMappingBuilder().context(map);
    createIndexAndMapping(mapping);
    int numDocs = 10;
    List<IndexRequestBuilder> indexRequestBuilders = new ArrayList<>();
    for (int i = 0; i < numDocs; i++) {
        XContentBuilder source = jsonBuilder().startObject().startObject(FIELD).field("input", "suggestion" + i).field("weight", i + 1).endObject().field("cat", "cat" + i % 2);
        if (addAnotherContext) {
            source.field("type", "type" + i % 3);
        }
        source.endObject();
        indexRequestBuilders.add(client().prepareIndex(INDEX, TYPE, "" + i).setSource(source));
    }
    indexRandom(true, indexRequestBuilders);
    CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg");
    assertSuggestions("foo", prefix, "suggestion9", "suggestion8", "suggestion7", "suggestion6", "suggestion5");
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) CompletionSuggestionBuilder(org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder) GeoContextMapping(org.elasticsearch.search.suggest.completion.context.GeoContextMapping) ContextMapping(org.elasticsearch.search.suggest.completion.context.ContextMapping) CategoryContextMapping(org.elasticsearch.search.suggest.completion.context.CategoryContextMapping) ArrayList(java.util.ArrayList) CompletionMappingBuilder(org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)227 ArrayList (java.util.ArrayList)134 SearchResponse (org.elasticsearch.action.search.SearchResponse)125 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)48 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)45 Matchers.containsString (org.hamcrest.Matchers.containsString)38 GeoPoint (org.elasticsearch.common.geo.GeoPoint)36 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)31 CompletionSuggestionBuilder (org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder)23 Settings (org.elasticsearch.common.settings.Settings)21 IOException (java.io.IOException)19 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)19 Client (org.elasticsearch.client.Client)18 SearchHit (org.elasticsearch.search.SearchHit)17 LinkedHashMap (java.util.LinkedHashMap)16 SearchHits (org.elasticsearch.search.SearchHits)16 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)15 CompletionMappingBuilder (org.elasticsearch.search.suggest.CompletionSuggestSearchIT.CompletionMappingBuilder)15 CategoryContextMapping (org.elasticsearch.search.suggest.completion.context.CategoryContextMapping)15 ContextMapping (org.elasticsearch.search.suggest.completion.context.ContextMapping)15