Search in sources :

Example 31 with Script

use of org.elasticsearch.script.Script in project elasticsearch by elastic.

the class RandomSearchRequestGenerator method randomSearchSourceBuilder.

public static SearchSourceBuilder randomSearchSourceBuilder(Supplier<HighlightBuilder> randomHighlightBuilder, Supplier<SuggestBuilder> randomSuggestBuilder, Supplier<RescoreBuilder<?>> randomRescoreBuilder, Supplier<List<SearchExtBuilder>> randomExtBuilders, Supplier<CollapseBuilder> randomCollapseBuilder) {
    SearchSourceBuilder builder = new SearchSourceBuilder();
    if (randomBoolean()) {
        builder.from(randomIntBetween(0, 10000));
    }
    if (randomBoolean()) {
        builder.size(randomIntBetween(0, 10000));
    }
    if (randomBoolean()) {
        builder.explain(randomBoolean());
    }
    if (randomBoolean()) {
        builder.version(randomBoolean());
    }
    if (randomBoolean()) {
        builder.trackScores(randomBoolean());
    }
    if (randomBoolean()) {
        builder.minScore(randomFloat() * 1000);
    }
    if (randomBoolean()) {
        builder.timeout(TimeValue.parseTimeValue(randomTimeValue(), null, "timeout"));
    }
    if (randomBoolean()) {
        builder.terminateAfter(randomIntBetween(1, 100000));
    }
    switch(randomInt(2)) {
        case 0:
            builder.storedFields();
            break;
        case 1:
            builder.storedField("_none_");
            break;
        case 2:
            int fieldsSize = randomInt(25);
            List<String> fields = new ArrayList<>(fieldsSize);
            for (int i = 0; i < fieldsSize; i++) {
                fields.add(randomAsciiOfLengthBetween(5, 50));
            }
            builder.storedFields(fields);
            break;
        default:
            throw new IllegalStateException();
    }
    if (randomBoolean()) {
        int scriptFieldsSize = randomInt(25);
        for (int i = 0; i < scriptFieldsSize; i++) {
            if (randomBoolean()) {
                builder.scriptField(randomAsciiOfLengthBetween(5, 50), new Script("foo"), randomBoolean());
            } else {
                builder.scriptField(randomAsciiOfLengthBetween(5, 50), new Script("foo"));
            }
        }
    }
    if (randomBoolean()) {
        FetchSourceContext fetchSourceContext;
        int branch = randomInt(5);
        String[] includes = new String[randomIntBetween(0, 20)];
        for (int i = 0; i < includes.length; i++) {
            includes[i] = randomAsciiOfLengthBetween(5, 20);
        }
        String[] excludes = new String[randomIntBetween(0, 20)];
        for (int i = 0; i < excludes.length; i++) {
            excludes[i] = randomAsciiOfLengthBetween(5, 20);
        }
        switch(branch) {
            case 0:
                fetchSourceContext = new FetchSourceContext(randomBoolean());
                break;
            case 1:
                fetchSourceContext = new FetchSourceContext(true, includes, excludes);
                break;
            case 2:
                fetchSourceContext = new FetchSourceContext(true, new String[] { randomAsciiOfLengthBetween(5, 20) }, new String[] { randomAsciiOfLengthBetween(5, 20) });
                break;
            case 3:
                fetchSourceContext = new FetchSourceContext(true, includes, excludes);
                break;
            case 4:
                fetchSourceContext = new FetchSourceContext(true, includes, null);
                break;
            case 5:
                fetchSourceContext = new FetchSourceContext(true, new String[] { randomAsciiOfLengthBetween(5, 20) }, null);
                break;
            default:
                throw new IllegalStateException();
        }
        builder.fetchSource(fetchSourceContext);
    }
    if (randomBoolean()) {
        int size = randomIntBetween(0, 20);
        List<String> statsGroups = new ArrayList<>(size);
        for (int i = 0; i < size; i++) {
            statsGroups.add(randomAsciiOfLengthBetween(5, 20));
        }
        builder.stats(statsGroups);
    }
    if (randomBoolean()) {
        int indexBoostSize = randomIntBetween(1, 10);
        for (int i = 0; i < indexBoostSize; i++) {
            builder.indexBoost(randomAsciiOfLengthBetween(5, 20), randomFloat() * 10);
        }
    }
    if (randomBoolean()) {
        builder.query(QueryBuilders.termQuery(randomAsciiOfLengthBetween(5, 20), randomAsciiOfLengthBetween(5, 20)));
    }
    if (randomBoolean()) {
        builder.postFilter(QueryBuilders.termQuery(randomAsciiOfLengthBetween(5, 20), randomAsciiOfLengthBetween(5, 20)));
    }
    if (randomBoolean()) {
        int numSorts = randomIntBetween(1, 5);
        for (int i = 0; i < numSorts; i++) {
            int branch = randomInt(5);
            switch(branch) {
                case 0:
                    builder.sort(SortBuilders.fieldSort(randomAsciiOfLengthBetween(5, 20)).order(randomFrom(SortOrder.values())));
                    break;
                case 1:
                    builder.sort(SortBuilders.geoDistanceSort(randomAsciiOfLengthBetween(5, 20), AbstractQueryTestCase.randomGeohash(1, 12)).order(randomFrom(SortOrder.values())));
                    break;
                case 2:
                    builder.sort(SortBuilders.scoreSort().order(randomFrom(SortOrder.values())));
                    break;
                case 3:
                    builder.sort(SortBuilders.scriptSort(new Script("foo"), ScriptSortBuilder.ScriptSortType.NUMBER).order(randomFrom(SortOrder.values())));
                    break;
                case 4:
                    builder.sort(randomAsciiOfLengthBetween(5, 20));
                    break;
                case 5:
                    builder.sort(randomAsciiOfLengthBetween(5, 20), randomFrom(SortOrder.values()));
                    break;
            }
        }
    }
    if (randomBoolean()) {
        int numSearchFrom = randomIntBetween(1, 5);
        try {
            // We build a json version of the search_from first in order to
            // ensure that every number type remain the same before/after xcontent (de)serialization.
            // This is not a problem because the final type of each field value is extracted from associated sort field.
            // This little trick ensure that equals and hashcode are the same when using the xcontent serialization.
            XContentBuilder jsonBuilder = XContentFactory.jsonBuilder();
            jsonBuilder.startObject();
            jsonBuilder.startArray("search_from");
            for (int i = 0; i < numSearchFrom; i++) {
                int branch = randomInt(8);
                switch(branch) {
                    case 0:
                        jsonBuilder.value(randomInt());
                        break;
                    case 1:
                        jsonBuilder.value(randomFloat());
                        break;
                    case 2:
                        jsonBuilder.value(randomLong());
                        break;
                    case 3:
                        jsonBuilder.value(randomDouble());
                        break;
                    case 4:
                        jsonBuilder.value(randomAsciiOfLengthBetween(5, 20));
                        break;
                    case 5:
                        jsonBuilder.value(randomBoolean());
                        break;
                    case 6:
                        jsonBuilder.value(randomByte());
                        break;
                    case 7:
                        jsonBuilder.value(randomShort());
                        break;
                    case 8:
                        jsonBuilder.value(new Text(randomAsciiOfLengthBetween(5, 20)));
                        break;
                }
            }
            jsonBuilder.endArray();
            jsonBuilder.endObject();
            XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(NamedXContentRegistry.EMPTY, jsonBuilder.bytes());
            parser.nextToken();
            parser.nextToken();
            parser.nextToken();
            builder.searchAfter(SearchAfterBuilder.fromXContent(parser).getSortValues());
        } catch (IOException e) {
            throw new RuntimeException("Error building search_from", e);
        }
    }
    if (randomBoolean()) {
        builder.highlighter(randomHighlightBuilder.get());
    }
    if (randomBoolean()) {
        builder.suggest(randomSuggestBuilder.get());
    }
    if (randomBoolean()) {
        int numRescores = randomIntBetween(1, 5);
        for (int i = 0; i < numRescores; i++) {
            builder.addRescorer(randomRescoreBuilder.get());
        }
    }
    if (randomBoolean()) {
        builder.aggregation(AggregationBuilders.avg(randomAsciiOfLengthBetween(5, 20)));
    }
    if (randomBoolean()) {
        builder.ext(randomExtBuilders.get());
    }
    if (randomBoolean()) {
        String field = randomBoolean() ? null : randomAsciiOfLengthBetween(5, 20);
        int max = between(2, 1000);
        int id = randomInt(max - 1);
        if (field == null) {
            builder.slice(new SliceBuilder(id, max));
        } else {
            builder.slice(new SliceBuilder(field, id, max));
        }
    }
    if (randomBoolean()) {
        builder.collapse(randomCollapseBuilder.get());
    }
    return builder;
}
Also used : Script(org.elasticsearch.script.Script) ArrayList(java.util.ArrayList) Text(org.elasticsearch.common.text.Text) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) SliceBuilder(org.elasticsearch.search.slice.SliceBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 32 with Script

use of org.elasticsearch.script.Script in project elasticsearch-river-rabbitmq by elastic.

the class RabbitmqRiver method buildScript.

/**
     * Build an executable script if provided as settings
     * @param settingName
     * @return
     */
private ExecutableScript buildScript(String settingName) {
    if (settings.settings().containsKey(settingName)) {
        Map<String, Object> scriptSettings = (Map<String, Object>) settings.settings().get(settingName);
        if (scriptSettings.containsKey("script")) {
            String scriptLang = "groovy";
            if (scriptSettings.containsKey("script_lang")) {
                scriptLang = scriptSettings.get("script_lang").toString();
            }
            Map<String, Object> scriptParams = null;
            if (scriptSettings.containsKey("script_params")) {
                scriptParams = (Map<String, Object>) scriptSettings.get("script_params");
            } else {
                scriptParams = Maps.newHashMap();
            }
            return scriptService.executable(new Script(scriptLang, scriptSettings.get("script").toString(), ScriptService.ScriptType.INLINE, scriptParams), ScriptContext.Standard.UPDATE);
        }
    }
    return null;
}
Also used : Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) HashMap(java.util.HashMap) Map(java.util.Map)

Example 33 with Script

use of org.elasticsearch.script.Script in project elasticsearch by elastic.

the class SearchCancellationIT method testCancellationOfScrollSearches.

public void testCancellationOfScrollSearches() throws Exception {
    List<ScriptedBlockPlugin> plugins = initBlockFactory();
    indexTestData();
    logger.info("Executing search");
    ListenableActionFuture<SearchResponse> searchResponse = client().prepareSearch("test").setScroll(TimeValue.timeValueSeconds(10)).setSize(5).setQuery(scriptQuery(new Script(ScriptType.INLINE, "native", NativeTestScriptedBlockFactory.TEST_NATIVE_BLOCK_SCRIPT, Collections.emptyMap()))).execute();
    awaitForBlock(plugins);
    cancelSearch(SearchAction.NAME);
    disableBlocks(plugins);
    SearchResponse response = ensureSearchWasCancelled(searchResponse);
    if (response != null) {
        // The response might not have failed on all shards - we need to clean scroll
        logger.info("Cleaning scroll with id {}", response.getScrollId());
        client().prepareClearScroll().addScrollId(response.getScrollId()).get();
    }
}
Also used : AbstractSearchScript(org.elasticsearch.script.AbstractSearchScript) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 34 with Script

use of org.elasticsearch.script.Script in project elasticsearch by elastic.

the class SearchCancellationIT method testCancellationDuringQueryPhase.

public void testCancellationDuringQueryPhase() throws Exception {
    List<ScriptedBlockPlugin> plugins = initBlockFactory();
    indexTestData();
    logger.info("Executing search");
    ListenableActionFuture<SearchResponse> searchResponse = client().prepareSearch("test").setQuery(scriptQuery(new Script(ScriptType.INLINE, "native", NativeTestScriptedBlockFactory.TEST_NATIVE_BLOCK_SCRIPT, Collections.emptyMap()))).execute();
    awaitForBlock(plugins);
    cancelSearch(SearchAction.NAME);
    disableBlocks(plugins);
    logger.info("Segments {}", XContentHelper.toString(client().admin().indices().prepareSegments("test").get(), FORMAT_PARAMS));
    ensureSearchWasCancelled(searchResponse);
}
Also used : AbstractSearchScript(org.elasticsearch.script.AbstractSearchScript) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 35 with Script

use of org.elasticsearch.script.Script in project elasticsearch by elastic.

the class EquivalenceIT method testDuelTermsHistogram.

// Duel between histograms and scripted terms
public void testDuelTermsHistogram() throws Exception {
    prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("num").field("type", "double").endObject().endObject().endObject().endObject()).execute().actionGet();
    final int numDocs = scaledRandomIntBetween(500, 5000);
    final int maxNumTerms = randomIntBetween(10, 2000);
    final int interval = randomIntBetween(1, 100);
    final Integer[] values = new Integer[maxNumTerms];
    for (int i = 0; i < values.length; ++i) {
        values[i] = randomInt(maxNumTerms * 3) - maxNumTerms;
    }
    for (int i = 0; i < numDocs; ++i) {
        XContentBuilder source = jsonBuilder().startObject().field("num", randomDouble()).startArray("values");
        final int numValues = randomInt(4);
        for (int j = 0; j < numValues; ++j) {
            source = source.value(randomFrom(values));
        }
        source = source.endArray().endObject();
        client().prepareIndex("idx", "type").setSource(source).execute().actionGet();
    }
    assertNoFailures(client().admin().indices().prepareRefresh("idx").setIndicesOptions(IndicesOptions.lenientExpandOpen()).execute().get());
    Map<String, Object> params = new HashMap<>();
    params.put("interval", interval);
    SearchResponse resp = client().prepareSearch("idx").addAggregation(terms("terms").field("values").collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "floor(_value / interval)", params)).size(maxNumTerms)).addAggregation(histogram("histo").field("values").interval(interval).minDocCount(1)).execute().actionGet();
    assertSearchResponse(resp);
    Terms terms = resp.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    Histogram histo = resp.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(terms.getBuckets().size(), equalTo(histo.getBuckets().size()));
    for (Histogram.Bucket bucket : histo.getBuckets()) {
        final double key = ((Number) bucket.getKey()).doubleValue() / interval;
        final Terms.Bucket termsBucket = terms.getBucketByKey(String.valueOf(key));
        assertEquals(bucket.getDocCount(), termsBucket.getDocCount());
    }
}
Also used : Script(org.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) HashMap(java.util.HashMap) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Aggregations

Script (org.elasticsearch.script.Script)307 SearchResponse (org.elasticsearch.action.search.SearchResponse)223 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)206 HashMap (java.util.HashMap)91 ExecutableScript (org.elasticsearch.script.ExecutableScript)52 CompiledScript (org.elasticsearch.script.CompiledScript)46 SearchScript (org.elasticsearch.script.SearchScript)41 ArrayList (java.util.ArrayList)32 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)32 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)26 LeafSearchScript (org.elasticsearch.script.LeafSearchScript)25 Matchers.containsString (org.hamcrest.Matchers.containsString)23 List (java.util.List)22 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)22 Map (java.util.Map)18 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)18 PercentileRanks (org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks)18 Percentiles (org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles)18 XContentParser (org.elasticsearch.common.xcontent.XContentParser)16 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)16