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));
}
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);
}
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);
}
}
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);
}
}
}
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));
}
}
}
Aggregations