use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class QueryRescorerIT method assertEquivalentOrSubstringMatch.
private static void assertEquivalentOrSubstringMatch(String query, SearchResponse plain, SearchResponse rescored) {
assertNoFailures(plain);
assertNoFailures(rescored);
SearchHits leftHits = plain.getHits();
SearchHits rightHits = rescored.getHits();
assertThat(leftHits.getTotalHits(), equalTo(rightHits.getTotalHits()));
assertThat(leftHits.getHits().length, equalTo(rightHits.getHits().length));
SearchHit[] hits = leftHits.getHits();
SearchHit[] otherHits = rightHits.getHits();
if (!hits[0].getId().equals(otherHits[0].getId())) {
assertThat(((String) otherHits[0].getSourceAsMap().get("field1")).contains(query), equalTo(true));
} else {
Arrays.sort(hits, searchHitsComparator);
Arrays.sort(otherHits, searchHitsComparator);
for (int i = 0; i < hits.length; i++) {
if (hits[i].getScore() == hits[hits.length - 1].getScore()) {
// we need to cut off here since this is the tail of the queue and we might not have fetched enough docs
return;
}
assertThat(query, hits[i].getId(), equalTo(rightHits.getHits()[i].getId()));
}
}
}
use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class GeoPolygonIT method testSimplePolygon.
public void testSimplePolygon() throws Exception {
List<GeoPoint> points = new ArrayList<>();
points.add(new GeoPoint(40.7, -74.0));
points.add(new GeoPoint(40.7, -74.1));
points.add(new GeoPoint(40.8, -74.1));
points.add(new GeoPoint(40.8, -74.0));
points.add(new GeoPoint(40.7, -74.0));
SearchResponse searchResponse = // from NY
client().prepareSearch("test").setQuery(boolQuery().must(geoPolygonQuery("location", points))).execute().actionGet();
assertHitCount(searchResponse, 4);
assertThat(searchResponse.getHits().getHits().length, equalTo(4));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("4"), equalTo("5")));
}
}
use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class GeoPolygonIT method testSimpleUnclosedPolygon.
public void testSimpleUnclosedPolygon() throws Exception {
List<GeoPoint> points = new ArrayList<>();
points.add(new GeoPoint(40.7, -74.0));
points.add(new GeoPoint(40.7, -74.1));
points.add(new GeoPoint(40.8, -74.1));
points.add(new GeoPoint(40.8, -74.0));
SearchResponse searchResponse = // from NY
client().prepareSearch("test").setQuery(boolQuery().must(geoPolygonQuery("location", points))).execute().actionGet();
assertHitCount(searchResponse, 4);
assertThat(searchResponse.getHits().getHits().length, equalTo(4));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("4"), equalTo("5")));
}
}
use of org.elasticsearch.search.SearchHit 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.search.SearchHit in project elasticsearch by elastic.
the class RandomScoreFunctionIT method testSeedReportedInExplain.
public void testSeedReportedInExplain() throws Exception {
createIndex("test");
ensureGreen();
index("test", "type", "1", jsonBuilder().startObject().endObject());
flush();
refresh();
int seed = 12345678;
SearchResponse resp = client().prepareSearch("test").setQuery(functionScoreQuery(matchAllQuery(), randomFunction(seed))).setExplain(true).get();
assertNoFailures(resp);
assertEquals(1, resp.getHits().getTotalHits());
SearchHit firstHit = resp.getHits().getAt(0);
assertThat(firstHit.getExplanation().toString(), containsString("" + seed));
}
Aggregations