Search in sources :

Example 1 with CollapseBuilder

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);
    });
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) ScoreSortBuilder(org.opensearch.search.sort.ScoreSortBuilder) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 2 with CollapseBuilder

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());
            }
        }
    });
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 3 with CollapseBuilder

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);
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 4 with CollapseBuilder

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);
    }
}
Also used : QueryBuilders(org.opensearch.index.query.QueryBuilders) Iterator(java.util.Iterator) SearchHit(org.opensearch.search.SearchHit) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) HashMap(java.util.HashMap) SearchHits(org.opensearch.search.SearchHits) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) List(java.util.List) AtomicArray(org.opensearch.common.util.concurrent.AtomicArray) QueryBuilder(org.opensearch.index.query.QueryBuilder) SearchPhaseResult(org.opensearch.search.SearchPhaseResult) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) ActionListener(org.opensearch.action.ActionListener) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) SearchHit(org.opensearch.search.SearchHit) HashMap(java.util.HashMap) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) Iterator(java.util.Iterator) SearchHits(org.opensearch.search.SearchHits)

Example 5 with CollapseBuilder

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());
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchHits(org.opensearch.search.SearchHits) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Aggregations

SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)9 CollapseBuilder (org.opensearch.search.collapse.CollapseBuilder)9 TotalHits (org.apache.lucene.search.TotalHits)5 InnerHitBuilder (org.opensearch.index.query.InnerHitBuilder)5 SearchHits (org.opensearch.search.SearchHits)5 InternalSearchResponse (org.opensearch.search.internal.InternalSearchResponse)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 SearchRequest (org.opensearch.action.search.SearchRequest)3 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)3 QueryBuilder (org.opensearch.index.query.QueryBuilder)3 SearchHit (org.opensearch.search.SearchHit)3 List (java.util.List)2 DocumentField (org.opensearch.common.document.DocumentField)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Executor (java.util.concurrent.Executor)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1