/* SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. */ /* * Modifications Copyright OpenSearch Contributors. See * GitHub history for details. * * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using OpenSearch.OpenSearch.Xunit.XunitPlumbing; using OpenSearch.Net; using FluentAssertions; using OpenSearch.Client; using Tests.Core.Extensions; using Tests.Core.ManagedOpenSearch.Clusters; using Tests.Domain; using Tests.Framework.EndpointTests; using Tests.Framework.EndpointTests.TestState; using static OpenSearch.Client.Infer; namespace Tests.Search.MultiSearch { public class MultiSearchApiTests : ApiIntegrationTestBase { public MultiSearchApiTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } protected override bool ExpectIsValid => true; protected override bool SupportsDeserialization => false; protected override string UrlPath => "/project/_msearch"; protected override int ExpectStatusCode => 200; protected override HttpMethod HttpMethod => HttpMethod.POST; protected override LazyResponses ClientUsage() => Calls( (c, f) => c.MultiSearch(Index(), f), (c, f) => c.MultiSearchAsync(Index(), f), (c, r) => c.MultiSearch(r), (c, r) => c.MultiSearchAsync(r) ); protected override object ExpectJson => new object[] { new { }, new { from = 0, size = 10, query = new { match_all = new { } }, track_total_hits = true }, new { search_type = "dfs_query_then_fetch" }, new { }, new { index = "devs" }, new { from = 0, size = 5, query = new { match_all = new { } } }, new { index = "devs" }, new { from = 0, size = 5, query = new { match_all = new { } } }, new { index = "queries" }, new { query = new { percolate = new { document = Project.InstanceAnonymous, field = "query", routing = Project.First.Name } } }, new { index = "queries" }, new { query = new { percolate = new { index = "project", id = Project.First.Name, version = 1, field = "query", routing = Project.First.Name } } }, }; protected override MultiSearchDescriptor NewDescriptor() => new MultiSearchDescriptor(Index()); protected override Func Fluent => ms => ms .Search("10projects", s => s.Query(q => q.MatchAll()).From(0).Size(10).TrackTotalHits()) .Search("dfs_projects", s => s.SearchType(SearchType.DfsQueryThenFetch)) .Search("5developers", s => s.Query(q => q.MatchAll()).From(0).Size(5)) .Search("infer_type_name", s => s.Index("devs").From(0).Size(5).MatchAll()) .Search("percolate_document", s => s .Index() .Query(q => q .Percolate(p => p .Document(Project.Instance) .Field(f => f.Query) ) ) ) .Search("percolate_existing_document", s => s .Index() .Query(q => q .Percolate(p => p .Index() .Id(Project.First.Name) .Version(1) .Routing(Project.First.Name) .Field(f => f.Query) ) ) ); protected override MultiSearchRequest Initializer => new MultiSearchRequest(typeof(Project)) { Operations = new Dictionary { { "10projects", new SearchRequest { From = 0, Size = 10, Query = new QueryContainer(new MatchAllQuery()), TrackTotalHits = true } }, { "dfs_projects", new SearchRequest { SearchType = SearchType.DfsQueryThenFetch } }, { "5developers", new SearchRequest { From = 0, Size = 5, Query = new QueryContainer(new MatchAllQuery()) } }, { "infer_type_name", new SearchRequest("devs") { From = 0, Size = 5, Query = new QueryContainer(new MatchAllQuery()) } }, { "percolate_document", new SearchRequest() { Query = new QueryContainer(new PercolateQuery { Document = Project.Instance, Field = Field(f => f.Query) }) } }, { "percolate_existing_document", new SearchRequest() { Query = new QueryContainer(new PercolateQuery { Index = typeof(Project), Id = Project.First.Name, Version = 1, Routing = Project.First.Name, Field = Field(f => f.Query) }) } }, } }; [I] public Task AssertResponse() => AssertOnAllResponses(r => { r.Took.Should().BeGreaterThan(0); r.TotalResponses.Should().Be(6); var nvalidResponses = r.GetInvalidResponses(); nvalidResponses.Should().BeEmpty(); var allResponses = r.AllResponses.ToList(); allResponses.Should().NotBeEmpty().And.HaveCount(6).And.OnlyContain(rr => rr.IsValid); var projects = r.GetResponse("10projects"); projects.ShouldBeValid(); projects.Documents.Should().HaveCount(10); projects.HitsMetadata.Total.Relation.Should().Be(TotalHitsRelation.EqualTo); var projectsCount = r.GetResponse("count_project"); projectsCount.Should().BeNull(); var developers = r.GetResponse("5developers"); developers.ShouldBeValid(); developers.Documents.Should().HaveCount(5); var inferredTypeName = r.GetResponse("infer_type_name"); inferredTypeName.ShouldBeValid(); inferredTypeName.Documents.Should().HaveCount(5); var percolateDocument = r.GetResponse("percolate_document"); percolateDocument.ShouldBeValid(); percolateDocument.Documents.Should().HaveCount(1); var percolateExistingDocument = r.GetResponse("percolate_existing_document"); percolateExistingDocument.ShouldBeValid(); percolateExistingDocument.Documents.Should().HaveCount(1); }); } }