use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.
the class QueryProfilerIT method testProfileMatchesRegular.
/**
* This test generates 1-10 random queries and executes a profiled and non-profiled
* search for each query. It then does some basic sanity checking of score and hits
* to make sure the profiling doesn't interfere with the hits being returned
*/
public void testProfileMatchesRegular() 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);
}
List<String> stringFields = Arrays.asList("field1");
List<String> numericFields = Arrays.asList("field2");
indexRandom(true, docs);
refresh();
int iters = between(1, 10);
for (int i = 0; i < iters; i++) {
QueryBuilder q = randomQueryBuilder(stringFields, numericFields, numDocs, 3);
logger.info("Query: {}", q);
SearchRequestBuilder vanilla = client().prepareSearch("test").setQuery(q).setProfile(false).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
SearchRequestBuilder profile = client().prepareSearch("test").setQuery(q).setProfile(true).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
MultiSearchResponse.Item[] responses = client().prepareMultiSearch().add(vanilla).add(profile).execute().actionGet().getResponses();
SearchResponse vanillaResponse = responses[0].getResponse();
SearchResponse profileResponse = responses[1].getResponse();
float vanillaMaxScore = vanillaResponse.getHits().getMaxScore();
float profileMaxScore = profileResponse.getHits().getMaxScore();
if (Float.isNaN(vanillaMaxScore)) {
assertTrue("Vanilla maxScore is NaN but Profile is not [" + profileMaxScore + "]", Float.isNaN(profileMaxScore));
} else {
assertTrue("Profile maxScore of [" + profileMaxScore + "] is not close to Vanilla maxScore [" + vanillaMaxScore + "]", nearlyEqual(vanillaMaxScore, profileMaxScore, 0.001));
}
assertThat("Profile totalHits of [" + profileResponse.getHits().getTotalHits() + "] is not close to Vanilla totalHits [" + vanillaResponse.getHits().getTotalHits() + "]", vanillaResponse.getHits().getTotalHits(), equalTo(profileResponse.getHits().getTotalHits()));
SearchHit[] vanillaHits = vanillaResponse.getHits().getHits();
SearchHit[] profileHits = profileResponse.getHits().getHits();
for (int j = 0; j < vanillaHits.length; j++) {
assertThat("Profile hit #" + j + " has a different ID from Vanilla", vanillaHits[j].getId(), equalTo(profileHits[j].getId()));
}
}
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.
the class SimpleSearchIT method assertRescoreWindowFails.
private void assertRescoreWindowFails(int windowSize) {
SearchRequestBuilder search = client().prepareSearch("idx").addRescorer(new QueryRescorerBuilder(matchAllQuery()).windowSize(windowSize));
SearchPhaseExecutionException e = expectThrows(SearchPhaseExecutionException.class, () -> search.get());
assertThat(e.toString(), containsString("Rescore window [" + windowSize + "] is too large. It must " + "be less than [" + IndexSettings.MAX_RESCORE_WINDOW_SETTING.get(Settings.EMPTY)));
assertThat(e.toString(), containsString("This limit can be set by changing the [" + IndexSettings.MAX_RESCORE_WINDOW_SETTING.getKey() + "] index level setting."));
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.
the class MoreExpressionTests method testSpecialValueVariable.
public void testSpecialValueVariable() throws Exception {
// i.e. _value for aggregations
createIndex("test");
ensureGreen("test");
indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("x", 5, "y", 1.2), client().prepareIndex("test", "doc", "2").setSource("x", 10, "y", 1.4), client().prepareIndex("test", "doc", "3").setSource("x", 13, "y", 1.8));
SearchRequestBuilder req = client().prepareSearch().setIndices("test");
req.setQuery(QueryBuilders.matchAllQuery()).addAggregation(AggregationBuilders.stats("int_agg").field("x").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value * 3", Collections.emptyMap()))).addAggregation(AggregationBuilders.stats("double_agg").field("y").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value - 1.1", Collections.emptyMap()))).addAggregation(// specifically to test a script w/o _value
AggregationBuilders.stats("const_agg").field("x").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "3.0", Collections.emptyMap())));
SearchResponse rsp = req.get();
assertEquals(3, rsp.getHits().getTotalHits());
Stats stats = rsp.getAggregations().get("int_agg");
assertEquals(39.0, stats.getMax(), 0.0001);
assertEquals(15.0, stats.getMin(), 0.0001);
stats = rsp.getAggregations().get("double_agg");
assertEquals(0.7, stats.getMax(), 0.0001);
assertEquals(0.1, stats.getMin(), 0.0001);
stats = rsp.getAggregations().get("const_agg");
assertThat(stats.getMax(), equalTo(3.0));
assertThat(stats.getMin(), equalTo(3.0));
assertThat(stats.getAvg(), equalTo(3.0));
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.
the class MoreExpressionTests method testScore.
public void testScore() throws Exception {
createIndex("test");
ensureGreen("test");
indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("text", "hello goodbye"), client().prepareIndex("test", "doc", "2").setSource("text", "hello hello hello goodbye"), client().prepareIndex("test", "doc", "3").setSource("text", "hello hello goodebye"));
ScoreFunctionBuilder<?> score = ScoreFunctionBuilders.scriptFunction(new Script(ScriptType.INLINE, "expression", "1 / _score", Collections.emptyMap()));
SearchRequestBuilder req = client().prepareSearch().setIndices("test");
req.setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("text", "hello"), score).boostMode(CombineFunction.REPLACE));
// make sure DF is consistent
req.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
SearchResponse rsp = req.get();
assertSearchResponse(rsp);
SearchHits hits = rsp.getHits();
assertEquals(3, hits.getTotalHits());
assertEquals("1", hits.getAt(0).getId());
assertEquals("3", hits.getAt(1).getId());
assertEquals("2", hits.getAt(2).getId());
}
use of org.elasticsearch.action.search.SearchRequestBuilder in project elasticsearch by elastic.
the class MoreExpressionTests method buildRequest.
private SearchRequestBuilder buildRequest(String script, Object... params) {
ensureGreen("test");
Map<String, Object> paramsMap = new HashMap<>();
assert (params.length % 2 == 0);
for (int i = 0; i < params.length; i += 2) {
paramsMap.put(params[i].toString(), params[i + 1]);
}
SearchRequestBuilder req = client().prepareSearch().setIndices("test");
req.setQuery(QueryBuilders.matchAllQuery()).addSort(SortBuilders.fieldSort("_uid").order(SortOrder.ASC)).addScriptField("foo", new Script(ScriptType.INLINE, "expression", script, paramsMap));
return req;
}
Aggregations