use of org.elasticsearch.search.SearchHits 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.SearchHits in project elasticsearch by elastic.
the class SearchQueryIT method testConstantScoreQuery.
// see #3521
public void testConstantScoreQuery() throws Exception {
Random random = random();
createIndex("test");
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox"), client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox"));
SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(matchQuery("field1", "quick"))).get();
assertHitCount(searchResponse, 2L);
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
assertSearchHit(searchHit, hasScore(1.0f));
}
searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + random().nextFloat()))).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
client().prepareSearch("test").setQuery(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + random().nextFloat())).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat()))))).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
assertSearchHit(searchHit, hasScore(1.0f));
}
int num = scaledRandomIntBetween(100, 200);
IndexRequestBuilder[] builders = new IndexRequestBuilder[num];
for (int i = 0; i < builders.length; i++) {
builders[i] = client().prepareIndex("test_1", "type", "" + i).setSource("f", English.intToEnglish(i));
}
createIndex("test_1");
indexRandom(true, builders);
int queryRounds = scaledRandomIntBetween(10, 20);
for (int i = 0; i < queryRounds; i++) {
MatchQueryBuilder matchQuery = matchQuery("f", English.intToEnglish(between(0, num)));
searchResponse = client().prepareSearch("test_1").setQuery(constantScoreQuery(matchQuery)).setSize(num).get();
long totalHits = searchResponse.getHits().getTotalHits();
SearchHits hits = searchResponse.getHits();
for (SearchHit searchHit : hits) {
assertSearchHit(searchHit, hasScore(1.0f));
}
searchResponse = client().prepareSearch("test_1").setQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat())))).setSize(num).get();
hits = searchResponse.getHits();
assertThat(hits.getTotalHits(), equalTo(totalHits));
if (totalHits > 1) {
float expected = hits.getAt(0).getScore();
for (SearchHit searchHit : hits) {
assertSearchHit(searchHit, hasScore(expected));
}
}
}
}
use of org.elasticsearch.search.SearchHits in project graylog2-server by Graylog2.
the class ScrollResult method nextChunk.
public ScrollChunk nextChunk() {
final SearchResponse search;
// make sure to return the initial hits, see https://github.com/Graylog2/graylog2-server/issues/2126
if (firstResponse == null) {
search = client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMinutes(1)).execute().actionGet();
} else {
search = firstResponse;
firstResponse = null;
}
final SearchHits hits = search.getHits();
if (hits.getHits().length == 0) {
// scroll exhausted
LOG.debug("[{}] Reached end of scroll results.", queryHash, getOriginalQuery());
return null;
}
LOG.debug("[{}][{}] New scroll id {}, number of hits in chunk: {}", queryHash, chunkId, search.getScrollId(), hits.getHits().length);
// save the id for the next request.
scrollId = search.getScrollId();
return new ScrollChunk(hits, fields, chunkId++);
}
use of org.elasticsearch.search.SearchHits in project titan by thinkaurelius.
the class ElasticSearchIndex method query.
@Override
public Iterable<RawQuery.Result<String>> query(RawQuery query, KeyInformation.IndexRetriever informations, BaseTransaction tx) throws BackendException {
SearchRequestBuilder srb = client.prepareSearch(indexName);
srb.setTypes(query.getStore());
srb.setQuery(QueryBuilders.queryStringQuery(query.getQuery()));
srb.setFrom(query.getOffset());
if (query.hasLimit())
srb.setSize(query.getLimit());
else
srb.setSize(maxResultsSize);
srb.setNoFields();
//srb.setExplain(true);
SearchResponse response = srb.execute().actionGet();
log.debug("Executed query [{}] in {} ms", query.getQuery(), response.getTookInMillis());
SearchHits hits = response.getHits();
if (!query.hasLimit() && hits.totalHits() >= maxResultsSize)
log.warn("Query result set truncated to first [{}] elements for query: {}", maxResultsSize, query);
List<RawQuery.Result<String>> result = new ArrayList<RawQuery.Result<String>>(hits.hits().length);
for (SearchHit hit : hits) {
result.add(new RawQuery.Result<String>(hit.id(), hit.getScore()));
}
return result;
}
use of org.elasticsearch.search.SearchHits in project fess by codelibs.
the class FessEsClient method deleteByQuery.
public int deleteByQuery(final String index, final String type, final QueryBuilder queryBuilder) {
final FessConfig fessConfig = ComponentUtil.getFessConfig();
SearchResponse response = client.prepareSearch(index).setTypes(type).setScroll(scrollForDelete).setSize(sizeForDelete).setFetchSource(new String[] { fessConfig.getIndexFieldId() }, null).setQuery(queryBuilder).setPreference(Constants.SEARCH_PREFERENCE_PRIMARY).execute().actionGet(fessConfig.getIndexScrollSearchTimeoutTimeout());
int count = 0;
String scrollId = response.getScrollId();
while (scrollId != null) {
final SearchHits searchHits = response.getHits();
final SearchHit[] hits = searchHits.getHits();
if (hits.length == 0) {
scrollId = null;
break;
}
final BulkRequestBuilder bulkRequest = client.prepareBulk();
for (final SearchHit hit : hits) {
bulkRequest.add(client.prepareDelete(index, type, hit.getId()));
}
count += hits.length;
final BulkResponse bulkResponse = bulkRequest.execute().actionGet(fessConfig.getIndexBulkTimeout());
if (bulkResponse.hasFailures()) {
throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
}
response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(fessConfig.getIndexBulkTimeout());
scrollId = response.getScrollId();
}
return count;
}
Aggregations