From 00776bb0327e4cc663ee99ac65cdd71133f546d8 Mon Sep 17 00:00:00 2001 From: Gideon Junge Date: Sat, 19 Aug 2023 10:42:12 +0200 Subject: [PATCH] Create > 2.2.0 unit test --- .../Search/Search/Highlighting/Highlight.cs | 1 + .../Search/Request/HighlightingUsageTests.cs | 8 +- ...lightingUsageTestsWithMaxAnalyzerOffset.cs | 313 ++++++++++++++++++ 3 files changed, 316 insertions(+), 6 deletions(-) create mode 100644 tests/Tests/Search/Request/HighlightingUsageTestsWithMaxAnalyzerOffset.cs diff --git a/src/OpenSearch.Client/Search/Search/Highlighting/Highlight.cs b/src/OpenSearch.Client/Search/Search/Highlighting/Highlight.cs index 5aae4617cc..94b93bbb04 100644 --- a/src/OpenSearch.Client/Search/Search/Highlighting/Highlight.cs +++ b/src/OpenSearch.Client/Search/Search/Highlighting/Highlight.cs @@ -107,6 +107,7 @@ public interface IHighlight /// If this setting is set to a non-negative value, the highlighting stops at this defined maximum limit, and the /// rest of the text is not processed, thus not highlighted and no error is returned. /// + /// Introduced in OpenSearch 2.2 [DataMember(Name ="max_analyzer_offset")] int? MaxAnalyzerOffset { get; set; } diff --git a/tests/Tests/Search/Request/HighlightingUsageTests.cs b/tests/Tests/Search/Request/HighlightingUsageTests.cs index 95a536576c..7a05eee5a3 100644 --- a/tests/Tests/Search/Request/HighlightingUsageTests.cs +++ b/tests/Tests/Search/Request/HighlightingUsageTests.cs @@ -145,9 +145,7 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba } } } - }, - max_analyzer_offset = 1_000_000 - + } } }; @@ -202,7 +200,6 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba ) ) ) - .MaxAnalyzerOffset(1_000_000) //the default value ); protected override SearchRequest Initializer => @@ -264,8 +261,7 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba } } } - }, - MaxAnalyzerOffset = 1_000_000 + } } }; diff --git a/tests/Tests/Search/Request/HighlightingUsageTestsWithMaxAnalyzerOffset.cs b/tests/Tests/Search/Request/HighlightingUsageTestsWithMaxAnalyzerOffset.cs new file mode 100644 index 0000000000..d1f87ba6f6 --- /dev/null +++ b/tests/Tests/Search/Request/HighlightingUsageTestsWithMaxAnalyzerOffset.cs @@ -0,0 +1,313 @@ +/* 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 FluentAssertions; +using OpenSearch.Client; +using OpenSearch.OpenSearch.Xunit.XunitPlumbing; +using Tests.Core.Extensions; +using Tests.Core.ManagedOpenSearch.Clusters; +using Tests.Domain; +using Tests.Framework.EndpointTests; +using Tests.Framework.EndpointTests.TestState; +using Xunit; + +namespace Tests.Search.Request +{ + /** + * Allows to highlight search results on one or more fields. + * The implementation uses either the lucene `highlighter` or `fast-vector-highlighter`. + * + * See the OpenSearch documentation on {ref_current}/search-request-body.html#request-body-search-highlighting[highlighting] for more detail. + */ + [SkipVersion("<2.2.0", "MaxAnalyzerOffset field was introduced in 2.2.0")] + public class HighlightingUsageTestsWithMaxAnalyzerOffset : HighlightingUsageTests + { + public HighlightingUsageTestsWithMaxAnalyzerOffset(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } + + protected override object ExpectJson => + new + { + query = new + { + match = new Dictionary + { + { + "name.standard", new Dictionary + { + { "query", "Upton Sons Shield Rice Rowe Roberts" } + } + } + } + }, + highlight = new + { + pre_tags = new[] { "" }, + post_tags = new[] { "" }, + encoder = "html", + highlight_query = new + { + match = new Dictionary + { + { + "name.standard", new Dictionary + { + { "query", "Upton Sons Shield Rice Rowe Roberts" } + } + } + } + }, + fields = new Dictionary + { + { + "name.standard", new Dictionary + { + { "type", "plain" }, + { "force_source", true }, + { "fragment_size", 150 }, + { "fragmenter", "span" }, + { "number_of_fragments", 3 }, + { "no_match_size", 150 } + } + }, + { + "leadDeveloper.firstName", new Dictionary + { + { "type", "fvh" }, + { "phrase_limit", 10 }, + { "boundary_max_scan", 50 }, + { "pre_tags", new[] { "" } }, + { "post_tags", new[] { "" } }, + { + "highlight_query", new Dictionary + { + { + "match", new Dictionary + { + { + "leadDeveloper.firstName", new Dictionary + { + { "query", "Kurt Edgardo Naomi Dariana Justice Felton" } + } + } + } + } + } + } + } + }, + { + "leadDeveloper.lastName", new Dictionary + { + { "type", "unified" }, + { "pre_tags", new[] { "" } }, + { "post_tags", new[] { "" } }, + { + "highlight_query", new Dictionary + { + { + "match", new Dictionary + { + { + "leadDeveloper.lastName", new Dictionary + { + { "query", LastNameSearch } + } + } + } + } + } + } + } + } + }, + max_analyzer_offset = 1_000_000 + } + }; + + protected override Func, ISearchRequest> Fluent => s => s + + + .Query(q => q + .Match(m => m + .Field(f => f.Name.Suffix("standard")) + .Query("Upton Sons Shield Rice Rowe Roberts") + ) + ) + .Highlight(h => h + .PreTags("") + .PostTags("") + .Encoder(HighlighterEncoder.Html) + .HighlightQuery(q => q + .Match(m => m + .Field(f => f.Name.Suffix("standard")) + .Query("Upton Sons Shield Rice Rowe Roberts") + ) + ) + .Fields( + fs => fs + .Field(p => p.Name.Suffix("standard")) + .Type("plain") + .ForceSource() + .FragmentSize(150) + .Fragmenter(HighlighterFragmenter.Span) + .NumberOfFragments(3) + .NoMatchSize(150), + fs => fs + .Field(p => p.LeadDeveloper.FirstName) + .Type(HighlighterType.Fvh) + .PreTags("") + .PostTags("") + .BoundaryMaxScan(50) + .PhraseLimit(10) + .HighlightQuery(q => q + .Match(m => m + .Field(p => p.LeadDeveloper.FirstName) + .Query("Kurt Edgardo Naomi Dariana Justice Felton") + ) + ), + fs => fs + .Field(p => p.LeadDeveloper.LastName) + .Type(HighlighterType.Unified) + .PreTags("") + .PostTags("") + .HighlightQuery(q => q + .Match(m => m + .Field(p => p.LeadDeveloper.LastName) + .Query(LastNameSearch) + ) + ) + ) + .MaxAnalyzerOffset(1_000_000) //the default value + ); + + protected override SearchRequest Initializer => + new SearchRequest + { + Query = new MatchQuery + { + Query = "Upton Sons Shield Rice Rowe Roberts", + Field = "name.standard" + }, + Highlight = new Highlight + { + PreTags = new[] { "" }, + PostTags = new[] { "" }, + Encoder = HighlighterEncoder.Html, + HighlightQuery = new MatchQuery + { + Query = "Upton Sons Shield Rice Rowe Roberts", + Field = "name.standard" + }, + Fields = new Dictionary + { + { + "name.standard", new HighlightField + { + Type = HighlighterType.Plain, + ForceSource = true, + FragmentSize = 150, + Fragmenter = HighlighterFragmenter.Span, + NumberOfFragments = 3, + NoMatchSize = 150 + } + }, + { + "leadDeveloper.firstName", new HighlightField + { + Type = "fvh", + PhraseLimit = 10, + BoundaryMaxScan = 50, + PreTags = new[] { "" }, + PostTags = new[] { "" }, + HighlightQuery = new MatchQuery + { + Field = "leadDeveloper.firstName", + Query = "Kurt Edgardo Naomi Dariana Justice Felton" + } + } + }, + { + "leadDeveloper.lastName", new HighlightField + { + Type = HighlighterType.Unified, + PreTags = new[] { "" }, + PostTags = new[] { "" }, + HighlightQuery = new MatchQuery + { + Field = "leadDeveloper.lastName", + Query = LastNameSearch + } + } + } + }, + MaxAnalyzerOffset = 1_000_000 + } + }; + + protected override void ExpectResponse(ISearchResponse response) + { + response.ShouldBeValid(); + + foreach (var highlightsInEachHit in response.Hits.Select(d => d.Highlight)) + { + foreach (var highlightField in highlightsInEachHit) + { + if (highlightField.Key == "name.standard") + { + foreach (var highlight in highlightField.Value) + { + highlight.Should().Contain(""); + highlight.Should().Contain(""); + } + } + else if (highlightField.Key == "leadDeveloper.firstName") + { + foreach (var highlight in highlightField.Value) + { + highlight.Should().Contain(""); + highlight.Should().Contain(""); + } + } + else if (highlightField.Key == "leadDeveloper.lastName") + { + foreach (var highlight in highlightField.Value) + { + highlight.Should().Contain(""); + highlight.Should().Contain(""); + } + } + else + Assert.True(false, $"highlights contains unexpected key {highlightField.Key}"); + } + } + } + } +}