use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.
the class TopHitsIT method testTrackScores.
public void testTrackScores() throws Exception {
boolean[] trackScores = new boolean[] { true, false };
for (boolean trackScore : trackScores) {
logger.info("Track score={}", trackScore);
SearchResponse response = client().prepareSearch("idx").setTypes("field-collapsing").setQuery(matchQuery("text", "term rare")).addAggregation(terms("terms").field("group").subAggregation(topHits("hits").trackScores(trackScore).size(1).sort("_uid", SortOrder.DESC))).get();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(3));
Terms.Bucket bucket = terms.getBucketByKey("a");
assertThat(key(bucket), equalTo("a"));
TopHits topHits = bucket.getAggregations().get("hits");
SearchHits hits = topHits.getHits();
assertThat(hits.getMaxScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
assertThat(hits.getAt(0).getScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
bucket = terms.getBucketByKey("b");
assertThat(key(bucket), equalTo("b"));
topHits = bucket.getAggregations().get("hits");
hits = topHits.getHits();
assertThat(hits.getMaxScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
assertThat(hits.getAt(0).getScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
bucket = terms.getBucketByKey("c");
assertThat(key(bucket), equalTo("c"));
topHits = bucket.getAggregations().get("hits");
hits = topHits.getHits();
assertThat(hits.getMaxScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
assertThat(hits.getAt(0).getScore(), trackScore ? not(equalTo(Float.NaN)) : equalTo(Float.NaN));
}
}
use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.
the class TopHitsIT method testTopHitsInNestedSimple.
public void testTopHitsInNestedSimple() throws Exception {
SearchResponse searchResponse = client().prepareSearch("articles").setQuery(matchQuery("title", "title")).addAggregation(nested("to-comments", "comments").subAggregation(terms("users").field("comments.user").subAggregation(topHits("top-comments").sort("comments.date", SortOrder.ASC)))).get();
Nested nested = searchResponse.getAggregations().get("to-comments");
assertThat(nested.getDocCount(), equalTo(4L));
Terms terms = nested.getAggregations().get("users");
Terms.Bucket bucket = terms.getBucketByKey("a");
assertThat(bucket.getDocCount(), equalTo(1L));
TopHits topHits = bucket.getAggregations().get("top-comments");
SearchHits searchHits = topHits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(1L));
assertThat(searchHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(0).getNestedIdentity().getOffset(), equalTo(0));
assertThat((Integer) searchHits.getAt(0).getSourceAsMap().get("date"), equalTo(1));
bucket = terms.getBucketByKey("b");
assertThat(bucket.getDocCount(), equalTo(2L));
topHits = bucket.getAggregations().get("top-comments");
searchHits = topHits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(2L));
assertThat(searchHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(0).getNestedIdentity().getOffset(), equalTo(1));
assertThat((Integer) searchHits.getAt(0).getSourceAsMap().get("date"), equalTo(2));
assertThat(searchHits.getAt(1).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(1).getNestedIdentity().getOffset(), equalTo(0));
assertThat((Integer) searchHits.getAt(1).getSourceAsMap().get("date"), equalTo(3));
bucket = terms.getBucketByKey("c");
assertThat(bucket.getDocCount(), equalTo(1L));
topHits = bucket.getAggregations().get("top-comments");
searchHits = topHits.getHits();
assertThat(searchHits.getTotalHits(), equalTo(1L));
assertThat(searchHits.getAt(0).getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHits.getAt(0).getNestedIdentity().getOffset(), equalTo(1));
assertThat((Integer) searchHits.getAt(0).getSourceAsMap().get("date"), equalTo(4));
}
use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.
the class DuelScrollIT method testDuelQueryThenFetch.
public void testDuelQueryThenFetch() throws Exception {
TestContext context = create(SearchType.DFS_QUERY_THEN_FETCH, SearchType.QUERY_THEN_FETCH);
SearchResponse control = client().prepareSearch("index").setSearchType(context.searchType).addSort(context.sort).setSize(context.numDocs).get();
assertNoFailures(control);
SearchHits sh = control.getHits();
assertThat(sh.getTotalHits(), equalTo((long) context.numDocs));
assertThat(sh.getHits().length, equalTo(context.numDocs));
SearchResponse searchScrollResponse = client().prepareSearch("index").setSearchType(context.searchType).addSort(context.sort).setSize(context.scrollRequestSize).setScroll("10m").get();
assertNoFailures(searchScrollResponse);
assertThat(searchScrollResponse.getHits().getTotalHits(), equalTo((long) context.numDocs));
assertThat(searchScrollResponse.getHits().getHits().length, equalTo(context.scrollRequestSize));
int counter = 0;
for (SearchHit hit : searchScrollResponse.getHits()) {
assertThat(hit.getSortValues()[0], equalTo(sh.getAt(counter++).getSortValues()[0]));
}
int iter = 1;
String scrollId = searchScrollResponse.getScrollId();
while (true) {
searchScrollResponse = client().prepareSearchScroll(scrollId).setScroll("10m").get();
assertNoFailures(searchScrollResponse);
assertThat(searchScrollResponse.getHits().getTotalHits(), equalTo((long) context.numDocs));
if (searchScrollResponse.getHits().getHits().length == 0) {
break;
}
int expectedLength;
int scrollSlice = ++iter * context.scrollRequestSize;
if (scrollSlice <= context.numDocs) {
expectedLength = context.scrollRequestSize;
} else {
expectedLength = context.scrollRequestSize - (scrollSlice - context.numDocs);
}
assertThat(searchScrollResponse.getHits().getHits().length, equalTo(expectedLength));
for (SearchHit hit : searchScrollResponse.getHits()) {
assertThat(hit.getSortValues()[0], equalTo(sh.getAt(counter++).getSortValues()[0]));
}
scrollId = searchScrollResponse.getScrollId();
}
assertThat(counter, equalTo(context.numDocs));
clearScroll(scrollId);
}
use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.
the class RelocationIT method testRelocationWhileIndexingRandom.
@TestLogging("org.elasticsearch.action.bulk:TRACE,org.elasticsearch.action.search:TRACE")
public void testRelocationWhileIndexingRandom() throws Exception {
int numberOfRelocations = scaledRandomIntBetween(1, rarely() ? 10 : 4);
int numberOfReplicas = randomBoolean() ? 0 : 1;
int numberOfNodes = numberOfReplicas == 0 ? 2 : 3;
logger.info("testRelocationWhileIndexingRandom(numRelocations={}, numberOfReplicas={}, numberOfNodes={})", numberOfRelocations, numberOfReplicas, numberOfNodes);
String[] nodes = new String[numberOfNodes];
logger.info("--> starting [node1] ...");
nodes[0] = internalCluster().startNode();
logger.info("--> creating test index ...");
prepareCreate("test", Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", numberOfReplicas)).get();
for (int i = 2; i <= numberOfNodes; i++) {
logger.info("--> starting [node{}] ...", i);
nodes[i - 1] = internalCluster().startNode();
if (i != numberOfNodes) {
ClusterHealthResponse healthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNodes(Integer.toString(i)).setWaitForGreenStatus().execute().actionGet();
assertThat(healthResponse.isTimedOut(), equalTo(false));
}
}
int numDocs = scaledRandomIntBetween(200, 2500);
try (BackgroundIndexer indexer = new BackgroundIndexer("test", "type1", client(), numDocs)) {
logger.info("--> waiting for {} docs to be indexed ...", numDocs);
waitForDocs(numDocs, indexer);
logger.info("--> {} docs indexed", numDocs);
logger.info("--> starting relocations...");
// if we have replicas shift those
int nodeShiftBased = numberOfReplicas;
for (int i = 0; i < numberOfRelocations; i++) {
int fromNode = (i % 2);
int toNode = fromNode == 0 ? 1 : 0;
fromNode += nodeShiftBased;
toNode += nodeShiftBased;
numDocs = scaledRandomIntBetween(200, 1000);
logger.debug("--> Allow indexer to index [{}] documents", numDocs);
indexer.continueIndexing(numDocs);
logger.info("--> START relocate the shard from {} to {}", nodes[fromNode], nodes[toNode]);
client().admin().cluster().prepareReroute().add(new MoveAllocationCommand("test", 0, nodes[fromNode], nodes[toNode])).get();
if (rarely()) {
logger.debug("--> flushing");
client().admin().indices().prepareFlush().get();
}
ClusterHealthResponse clusterHealthResponse = client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForNoRelocatingShards(true).setTimeout(ACCEPTABLE_RELOCATION_TIME).execute().actionGet();
assertThat(clusterHealthResponse.isTimedOut(), equalTo(false));
indexer.pauseIndexing();
logger.info("--> DONE relocate the shard from {} to {}", fromNode, toNode);
}
logger.info("--> done relocations");
logger.info("--> waiting for indexing threads to stop ...");
indexer.stop();
logger.info("--> indexing threads stopped");
logger.info("--> refreshing the index");
client().admin().indices().prepareRefresh("test").execute().actionGet();
logger.info("--> searching the index");
boolean ranOnce = false;
for (int i = 0; i < 10; i++) {
logger.info("--> START search test round {}", i + 1);
SearchHits hits = client().prepareSearch("test").setQuery(matchAllQuery()).setSize((int) indexer.totalIndexedDocs()).storedFields().execute().actionGet().getHits();
ranOnce = true;
if (hits.getTotalHits() != indexer.totalIndexedDocs()) {
int[] hitIds = new int[(int) indexer.totalIndexedDocs()];
for (int hit = 0; hit < indexer.totalIndexedDocs(); hit++) {
hitIds[hit] = hit + 1;
}
IntHashSet set = IntHashSet.from(hitIds);
for (SearchHit hit : hits.getHits()) {
int id = Integer.parseInt(hit.getId());
if (!set.remove(id)) {
logger.error("Extra id [{}]", id);
}
}
set.forEach((IntProcedure) value -> {
logger.error("Missing id [{}]", value);
});
}
assertThat(hits.getTotalHits(), equalTo(indexer.totalIndexedDocs()));
logger.info("--> DONE search test round {}", i + 1);
}
if (!ranOnce) {
fail();
}
}
}
use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.
the class MultiMatchQueryIT method assertEquivalent.
private static void assertEquivalent(String query, SearchResponse left, SearchResponse right) {
assertNoFailures(left);
assertNoFailures(right);
SearchHits leftHits = left.getHits();
SearchHits rightHits = right.getHits();
assertThat(leftHits.getTotalHits(), equalTo(rightHits.getTotalHits()));
assertThat(leftHits.getHits().length, equalTo(rightHits.getHits().length));
SearchHit[] hits = leftHits.getHits();
SearchHit[] rHits = rightHits.getHits();
for (int i = 0; i < hits.length; i++) {
assertThat("query: " + query + " hit: " + i, (double) hits[i].getScore(), closeTo(rHits[i].getScore(), 0.00001d));
}
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: " + query, hits[i].getId(), equalTo(rHits[i].getId()));
}
}
Aggregations