Search in sources :

Example 86 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.

the class GeoDistanceSortBuilderIT method testManyToManyGeoPointsWithDifferentFormats.

public void testManyToManyGeoPointsWithDifferentFormats() throws ExecutionException, InterruptedException, IOException {
    /**   q     d1       d2
         * |4  o|   x    |   x
         * |    |        |
         * |3  o|  x     |  x
         * |    |        |
         * |2  o| x      | x
         * |    |        |
         * |1  o|x       |x
         * |______________________
         * 1   2   3   4   5   6
         */
    Version version = randomBoolean() ? Version.CURRENT : VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
    Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
    assertAcked(prepareCreate("index").setSettings(settings).addMapping("type", LOCATION_FIELD, "type=geo_point"));
    XContentBuilder d1Builder = jsonBuilder();
    GeoPoint[] d1Points = { new GeoPoint(2.5, 1), new GeoPoint(2.75, 2), new GeoPoint(3, 3), new GeoPoint(3.25, 4) };
    createShuffeldJSONArray(d1Builder, d1Points);
    XContentBuilder d2Builder = jsonBuilder();
    GeoPoint[] d2Points = { new GeoPoint(4.5, 1), new GeoPoint(4.75, 2), new GeoPoint(5, 3), new GeoPoint(5.25, 4) };
    createShuffeldJSONArray(d2Builder, d2Points);
    indexRandom(true, client().prepareIndex("index", "type", "d1").setSource(d1Builder), client().prepareIndex("index", "type", "d2").setSource(d2Builder));
    List<String> qHashes = new ArrayList<>();
    List<GeoPoint> qPoints = new ArrayList<>();
    createQPoints(qHashes, qPoints);
    GeoDistanceSortBuilder geoDistanceSortBuilder = null;
    for (int i = 0; i < 4; i++) {
        int at = randomInt(3 - i);
        if (randomBoolean()) {
            if (geoDistanceSortBuilder == null) {
                geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, qHashes.get(at));
            } else {
                geoDistanceSortBuilder.geohashes(qHashes.get(at));
            }
        } else {
            if (geoDistanceSortBuilder == null) {
                geoDistanceSortBuilder = new GeoDistanceSortBuilder(LOCATION_FIELD, qPoints.get(at));
            } else {
                geoDistanceSortBuilder.points(qPoints.get(at));
            }
        }
        qHashes.remove(at);
        qPoints.remove(at);
    }
    SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.sortMode(SortMode.MIN).order(SortOrder.ASC)).execute().actionGet();
    assertOrderedSearchHits(searchResponse, "d1", "d2");
    assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(2.5, 1, 2, 1, DistanceUnit.METERS), 1.e-1));
    assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(4.5, 1, 2, 1, DistanceUnit.METERS), 1.e-1));
    searchResponse = client().prepareSearch().setQuery(matchAllQuery()).addSort(geoDistanceSortBuilder.sortMode(SortMode.MAX).order(SortOrder.ASC)).execute().actionGet();
    assertOrderedSearchHits(searchResponse, "d1", "d2");
    assertThat((Double) searchResponse.getHits().getAt(0).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(3.25, 4, 2, 1, DistanceUnit.METERS), 1.e-1));
    assertThat((Double) searchResponse.getHits().getAt(1).getSortValues()[0], closeTo(GeoDistance.ARC.calculate(5.25, 4, 2, 1, DistanceUnit.METERS), 1.e-1));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) Version(org.elasticsearch.Version) ArrayList(java.util.ArrayList) Settings(org.elasticsearch.common.settings.Settings) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 87 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder 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 88 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder 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 89 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder 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 90 with XContentBuilder

use of org.elasticsearch.common.xcontent.XContentBuilder in project elasticsearch by elastic.

the class SuggestSearchIT method testSuggestAcrossMultipleIndices.

// see #3196
public void testSuggestAcrossMultipleIndices() throws IOException {
    createIndex("test");
    ensureGreen();
    index("test", "type1", "1", "text", "abcd");
    index("test", "type1", "2", "text", "aacd");
    index("test", "type1", "3", "text", "abbd");
    index("test", "type1", "4", "text", "abcc");
    refresh();
    TermSuggestionBuilder termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("abcd");
    logger.info("--> run suggestions with one index");
    searchSuggest("test", termSuggest);
    createIndex("test_1");
    ensureGreen();
    index("test_1", "type1", "1", "text", "ab cd");
    index("test_1", "type1", "2", "text", "aa cd");
    index("test_1", "type1", "3", "text", "ab bd");
    index("test_1", "type1", "4", "text", "ab cc");
    refresh();
    termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("ab cd").minWordLength(1);
    logger.info("--> run suggestions with two indices");
    searchSuggest("test", termSuggest);
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("text").field("type", "text").field("analyzer", "keyword").endObject().endObject().endObject().endObject();
    assertAcked(prepareCreate("test_2").addMapping("type1", mapping));
    ensureGreen();
    index("test_2", "type1", "1", "text", "ab cd");
    index("test_2", "type1", "2", "text", "aa cd");
    index("test_2", "type1", "3", "text", "ab bd");
    index("test_2", "type1", "4", "text", "ab cc");
    index("test_2", "type1", "1", "text", "abcd");
    index("test_2", "type1", "2", "text", "aacd");
    index("test_2", "type1", "3", "text", "abbd");
    index("test_2", "type1", "4", "text", "abcc");
    refresh();
    termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("ab cd").minWordLength(1);
    logger.info("--> run suggestions with three indices");
    try {
        searchSuggest("test", termSuggest);
        fail(" can not suggest across multiple indices with different analysis chains");
    } catch (SearchPhaseExecutionException ex) {
        assertThat(ex.getCause(), instanceOf(IllegalStateException.class));
        assertThat(ex.getCause().getMessage(), anyOf(endsWith("Suggest entries have different sizes actual [1] expected [2]"), endsWith("Suggest entries have different sizes actual [2] expected [1]")));
    } catch (IllegalStateException ex) {
        assertThat(ex.getMessage(), anyOf(endsWith("Suggest entries have different sizes actual [1] expected [2]"), endsWith("Suggest entries have different sizes actual [2] expected [1]")));
    }
    termSuggest = termSuggestion("text").suggestMode(// Always, otherwise the results can vary between requests.
    SuggestMode.ALWAYS).text("ABCD").minWordLength(1);
    logger.info("--> run suggestions with four indices");
    try {
        searchSuggest("test", termSuggest);
        fail(" can not suggest across multiple indices with different analysis chains");
    } catch (SearchPhaseExecutionException ex) {
        assertThat(ex.getCause(), instanceOf(IllegalStateException.class));
        assertThat(ex.getCause().getMessage(), anyOf(endsWith("Suggest entries have different text actual [ABCD] expected [abcd]"), endsWith("Suggest entries have different text actual [abcd] expected [ABCD]")));
    } catch (IllegalStateException ex) {
        assertThat(ex.getMessage(), anyOf(endsWith("Suggest entries have different text actual [ABCD] expected [abcd]"), endsWith("Suggest entries have different text actual [abcd] expected [ABCD]")));
    }
}
Also used : SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) TermSuggestionBuilder(org.elasticsearch.search.suggest.term.TermSuggestionBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)619 IOException (java.io.IOException)127 XContentParser (org.elasticsearch.common.xcontent.XContentParser)122 Settings (org.elasticsearch.common.settings.Settings)60 ArrayList (java.util.ArrayList)59 SearchResponse (org.elasticsearch.action.search.SearchResponse)56 Matchers.containsString (org.hamcrest.Matchers.containsString)53 HashMap (java.util.HashMap)47 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)43 Map (java.util.Map)41 RestRequest (org.elasticsearch.rest.RestRequest)37 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)36 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)35 Test (org.junit.Test)34 RestController (org.elasticsearch.rest.RestController)33 NodeClient (org.elasticsearch.client.node.NodeClient)32 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)32 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)32 GeoPoint (org.elasticsearch.common.geo.GeoPoint)31 CrateUnitTest (io.crate.test.integration.CrateUnitTest)28