use of org.opensearch.search.sort.ScoreSortBuilder in project OpenSearch by opensearch-project.
the class CCSDuelIT method testFieldCollapsingSortByField.
public void testFieldCollapsingSortByField() throws Exception {
assumeMultiClusterSetup();
SearchRequest searchRequest = initSearchRequest();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
searchRequest.source(sourceBuilder);
sourceBuilder.query(QueryBuilders.matchQuery("tags", "ruby"));
sourceBuilder.sort("creationDate", SortOrder.DESC);
sourceBuilder.sort(new ScoreSortBuilder());
sourceBuilder.collapse(new CollapseBuilder("user.keyword"));
duelSearch(searchRequest, response -> {
assertHits(response);
assertEquals(2, response.getHits().getHits()[0].getSortValues().length);
});
}
use of org.opensearch.search.sort.ScoreSortBuilder in project OpenSearch by opensearch-project.
the class TermsAggregatorTests method testWithNestedAggregations.
public void testWithNestedAggregations() throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
for (int i = 0; i < 10; i++) {
int[] nestedValues = new int[i];
for (int j = 0; j < i; j++) {
nestedValues[j] = j;
}
indexWriter.addDocuments(generateDocsWithNested(Integer.toString(i), i, nestedValues));
}
indexWriter.commit();
for (Aggregator.SubAggCollectionMode mode : Aggregator.SubAggCollectionMode.values()) {
for (boolean withScore : new boolean[] { true, false }) {
NestedAggregationBuilder nested = new NestedAggregationBuilder("nested", "nested_object").subAggregation(new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.LONG).field("nested_value").collectMode(mode).order(BucketOrder.key(true)).subAggregation(new TopHitsAggregationBuilder("top_hits").sort(withScore ? new ScoreSortBuilder() : new FieldSortBuilder("_doc")).storedField("_none_")));
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG);
try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
{
InternalNested result = searchAndReduce(newSearcher(indexReader, false, true), // match root document only
new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType);
InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
assertNestedTopHitsScore(terms, withScore);
}
{
FilterAggregationBuilder filter = new FilterAggregationBuilder("filter", new MatchAllQueryBuilder()).subAggregation(nested);
InternalFilter result = searchAndReduce(newSearcher(indexReader, false, true), // match root document only
new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), filter, fieldType);
InternalNested nestedResult = result.getAggregations().get("nested");
InternalMultiBucketAggregation<?, ?> terms = nestedResult.getAggregations().get("terms");
assertNestedTopHitsScore(terms, withScore);
}
}
}
}
}
}
}
use of org.opensearch.search.sort.ScoreSortBuilder in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testSearch.
@SuppressWarnings({ "unused", "unchecked" })
public void testSearch() throws Exception {
indexSearchTestData();
RestHighLevelClient client = highLevelClient();
{
// tag::search-request-basic
// <1>
SearchRequest searchRequest = new SearchRequest();
// <2>
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// <3>
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
// <4>
searchRequest.source(searchSourceBuilder);
// end::search-request-basic
}
{
// tag::search-request-indices
// <1>
SearchRequest searchRequest = new SearchRequest("posts");
// end::search-request-indices
// tag::search-request-routing
// <1>
searchRequest.routing("routing");
// end::search-request-routing
// tag::search-request-indicesOptions
// <1>
searchRequest.indicesOptions(IndicesOptions.lenientExpandOpen());
// end::search-request-indicesOptions
// tag::search-request-preference
// <1>
searchRequest.preference("_local");
// end::search-request-preference
assertNotNull(client.search(searchRequest, RequestOptions.DEFAULT));
}
{
// tag::search-source-basics
// <1>
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// <2>
sourceBuilder.query(QueryBuilders.termQuery("user", "foobar"));
// <3>
sourceBuilder.from(0);
// <4>
sourceBuilder.size(5);
// <5>
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
// end::search-source-basics
// tag::search-source-sorting
// <1>
sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
// <2>
sourceBuilder.sort(new FieldSortBuilder("id").order(SortOrder.ASC));
// end::search-source-sorting
// tag::search-source-filtering-off
sourceBuilder.fetchSource(false);
// end::search-source-filtering-off
// tag::search-source-filtering-includes
String[] includeFields = new String[] { "title", "innerObject.*" };
String[] excludeFields = new String[] { "user" };
sourceBuilder.fetchSource(includeFields, excludeFields);
// end::search-source-filtering-includes
sourceBuilder.fetchSource(true);
// tag::search-source-setter
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("posts");
searchRequest.source(sourceBuilder);
// end::search-source-setter
// tag::search-execute
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// end::search-execute
// tag::search-execute-listener
ActionListener<SearchResponse> listener = new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse searchResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::search-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::search-execute-async
// <1>
client.searchAsync(searchRequest, RequestOptions.DEFAULT, listener);
// end::search-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
// tag::search-response-1
RestStatus status = searchResponse.status();
TimeValue took = searchResponse.getTook();
Boolean terminatedEarly = searchResponse.isTerminatedEarly();
boolean timedOut = searchResponse.isTimedOut();
// end::search-response-1
// tag::search-response-2
int totalShards = searchResponse.getTotalShards();
int successfulShards = searchResponse.getSuccessfulShards();
int failedShards = searchResponse.getFailedShards();
for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
// failures should be handled here
}
// end::search-response-2
assertNotNull(searchResponse);
// tag::search-hits-get
SearchHits hits = searchResponse.getHits();
// end::search-hits-get
// tag::search-hits-info
TotalHits totalHits = hits.getTotalHits();
// the total number of hits, must be interpreted in the context of totalHits.relation
long numHits = totalHits.value;
// whether the number of hits is accurate (EQUAL_TO) or a lower bound of the total (GREATER_THAN_OR_EQUAL_TO)
TotalHits.Relation relation = totalHits.relation;
float maxScore = hits.getMaxScore();
// end::search-hits-info
// tag::search-hits-singleHit
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
// do something with the SearchHit
}
// end::search-hits-singleHit
for (SearchHit hit : searchHits) {
// tag::search-hits-singleHit-properties
String index = hit.getIndex();
String id = hit.getId();
float score = hit.getScore();
// end::search-hits-singleHit-properties
// tag::search-hits-singleHit-source
String sourceAsString = hit.getSourceAsString();
Map<String, Object> sourceAsMap = hit.getSourceAsMap();
String documentTitle = (String) sourceAsMap.get("title");
List<Object> users = (List<Object>) sourceAsMap.get("user");
Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject");
// end::search-hits-singleHit-source
}
assertEquals(3, numHits);
assertEquals(TotalHits.Relation.EQUAL_TO, relation);
assertNotNull(hits.getHits()[0].getSourceAsString());
assertNotNull(hits.getHits()[0].getSourceAsMap().get("title"));
assertNotNull(hits.getHits()[0].getSourceAsMap().get("innerObject"));
assertNull(hits.getHits()[0].getSourceAsMap().get("user"));
}
}
use of org.opensearch.search.sort.ScoreSortBuilder in project OpenSearch by opensearch-project.
the class RareTermsAggregatorTests method testWithNestedScoringAggregations.
public void testWithNestedScoringAggregations() throws IOException {
try (Directory directory = newDirectory()) {
try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
for (int i = 0; i < 10; i++) {
int[] nestedValues = new int[i];
for (int j = 0; j < i; j++) {
nestedValues[j] = j;
}
indexWriter.addDocuments(generateDocsWithNested(Integer.toString(i), i, nestedValues));
}
indexWriter.commit();
for (boolean withScore : new boolean[] { true, false }) {
NestedAggregationBuilder nested = new NestedAggregationBuilder("nested", "nested_object").subAggregation(new RareTermsAggregationBuilder("terms").field("nested_value").maxDocCount(2).subAggregation(new TopHitsAggregationBuilder("top_hits").sort(withScore ? new ScoreSortBuilder() : new FieldSortBuilder("_doc")).storedField("_none_")));
MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType("nested_value", NumberFieldMapper.NumberType.LONG);
try (IndexReader indexReader = wrapInMockESDirectoryReader(DirectoryReader.open(directory))) {
if (withScore) {
IllegalStateException e = expectThrows(IllegalStateException.class, () -> searchAndReduce(newIndexSearcher(indexReader), // match root document only
new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType));
assertThat(e.getMessage(), equalTo("RareTerms agg [terms] is the child of the nested agg [nested], " + "and also has a scoring child agg [top_hits]. This combination is not supported because it requires " + "executing in [depth_first] mode, which the RareTerms agg cannot do."));
} else {
InternalNested result = searchAndReduce(newIndexSearcher(indexReader), // match root document only
new DocValuesFieldExistsQuery(PRIMARY_TERM_NAME), nested, fieldType);
InternalMultiBucketAggregation<?, ?> terms = result.getAggregations().get("terms");
assertThat(terms.getBuckets().size(), equalTo(2));
long counter = 1;
for (MultiBucketsAggregation.Bucket bucket : terms.getBuckets()) {
InternalTopHits topHits = bucket.getAggregations().get("top_hits");
TotalHits hits = topHits.getHits().getTotalHits();
assertNotNull(hits);
assertThat(hits.value, equalTo(counter));
assertThat(topHits.getHits().getMaxScore(), equalTo(Float.NaN));
counter += 1;
}
}
}
}
}
}
}
Aggregations