Search in sources :

Example 81 with IndexRequestBuilder

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

the class QueryProfilerIT method testNoProfile.

/**
     * This test makes sure no profile results are returned when profiling is disabled
     */
public void testNoProfile() throws Exception {
    createIndex("test");
    ensureGreen();
    int numDocs = randomIntBetween(100, 150);
    IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
        docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "field2", i);
    }
    indexRandom(true, docs);
    refresh();
    QueryBuilder q = QueryBuilders.rangeQuery("field2").from(0).to(5);
    logger.info("Query: {}", q);
    SearchResponse resp = client().prepareSearch().setQuery(q).setProfile(false).execute().actionGet();
    assertThat("Profile response element should be an empty map", resp.getProfileResults().size(), equalTo(0));
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) RandomQueryGenerator.randomQueryBuilder(org.elasticsearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 82 with IndexRequestBuilder

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

the class FunctionScoreIT method testMinScoreFunctionScoreManyDocsAndRandomMinScore.

public void testMinScoreFunctionScoreManyDocsAndRandomMinScore() throws IOException, ExecutionException, InterruptedException {
    List<IndexRequestBuilder> docs = new ArrayList<>();
    int numDocs = randomIntBetween(1, 100);
    int scoreOffset = randomIntBetween(-2 * numDocs, 2 * numDocs);
    int minScore = randomIntBetween(-2 * numDocs, 2 * numDocs);
    for (int i = 0; i < numDocs; i++) {
        docs.add(client().prepareIndex(INDEX, TYPE, Integer.toString(i)).setSource("num", i + scoreOffset));
    }
    indexRandom(true, docs);
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "return (doc['num'].value)", Collections.emptyMap());
    int numMatchingDocs = numDocs + scoreOffset - minScore;
    if (numMatchingDocs < 0) {
        numMatchingDocs = 0;
    }
    if (numMatchingDocs > numDocs) {
        numMatchingDocs = numDocs;
    }
    SearchResponse searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(scriptFunction(script)).setMinScore(minScore)).size(numDocs))).actionGet();
    assertMinScoreSearchResponses(numDocs, searchResponse, numMatchingDocs);
    searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(new MatchAllQueryBuilder(), new FilterFunctionBuilder[] { new FilterFunctionBuilder(scriptFunction(script)), new FilterFunctionBuilder(scriptFunction(script)) }).scoreMode(FiltersFunctionScoreQuery.ScoreMode.AVG).setMinScore(minScore)).size(numDocs))).actionGet();
    assertMinScoreSearchResponses(numDocs, searchResponse, numMatchingDocs);
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Script(org.elasticsearch.script.Script) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder) ArrayList(java.util.ArrayList) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 83 with IndexRequestBuilder

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

the class MoreLikeThisIT method testMoreLikeThisMultiValueFields.

public void testMoreLikeThisMultiValueFields() throws Exception {
    logger.info("Creating the index ...");
    assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text,analyzer=keyword").setSettings(SETTING_NUMBER_OF_SHARDS, 1));
    ensureGreen();
    logger.info("Indexing ...");
    String[] values = { "aaaa", "bbbb", "cccc", "dddd", "eeee", "ffff", "gggg", "hhhh", "iiii", "jjjj" };
    List<IndexRequestBuilder> builders = new ArrayList<>(values.length + 1);
    // index one document with all the values
    builders.add(client().prepareIndex("test", "type1", "0").setSource("text", values));
    // index each document with only one of the values
    for (int i = 0; i < values.length; i++) {
        builders.add(client().prepareIndex("test", "type1", String.valueOf(i + 1)).setSource("text", values[i]));
    }
    indexRandom(true, builders);
    int maxIters = randomIntBetween(10, 20);
    for (int i = 0; i < maxIters; i++) {
        int max_query_terms = randomIntBetween(1, values.length);
        logger.info("Running More Like This with max_query_terms = {}", max_query_terms);
        MoreLikeThisQueryBuilder mltQuery = moreLikeThisQuery(new String[] { "text" }, null, new Item[] { new Item(null, null, "0") }).minTermFreq(1).minDocFreq(1).maxQueryTerms(max_query_terms).minimumShouldMatch("0%");
        SearchResponse response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).execute().actionGet();
        assertSearchResponse(response);
        assertHitCount(response, max_query_terms);
    }
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Item(org.elasticsearch.index.query.MoreLikeThisQueryBuilder.Item) ArrayList(java.util.ArrayList) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 84 with IndexRequestBuilder

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

the class MoreLikeThisIT method testMinimumShouldMatch.

public void testMinimumShouldMatch() throws ExecutionException, InterruptedException {
    logger.info("Creating the index ...");
    assertAcked(prepareCreate("test").addMapping("type1", "text", "type=text,analyzer=whitespace").setSettings(SETTING_NUMBER_OF_SHARDS, 1));
    ensureGreen();
    logger.info("Indexing with each doc having one less term ...");
    List<IndexRequestBuilder> builders = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        String text = "";
        for (int j = 1; j <= 10 - i; j++) {
            text += j + " ";
        }
        builders.add(client().prepareIndex("test", "type1", i + "").setSource("text", text));
    }
    indexRandom(true, builders);
    logger.info("Testing each minimum_should_match from 0% - 100% with 10% increment ...");
    for (int i = 0; i <= 10; i++) {
        String minimumShouldMatch = (10 * i) + "%";
        MoreLikeThisQueryBuilder mltQuery = moreLikeThisQuery(new String[] { "text" }, new String[] { "1 2 3 4 5 6 7 8 9 10" }, null).minTermFreq(1).minDocFreq(1).minimumShouldMatch(minimumShouldMatch);
        logger.info("Testing with minimum_should_match = {}", minimumShouldMatch);
        SearchResponse response = client().prepareSearch("test").setTypes("type1").setQuery(mltQuery).get();
        assertSearchResponse(response);
        if (minimumShouldMatch.equals("0%")) {
            assertHitCount(response, 10);
        } else {
            assertHitCount(response, 11 - i);
        }
    }
}
Also used : CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ArrayList(java.util.ArrayList) MoreLikeThisQueryBuilder(org.elasticsearch.index.query.MoreLikeThisQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 85 with IndexRequestBuilder

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

the class QueryProfilerIT method testCollapsingBool.

/**
     * Tests a series of three nested boolean queries with a single "leaf" match query.
     * The rewrite process will "collapse" this down to a single bool, so this tests to make sure
     * nothing catastrophic happens during that fairly substantial rewrite
     */
public void testCollapsingBool() throws Exception {
    createIndex("test");
    ensureGreen();
    int numDocs = randomIntBetween(100, 150);
    IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
        docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "field2", i);
    }
    indexRandom(true, docs);
    refresh();
    QueryBuilder q = QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("field1", "one"))));
    logger.info("Query: {}", q);
    SearchResponse resp = client().prepareSearch().setQuery(q).setProfile(true).setSearchType(SearchType.QUERY_THEN_FETCH).execute().actionGet();
    assertNotNull("Profile response element should not be null", resp.getProfileResults());
    assertThat("Profile response should not be an empty array", resp.getProfileResults().size(), not(0));
    for (Map.Entry<String, ProfileShardResult> shardResult : resp.getProfileResults().entrySet()) {
        for (QueryProfileShardResult searchProfiles : shardResult.getValue().getQueryProfileResults()) {
            for (ProfileResult result : searchProfiles.getQueryResults()) {
                assertNotNull(result.getQueryName());
                assertNotNull(result.getLuceneDescription());
                assertThat(result.getTime(), greaterThan(0L));
                assertNotNull(result.getTimeBreakdown());
            }
            CollectorResult result = searchProfiles.getCollectorResult();
            assertThat(result.getName(), not(isEmptyOrNullString()));
            assertThat(result.getTime(), greaterThan(0L));
        }
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ProfileResult(org.elasticsearch.search.profile.ProfileResult) RandomQueryGenerator.randomQueryBuilder(org.elasticsearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Map(java.util.Map) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) ProfileShardResult(org.elasticsearch.search.profile.ProfileShardResult)

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