use of org.elasticsearch.search.SearchHits 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.search.SearchHits in project elasticsearch by elastic.
the class MoreExpressionTests method testDateObjectMethods.
public void testDateObjectMethods() throws Exception {
ElasticsearchAssertions.assertAcked(prepareCreate("test").addMapping("doc", "date0", "type=date", "date1", "type=date"));
ensureGreen("test");
indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("date0", "2015-04-28T04:02:07Z", "date1", "1985-09-01T23:11:01Z"), client().prepareIndex("test", "doc", "2").setSource("date0", "2013-12-25T11:56:45Z", "date1", "1983-10-13T23:15:00Z"));
SearchResponse rsp = buildRequest("doc['date0'].date.secondOfMinute - doc['date0'].date.minuteOfHour").get();
assertEquals(2, rsp.getHits().getTotalHits());
SearchHits hits = rsp.getHits();
assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
assertEquals(-11.0, hits.getAt(1).field("foo").getValue(), 0.0D);
rsp = buildRequest("doc['date0'].date.getHourOfDay() + doc['date1'].date.dayOfMonth").get();
assertEquals(2, rsp.getHits().getTotalHits());
hits = rsp.getHits();
assertEquals(5.0, hits.getAt(0).field("foo").getValue(), 0.0D);
assertEquals(24.0, hits.getAt(1).field("foo").getValue(), 0.0D);
rsp = buildRequest("doc['date1'].date.monthOfYear + 1").get();
assertEquals(2, rsp.getHits().getTotalHits());
hits = rsp.getHits();
assertEquals(10.0, hits.getAt(0).field("foo").getValue(), 0.0D);
assertEquals(11.0, hits.getAt(1).field("foo").getValue(), 0.0D);
rsp = buildRequest("doc['date1'].date.year").get();
assertEquals(2, rsp.getHits().getTotalHits());
hits = rsp.getHits();
assertEquals(1985.0, hits.getAt(0).field("foo").getValue(), 0.0D);
assertEquals(1983.0, hits.getAt(1).field("foo").getValue(), 0.0D);
}
use of org.elasticsearch.search.SearchHits in project elasticsearch by elastic.
the class SearchPhaseController method getHits.
private SearchHits getHits(ReducedQueryPhase reducedQueryPhase, boolean ignoreFrom, ScoreDoc[] sortedDocs, AtomicArray<? extends QuerySearchResultProvider> fetchResultsArr) {
List<? extends AtomicArray.Entry<? extends QuerySearchResultProvider>> fetchResults = fetchResultsArr.asList();
boolean sorted = false;
int sortScoreIndex = -1;
if (reducedQueryPhase.oneResult.topDocs() instanceof TopFieldDocs) {
TopFieldDocs fieldDocs = (TopFieldDocs) reducedQueryPhase.oneResult.queryResult().topDocs();
if (fieldDocs instanceof CollapseTopFieldDocs && fieldDocs.fields.length == 1 && fieldDocs.fields[0].getType() == SortField.Type.SCORE) {
sorted = false;
} else {
sorted = true;
for (int i = 0; i < fieldDocs.fields.length; i++) {
if (fieldDocs.fields[i].getType() == SortField.Type.SCORE) {
sortScoreIndex = i;
}
}
}
}
// clean the fetch counter
for (AtomicArray.Entry<? extends QuerySearchResultProvider> entry : fetchResults) {
entry.value.fetchResult().initCounter();
}
int from = ignoreFrom ? 0 : reducedQueryPhase.oneResult.queryResult().from();
int numSearchHits = (int) Math.min(reducedQueryPhase.fetchHits - from, reducedQueryPhase.oneResult.size());
// with collapsing we can have more fetch hits than sorted docs
numSearchHits = Math.min(sortedDocs.length, numSearchHits);
// merge hits
List<SearchHit> hits = new ArrayList<>();
if (!fetchResults.isEmpty()) {
for (int i = 0; i < numSearchHits; i++) {
ScoreDoc shardDoc = sortedDocs[i];
QuerySearchResultProvider fetchResultProvider = fetchResultsArr.get(shardDoc.shardIndex);
if (fetchResultProvider == null) {
continue;
}
FetchSearchResult fetchResult = fetchResultProvider.fetchResult();
int index = fetchResult.counterGetAndIncrement();
if (index < fetchResult.hits().internalHits().length) {
SearchHit searchHit = fetchResult.hits().internalHits()[index];
searchHit.score(shardDoc.score);
searchHit.shard(fetchResult.shardTarget());
if (sorted) {
FieldDoc fieldDoc = (FieldDoc) shardDoc;
searchHit.sortValues(fieldDoc.fields, reducedQueryPhase.oneResult.sortValueFormats());
if (sortScoreIndex != -1) {
searchHit.score(((Number) fieldDoc.fields[sortScoreIndex]).floatValue());
}
}
hits.add(searchHit);
}
}
}
return new SearchHits(hits.toArray(new SearchHit[hits.size()]), reducedQueryPhase.totalHits, reducedQueryPhase.maxScore);
}
use of org.elasticsearch.search.SearchHits 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.elasticsearch.search.SearchHits 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