Search in sources :

Example 16 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class CollapseBuilderTests method testBuild.

public void testBuild() throws IOException {
    Directory dir = new ByteBuffersDirectory();
    try (IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())))) {
        writer.commit();
    }
    QueryShardContext shardContext = mock(QueryShardContext.class);
    try (IndexReader reader = DirectoryReader.open(dir)) {
        when(shardContext.getIndexReader()).thenReturn(reader);
        MappedFieldType numberFieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG);
        when(shardContext.fieldMapper("field")).thenReturn(numberFieldType);
        CollapseBuilder builder = new CollapseBuilder("field");
        CollapseContext collapseContext = builder.build(shardContext);
        assertEquals(collapseContext.getFieldType(), numberFieldType);
        numberFieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG, true, false, false, false, null, Collections.emptyMap());
        when(shardContext.fieldMapper("field")).thenReturn(numberFieldType);
        IllegalArgumentException exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext));
        assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`");
        numberFieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.LONG, false, false, true, false, null, Collections.emptyMap());
        when(shardContext.fieldMapper("field")).thenReturn(numberFieldType);
        builder.setInnerHits(new InnerHitBuilder());
        exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext));
        assertEquals(exc.getMessage(), "cannot expand `inner_hits` for collapse field `field`, only indexed field can retrieve `inner_hits`");
        MappedFieldType keywordFieldType = new KeywordFieldMapper.KeywordFieldType("field");
        when(shardContext.fieldMapper("field")).thenReturn(keywordFieldType);
        CollapseBuilder kbuilder = new CollapseBuilder("field");
        collapseContext = kbuilder.build(shardContext);
        assertEquals(collapseContext.getFieldType(), keywordFieldType);
        keywordFieldType = new KeywordFieldMapper.KeywordFieldType("field", true, false, Collections.emptyMap());
        when(shardContext.fieldMapper("field")).thenReturn(keywordFieldType);
        exc = expectThrows(IllegalArgumentException.class, () -> kbuilder.build(shardContext));
        assertEquals(exc.getMessage(), "cannot collapse on field `field` without `doc_values`");
        keywordFieldType = new KeywordFieldMapper.KeywordFieldType("field", false, true, Collections.emptyMap());
        when(shardContext.fieldMapper("field")).thenReturn(keywordFieldType);
        kbuilder.setInnerHits(new InnerHitBuilder());
        exc = expectThrows(IllegalArgumentException.class, () -> builder.build(shardContext));
        assertEquals(exc.getMessage(), "cannot expand `inner_hits` for collapse field `field`, only indexed field can retrieve `inner_hits`");
    }
}
Also used : NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) KeywordFieldMapper(org.opensearch.index.mapper.KeywordFieldMapper) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.opensearch.index.mapper.MappedFieldType) QueryShardContext(org.opensearch.index.query.QueryShardContext) ByteBuffersDirectory(org.apache.lucene.store.ByteBuffersDirectory) Directory(org.apache.lucene.store.Directory)

Example 17 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class CollapseBuilder method innerToXContent.

private void innerToXContent(XContentBuilder builder) throws IOException {
    builder.field(FIELD_FIELD.getPreferredName(), field);
    if (maxConcurrentGroupRequests > 0) {
        builder.field(MAX_CONCURRENT_GROUP_REQUESTS_FIELD.getPreferredName(), maxConcurrentGroupRequests);
    }
    if (innerHits.isEmpty() == false) {
        if (innerHits.size() == 1) {
            builder.field(INNER_HITS_FIELD.getPreferredName(), innerHits.get(0));
        } else {
            builder.startArray(INNER_HITS_FIELD.getPreferredName());
            for (InnerHitBuilder innerHit : innerHits) {
                innerHit.toXContent(builder, ToXContent.EMPTY_PARAMS);
            }
            builder.endArray();
        }
    }
}
Also used : InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder)

Example 18 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder 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)

Example 19 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class ExpandSearchPhaseTests method testCollapseSingleHit.

public void testCollapseSingleHit() throws IOException {
    final int iters = randomIntBetween(5, 10);
    for (int i = 0; i < iters; i++) {
        final int numInnerHits = randomIntBetween(1, 5);
        List<SearchHits> collapsedHits = new ArrayList<>(numInnerHits);
        for (int innerHitNum = 0; innerHitNum < numInnerHits; innerHitNum++) {
            SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(innerHitNum, "ID", Collections.emptyMap(), Collections.emptyMap()), new SearchHit(innerHitNum + 1, "ID", Collections.emptyMap(), Collections.emptyMap()) }, new TotalHits(2, TotalHits.Relation.EQUAL_TO), 1.0F);
            collapsedHits.add(hits);
        }
        AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
        QueryBuilder originalQuery = randomBoolean() ? null : QueryBuilders.termQuery("foo", "bar");
        final MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
        String collapseValue = randomBoolean() ? null : "boom";
        mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(IntStream.range(0, numInnerHits).mapToObj(hitNum -> new InnerHitBuilder().setName("innerHit" + hitNum)).collect(Collectors.toList()))));
        mockSearchPhaseContext.getRequest().source().query(originalQuery);
        mockSearchPhaseContext.searchTransport = new SearchTransportService(null, null) {

            @Override
            void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
                assertTrue(executedMultiSearch.compareAndSet(false, true));
                assertEquals(numInnerHits, request.requests().size());
                SearchRequest searchRequest = request.requests().get(0);
                assertTrue(searchRequest.source().query() instanceof BoolQueryBuilder);
                BoolQueryBuilder groupBuilder = (BoolQueryBuilder) searchRequest.source().query();
                if (collapseValue == null) {
                    assertThat(groupBuilder.mustNot(), Matchers.contains(QueryBuilders.existsQuery("someField")));
                } else {
                    assertThat(groupBuilder.filter(), Matchers.contains(QueryBuilders.matchQuery("someField", "boom")));
                }
                if (originalQuery != null) {
                    assertThat(groupBuilder.must(), Matchers.contains(QueryBuilders.termQuery("foo", "bar")));
                }
                assertArrayEquals(mockSearchPhaseContext.getRequest().indices(), searchRequest.indices());
                List<MultiSearchResponse.Item> mSearchResponses = new ArrayList<>(numInnerHits);
                for (int innerHitNum = 0; innerHitNum < numInnerHits; innerHitNum++) {
                    InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits.get(innerHitNum), null, null, null, false, null, 1);
                    mockSearchPhaseContext.sendSearchResponse(internalSearchResponse, null);
                    mSearchResponses.add(new MultiSearchResponse.Item(mockSearchPhaseContext.searchResponse.get(), null));
                }
                listener.onResponse(new MultiSearchResponse(mSearchResponses.toArray(new MultiSearchResponse.Item[0]), randomIntBetween(1, 10000)));
            }
        };
        SearchHits hits = new SearchHits(new SearchHit[] { new SearchHit(1, "ID", Collections.singletonMap("someField", new DocumentField("someField", Collections.singletonList(collapseValue))), Collections.emptyMap()) }, 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();
        SearchResponse theResponse = mockSearchPhaseContext.searchResponse.get();
        assertNotNull(theResponse);
        assertEquals(numInnerHits, theResponse.getHits().getHits()[0].getInnerHits().size());
        for (int innerHitNum = 0; innerHitNum < numInnerHits; innerHitNum++) {
            assertSame(theResponse.getHits().getHits()[0].getInnerHits().get("innerHit" + innerHitNum), collapsedHits.get(innerHitNum));
        }
        assertTrue(executedMultiSearch.get());
    }
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) SearchHit(org.opensearch.search.SearchHit) DocumentField(org.opensearch.common.document.DocumentField) ArrayList(java.util.ArrayList) 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) ArrayList(java.util.ArrayList) List(java.util.List) SearchHits(org.opensearch.search.SearchHits) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 20 with InnerHitBuilder

use of org.opensearch.index.query.InnerHitBuilder in project OpenSearch by opensearch-project.

the class HasChildQueryBuilderTests method testFromJson.

public void testFromJson() throws IOException {
    String query = "{\n" + "  \"has_child\" : {\n" + "    \"query\" : {\n" + "      \"range\" : {\n" + "        \"mapped_string\" : {\n" + "          \"from\" : \"agJhRET\",\n" + "          \"to\" : \"zvqIq\",\n" + "          \"include_lower\" : true,\n" + "          \"include_upper\" : true,\n" + "          \"boost\" : 1.0\n" + "        }\n" + "      }\n" + "    },\n" + "    \"type\" : \"child\",\n" + "    \"score_mode\" : \"avg\",\n" + "    \"min_children\" : 883170873,\n" + "    \"max_children\" : 1217235442,\n" + "    \"ignore_unmapped\" : false,\n" + "    \"boost\" : 2.0,\n" + "    \"_name\" : \"WNzYMJKRwePuRBh\",\n" + "    \"inner_hits\" : {\n" + "      \"name\" : \"inner_hits_name\",\n" + "      \"ignore_unmapped\" : false,\n" + "      \"from\" : 0,\n" + "      \"size\" : 100,\n" + "      \"version\" : false,\n" + "      \"seq_no_primary_term\" : false,\n" + "      \"explain\" : false,\n" + "      \"track_scores\" : false,\n" + "      \"sort\" : [ {\n" + "        \"mapped_string\" : {\n" + "          \"order\" : \"asc\"\n" + "        }\n" + "      } ]\n" + "    }\n" + "  }\n" + "}";
    HasChildQueryBuilder queryBuilder = (HasChildQueryBuilder) parseQuery(query);
    checkGeneratedJson(query, queryBuilder);
    assertEquals(query, queryBuilder.maxChildren(), 1217235442);
    assertEquals(query, queryBuilder.minChildren(), 883170873);
    assertEquals(query, queryBuilder.boost(), 2.0f, 0.0f);
    assertEquals(query, queryBuilder.queryName(), "WNzYMJKRwePuRBh");
    assertEquals(query, queryBuilder.childType(), "child");
    assertEquals(query, queryBuilder.scoreMode(), ScoreMode.Avg);
    assertNotNull(query, queryBuilder.innerHit());
    InnerHitBuilder expected = new InnerHitBuilder("child").setName("inner_hits_name").setSize(100).addSort(new FieldSortBuilder("mapped_string").order(SortOrder.ASC));
    assertEquals(query, queryBuilder.innerHit(), expected);
}
Also used : InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Aggregations

InnerHitBuilder (org.opensearch.index.query.InnerHitBuilder)43 SearchResponse (org.opensearch.action.search.SearchResponse)22 ArrayList (java.util.ArrayList)15 SearchHits (org.opensearch.search.SearchHits)14 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)13 QueryBuilder (org.opensearch.index.query.QueryBuilder)11 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)9 SearchHit (org.opensearch.search.SearchHit)9 OpenSearchAssertions.assertSearchHits (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHits)8 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)7 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)6 CollapseBuilder (org.opensearch.search.collapse.CollapseBuilder)5 InternalSearchResponse (org.opensearch.search.internal.InternalSearchResponse)5 FieldSortBuilder (org.opensearch.search.sort.FieldSortBuilder)5 OpenSearchAssertions.assertSearchHit (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchHit)5 TotalHits (org.apache.lucene.search.TotalHits)4 List (java.util.List)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 SearchPhaseExecutionException (org.opensearch.action.search.SearchPhaseExecutionException)3 WrapperQueryBuilder (org.opensearch.index.query.WrapperQueryBuilder)3