Search in sources :

Example 26 with InnerHitBuilder

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

the class ExpandSearchPhaseTests method testSkipExpandCollapseNoHits.

public void testSkipExpandCollapseNoHits() throws IOException {
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    mockSearchPhaseContext.searchTransport = new SearchTransportService(null, null) {

        @Override
        void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
            fail("expand should not try to send empty multi search request");
        }
    };
    mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz"))));
    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) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchHits(org.opensearch.search.SearchHits) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 27 with InnerHitBuilder

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

the class ExpandSearchPhaseTests method testFailOneItemFailsEntirePhase.

public void testFailOneItemFailsEntirePhase() throws IOException {
    AtomicBoolean executedMultiSearch = new AtomicBoolean(false);
    SearchHits collapsedHits = new SearchHits(new SearchHit[] { new SearchHit(2, "ID", Collections.emptyMap(), Collections.emptyMap()), new SearchHit(3, "ID", Collections.emptyMap(), Collections.emptyMap()) }, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0F);
    MockSearchPhaseContext mockSearchPhaseContext = new MockSearchPhaseContext(1);
    String collapseValue = randomBoolean() ? null : "boom";
    mockSearchPhaseContext.getRequest().source(new SearchSourceBuilder().collapse(new CollapseBuilder("someField").setInnerHits(new InnerHitBuilder().setName("foobarbaz"))));
    mockSearchPhaseContext.searchTransport = new SearchTransportService(null, null) {

        @Override
        void sendExecuteMultiSearch(MultiSearchRequest request, SearchTask task, ActionListener<MultiSearchResponse> listener) {
            assertTrue(executedMultiSearch.compareAndSet(false, true));
            InternalSearchResponse internalSearchResponse = new InternalSearchResponse(collapsedHits, null, null, null, false, null, 1);
            SearchResponse searchResponse = new SearchResponse(internalSearchResponse, null, 1, 1, 0, 0, ShardSearchFailure.EMPTY_ARRAY, SearchResponse.Clusters.EMPTY);
            listener.onResponse(new MultiSearchResponse(new MultiSearchResponse.Item[] { new MultiSearchResponse.Item(null, new RuntimeException("boom")), new MultiSearchResponse.Item(searchResponse, null) }, 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 SearchHit(2, "ID2", 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();
    assertThat(mockSearchPhaseContext.phaseFailure.get(), Matchers.instanceOf(RuntimeException.class));
    assertEquals("boom", mockSearchPhaseContext.phaseFailure.get().getMessage());
    assertNotNull(mockSearchPhaseContext.phaseFailure.get());
    assertNull(mockSearchPhaseContext.searchResponse.get());
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) SearchHit(org.opensearch.search.SearchHit) CollapseBuilder(org.opensearch.search.collapse.CollapseBuilder) DocumentField(org.opensearch.common.document.DocumentField) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SearchHits(org.opensearch.search.SearchHits) InternalSearchResponse(org.opensearch.search.internal.InternalSearchResponse)

Example 28 with InnerHitBuilder

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

the class CollapseBuilderTests method randomCollapseBuilder.

public static CollapseBuilder randomCollapseBuilder(boolean multiInnerHits) {
    CollapseBuilder builder = new CollapseBuilder(randomAlphaOfLength(10));
    builder.setMaxConcurrentGroupRequests(randomIntBetween(1, 48));
    int numInnerHits = randomIntBetween(0, multiInnerHits ? 5 : 1);
    if (numInnerHits == 1) {
        InnerHitBuilder innerHit = InnerHitBuilderTests.randomInnerHits();
        builder.setInnerHits(innerHit);
    } else if (numInnerHits > 1) {
        List<InnerHitBuilder> innerHits = new ArrayList<>(numInnerHits);
        for (int i = 0; i < numInnerHits; i++) {
            innerHits.add(InnerHitBuilderTests.randomInnerHits());
        }
        builder.setInnerHits(innerHits);
    }
    return builder;
}
Also used : InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) ArrayList(java.util.ArrayList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List)

Example 29 with InnerHitBuilder

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

the class HasChildQueryBuilder method fromXContent.

public static HasChildQueryBuilder fromXContent(XContentParser parser) throws IOException {
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String childType = null;
    ScoreMode scoreMode = ScoreMode.None;
    int minChildren = HasChildQueryBuilder.DEFAULT_MIN_CHILDREN;
    int maxChildren = HasChildQueryBuilder.DEFAULT_MAX_CHILDREN;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    String queryName = null;
    InnerHitBuilder innerHitBuilder = null;
    String currentFieldName = null;
    XContentParser.Token token;
    QueryBuilder iqb = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                iqb = parseInnerQueryBuilder(parser);
            } else if (INNER_HITS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                innerHitBuilder = InnerHitBuilder.fromXContent(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                childType = parser.text();
            } else if (SCORE_MODE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                scoreMode = NestedQueryBuilder.parseScoreMode(parser.text());
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (MIN_CHILDREN_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                minChildren = parser.intValue(true);
            } else if (MAX_CHILDREN_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                maxChildren = parser.intValue(true);
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                ignoreUnmapped = parser.booleanValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
            }
        }
    }
    HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(childType, iqb, scoreMode);
    hasChildQueryBuilder.minMaxChildren(minChildren, maxChildren);
    hasChildQueryBuilder.queryName(queryName);
    hasChildQueryBuilder.boost(boost);
    hasChildQueryBuilder.ignoreUnmapped(ignoreUnmapped);
    if (innerHitBuilder != null) {
        hasChildQueryBuilder.innerHit(innerHitBuilder);
    }
    return hasChildQueryBuilder;
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) ParsingException(org.opensearch.common.ParsingException) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 30 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