use of org.opensearch.search.collapse.CollapseBuilder 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.collapse.CollapseBuilder in project OpenSearch by opensearch-project.
the class CCSDuelIT method testFieldCollapsingOneClusterHasNoResults.
public void testFieldCollapsingOneClusterHasNoResults() throws Exception {
assumeMultiClusterSetup();
SearchRequest searchRequest = initSearchRequest();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
boolean onlyRemote = randomBoolean();
sourceBuilder.query(new TermQueryBuilder("_index", onlyRemote ? REMOTE_INDEX_NAME : INDEX_NAME));
sourceBuilder.collapse(new CollapseBuilder("user.keyword"));
searchRequest.source(sourceBuilder);
duelSearch(searchRequest, response -> {
assertHits(response);
for (SearchHit hit : response.getHits().getHits()) {
assertEquals(INDEX_NAME, hit.getIndex());
if (onlyRemote) {
assertEquals("my_remote_cluster", hit.getClusterAlias());
} else {
assertNull(hit.getClusterAlias());
}
}
});
}
use of org.opensearch.search.collapse.CollapseBuilder in project OpenSearch by opensearch-project.
the class CCSDuelIT method testFieldCollapsingSortByScore.
public void testFieldCollapsingSortByScore() throws Exception {
assumeMultiClusterSetup();
SearchRequest searchRequest = initSearchRequest();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
searchRequest.source(sourceBuilder);
sourceBuilder.query(QueryBuilders.matchQuery("tags", "ruby"));
sourceBuilder.collapse(new CollapseBuilder("user.keyword"));
duelSearch(searchRequest, CCSDuelIT::assertHits);
}
use of org.opensearch.search.collapse.CollapseBuilder in project OpenSearch by opensearch-project.
the class ExpandSearchPhase method run.
@Override
public void run() {
if (isCollapseRequest() && searchResponse.hits().getHits().length > 0) {
SearchRequest searchRequest = context.getRequest();
CollapseBuilder collapseBuilder = searchRequest.source().collapse();
final List<InnerHitBuilder> innerHitBuilders = collapseBuilder.getInnerHits();
MultiSearchRequest multiRequest = new MultiSearchRequest();
if (collapseBuilder.getMaxConcurrentGroupRequests() > 0) {
multiRequest.maxConcurrentSearchRequests(collapseBuilder.getMaxConcurrentGroupRequests());
}
for (SearchHit hit : searchResponse.hits().getHits()) {
BoolQueryBuilder groupQuery = new BoolQueryBuilder();
Object collapseValue = hit.field(collapseBuilder.getField()).getValue();
if (collapseValue != null) {
groupQuery.filter(QueryBuilders.matchQuery(collapseBuilder.getField(), collapseValue));
} else {
groupQuery.mustNot(QueryBuilders.existsQuery(collapseBuilder.getField()));
}
QueryBuilder origQuery = searchRequest.source().query();
if (origQuery != null) {
groupQuery.must(origQuery);
}
for (InnerHitBuilder innerHitBuilder : innerHitBuilders) {
CollapseBuilder innerCollapseBuilder = innerHitBuilder.getInnerCollapseBuilder();
SearchSourceBuilder sourceBuilder = buildExpandSearchSourceBuilder(innerHitBuilder, innerCollapseBuilder).query(groupQuery).postFilter(searchRequest.source().postFilter());
SearchRequest groupRequest = new SearchRequest(searchRequest);
groupRequest.source(sourceBuilder);
multiRequest.add(groupRequest);
}
}
context.getSearchTransport().sendExecuteMultiSearch(multiRequest, context.getTask(), ActionListener.wrap(response -> {
Iterator<MultiSearchResponse.Item> it = response.iterator();
for (SearchHit hit : searchResponse.hits.getHits()) {
for (InnerHitBuilder innerHitBuilder : innerHitBuilders) {
MultiSearchResponse.Item item = it.next();
if (item.isFailure()) {
context.onPhaseFailure(this, "failed to expand hits", item.getFailure());
return;
}
SearchHits innerHits = item.getResponse().getHits();
if (hit.getInnerHits() == null) {
hit.setInnerHits(new HashMap<>(innerHitBuilders.size()));
}
hit.getInnerHits().put(innerHitBuilder.getName(), innerHits);
}
}
context.sendSearchResponse(searchResponse, queryResults);
}, context::onFailure));
} else {
context.sendSearchResponse(searchResponse, queryResults);
}
}
use of org.opensearch.search.collapse.CollapseBuilder in project OpenSearch by opensearch-project.
the class ExpandSearchPhaseTests method testExpandRequestOptions.
public void testExpandRequestOptions() throws IOException {
MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
boolean version = randomBoolean();
final boolean seqNoAndTerm = randomBoolean();
mockSearchPhaseContext.searchTransport = new SearchTransportService(null, null) {
@Override
void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
final QueryBuilder postFilter = QueryBuilders.existsQuery("foo");
assertTrue(request.requests().stream().allMatch((r) -> "foo".equals(r.preference())));
assertTrue(request.requests().stream().allMatch((r) -> "baz".equals(r.routing())));
assertTrue(request.requests().stream().allMatch((r) -> version == r.source().version()));
assertTrue(request.requests().stream().allMatch((r) -> seqNoAndTerm == r.source().seqNoAndPrimaryTerm()));
assertTrue(request.requests().stream().allMatch((r) -> postFilter.equals(r.source().postFilter())));
assertTrue(request.requests().stream().allMatch((r) -> r.source().fetchSource().fetchSource() == false));
assertTrue(request.requests().stream().allMatch((r) -> r.source().fetchSource().includes().length == 0));
assertTrue(request.requests().stream().allMatch((r) -> r.source().fetchSource().excludes().length == 0));
}
};
mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz").setVersion(version).setSeqNoAndPrimaryTerm(seqNoAndTerm))).fetchSource(false).postFilter(QueryBuilders.existsQuery("foo"))).preference("foobar").routing("baz");
SearchHits hits = new SearchHits(new SearchHit[0], new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f);
InternalSearchResponse internalSearchResponse = new InternalSearchResponse(hits, null, null, null, false, null, 1);
ExpandSearchPhase phase = new ExpandSearchPhase(mockSearchPhaseContext, internalSearchResponse, null);
phase.run();
mockSearchPhaseContext.assertNoFailure();
assertNotNull(mockSearchPhaseContext.searchResponse.get());
}
Aggregations