use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project sonarqube by SonarSource.
the class ComponentIndex method bucketToQualifier.
private static ComponentsPerQualifier bucketToQualifier(Bucket bucket) {
InternalTopHits docs = bucket.getAggregations().get(DOCS_AGGREGATION_NAME);
SearchHits hitList = docs.getHits();
SearchHit[] hits = hitList.getHits();
List<String> componentUuids = Arrays.stream(hits).map(SearchHit::getId).collect(Collectors.toList(hits.length));
return new ComponentsPerQualifier(bucket.getKey(), componentUuids, hitList.totalHits());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project sonarqube by SonarSource.
the class BulkIndexer method addDeletion.
public void addDeletion(SearchRequestBuilder searchRequest) {
searchRequest.addSort("_doc", SortOrder.ASC).setScroll(TimeValue.timeValueMinutes(5)).setSize(100).setFetchSource(false);
// this search is synchronous. An optimization would be to be non-blocking,
// but it requires to tracking pending requests in close().
// Same semaphore can't be reused because of potential deadlock (requires to acquire
// two locks)
SearchResponse searchResponse = searchRequest.get();
while (true) {
SearchHit[] hits = searchResponse.getHits().getHits();
for (SearchHit hit : hits) {
SearchHitField routing = hit.field("_routing");
DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete(hit.index(), hit.type(), hit.getId());
if (routing != null) {
deleteRequestBuilder.setRouting(routing.getValue());
}
add(deleteRequestBuilder.request());
}
String scrollId = searchResponse.getScrollId();
searchResponse = client.prepareSearchScroll(scrollId).setScroll(TimeValue.timeValueMinutes(5)).get();
if (hits.length == 0) {
client.nativeClient().prepareClearScroll().addScrollId(scrollId).get();
break;
}
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class ScriptFieldIT method testNativeScript.
public void testNativeScript() throws InterruptedException, ExecutionException {
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("text", "doc1"), client().prepareIndex("test", "type1", "2").setSource("text", "doc2"), client().prepareIndex("test", "type1", "3").setSource("text", "doc3"), client().prepareIndex("test", "type1", "4").setSource("text", "doc4"), client().prepareIndex("test", "type1", "5").setSource("text", "doc5"), client().prepareIndex("test", "type1", "6").setSource("text", "doc6"));
client().admin().indices().prepareFlush("test").execute().actionGet();
SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("int", new Script(ScriptType.INLINE, "native", "int", Collections.emptyMap())).addScriptField("float", new Script(ScriptType.INLINE, "native", "float", Collections.emptyMap())).addScriptField("double", new Script(ScriptType.INLINE, "native", "double", Collections.emptyMap())).addScriptField("long", new Script(ScriptType.INLINE, "native", "long", Collections.emptyMap())).execute().actionGet();
assertThat(sr.getHits().getHits().length, equalTo(6));
for (SearchHit hit : sr.getHits().getHits()) {
Object result = hit.getFields().get("int").getValues().get(0);
assertThat(result, equalTo((Object) intArray));
result = hit.getFields().get("long").getValues().get(0);
assertThat(result, equalTo((Object) longArray));
result = hit.getFields().get("float").getValues().get(0);
assertThat(result, equalTo((Object) floatArray));
result = hit.getFields().get("double").getValues().get(0);
assertThat(result, equalTo((Object) doubleArray));
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class ChildrenIT method testParentWithMultipleBuckets.
public void testParentWithMultipleBuckets() throws Exception {
SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchQuery("randomized", false)).addAggregation(terms("category").field("category").size(10000).subAggregation(children("to_comment", "comment").subAggregation(topHits("top_comments").sort("_uid", SortOrder.ASC)))).get();
assertSearchResponse(searchResponse);
Terms categoryTerms = searchResponse.getAggregations().get("category");
assertThat(categoryTerms.getBuckets().size(), equalTo(3));
for (Terms.Bucket bucket : categoryTerms.getBuckets()) {
logger.info("bucket={}", bucket.getKey());
Children childrenBucket = bucket.getAggregations().get("to_comment");
TopHits topHits = childrenBucket.getAggregations().get("top_comments");
logger.info("total_hits={}", topHits.getHits().getTotalHits());
for (SearchHit searchHit : topHits.getHits()) {
logger.info("hit= {} {} {}", searchHit.getSortValues()[0], searchHit.getType(), searchHit.getId());
}
}
Terms.Bucket categoryBucket = categoryTerms.getBucketByKey("a");
assertThat(categoryBucket.getKeyAsString(), equalTo("a"));
assertThat(categoryBucket.getDocCount(), equalTo(3L));
Children childrenBucket = categoryBucket.getAggregations().get("to_comment");
assertThat(childrenBucket.getName(), equalTo("to_comment"));
assertThat(childrenBucket.getDocCount(), equalTo(2L));
TopHits topHits = childrenBucket.getAggregations().get("top_comments");
assertThat(topHits.getHits().getTotalHits(), equalTo(2L));
assertThat(topHits.getHits().getAt(0).getId(), equalTo("a"));
assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
assertThat(topHits.getHits().getAt(1).getId(), equalTo("c"));
assertThat(topHits.getHits().getAt(1).getType(), equalTo("comment"));
categoryBucket = categoryTerms.getBucketByKey("b");
assertThat(categoryBucket.getKeyAsString(), equalTo("b"));
assertThat(categoryBucket.getDocCount(), equalTo(2L));
childrenBucket = categoryBucket.getAggregations().get("to_comment");
assertThat(childrenBucket.getName(), equalTo("to_comment"));
assertThat(childrenBucket.getDocCount(), equalTo(1L));
topHits = childrenBucket.getAggregations().get("top_comments");
assertThat(topHits.getHits().getTotalHits(), equalTo(1L));
assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
categoryBucket = categoryTerms.getBucketByKey("c");
assertThat(categoryBucket.getKeyAsString(), equalTo("c"));
assertThat(categoryBucket.getDocCount(), equalTo(2L));
childrenBucket = categoryBucket.getAggregations().get("to_comment");
assertThat(childrenBucket.getName(), equalTo("to_comment"));
assertThat(childrenBucket.getDocCount(), equalTo(1L));
topHits = childrenBucket.getAggregations().get("top_comments");
assertThat(topHits.getHits().getTotalHits(), equalTo(1L));
assertThat(topHits.getHits().getAt(0).getId(), equalTo("c"));
assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class TopHitsIT method testNestedFetchFeatures.
public void testNestedFetchFeatures() {
String hlType = randomFrom("plain", "fvh", "postings");
HighlightBuilder.Field hlField = new HighlightBuilder.Field("comments.message").highlightQuery(matchQuery("comments.message", "comment")).forceSource(// randomly from stored field or _source
randomBoolean()).highlighterType(hlType);
SearchResponse searchResponse = client().prepareSearch("articles").setQuery(nestedQuery("comments", matchQuery("comments.message", "comment").queryName("test"), ScoreMode.Avg)).addAggregation(nested("to-comments", "comments").subAggregation(topHits("top-comments").size(1).highlighter(new HighlightBuilder().field(hlField)).explain(true).fieldDataField("comments.user").scriptField("script", new Script(ScriptType.INLINE, MockScriptEngine.NAME, "5", Collections.emptyMap())).fetchSource("comments.message", null).version(true).sort("comments.date", SortOrder.ASC))).get();
assertHitCount(searchResponse, 2);
Nested nested = searchResponse.getAggregations().get("to-comments");
assertThat(nested.getDocCount(), equalTo(4L));
SearchHits hits = ((TopHits) nested.getAggregations().get("top-comments")).getHits();
assertThat(hits.getTotalHits(), equalTo(4L));
SearchHit searchHit = hits.getAt(0);
assertThat(searchHit.getId(), equalTo("1"));
assertThat(searchHit.getNestedIdentity().getField().string(), equalTo("comments"));
assertThat(searchHit.getNestedIdentity().getOffset(), equalTo(0));
HighlightField highlightField = searchHit.getHighlightFields().get("comments.message");
assertThat(highlightField.getFragments().length, equalTo(1));
assertThat(highlightField.getFragments()[0].string(), equalTo("some <em>comment</em>"));
// Can't explain nested hit with the main query, since both are in a different scopes, also the nested doc may not even have matched with the main query
// If top_hits would have a query option then we can explain that query
Explanation explanation = searchHit.getExplanation();
assertFalse(explanation.isMatch());
// Returns the version of the root document. Nested docs don't have a separate version
long version = searchHit.getVersion();
assertThat(version, equalTo(1L));
assertThat(searchHit.getMatchedQueries(), arrayContaining("test"));
SearchHitField field = searchHit.field("comments.user");
assertThat(field.getValue().toString(), equalTo("a"));
field = searchHit.field("script");
assertThat(field.getValue().toString(), equalTo("5"));
assertThat(searchHit.getSourceAsMap().size(), equalTo(1));
assertThat(XContentMapValues.extractValue("comments.message", searchHit.getSourceAsMap()), equalTo("some comment"));
}
Aggregations