Skip to content

Commit

Permalink
Create > 2.2.0 unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
gjunge committed Aug 19, 2023
1 parent 0a5517c commit 00776bb
Show file tree
Hide file tree
Showing 3 changed files with 316 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
/// </summary>
/// <remarks>Introduced in OpenSearch 2.2</remarks>
[DataMember(Name ="max_analyzer_offset")]
int? MaxAnalyzerOffset { get; set; }

Expand Down
8 changes: 2 additions & 6 deletions tests/Tests/Search/Request/HighlightingUsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
}
}
}
},
max_analyzer_offset = 1_000_000

}
}
};

Expand Down Expand Up @@ -202,7 +200,6 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
)
)
)
.MaxAnalyzerOffset(1_000_000) //the default value
);

protected override SearchRequest<Project> Initializer =>
Expand Down Expand Up @@ -264,8 +261,7 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
}
}
}
},
MaxAnalyzerOffset = 1_000_000
}
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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<string, object>
{
{
"name.standard", new Dictionary<string, object>
{
{ "query", "Upton Sons Shield Rice Rowe Roberts" }
}
}
}
},
highlight = new
{
pre_tags = new[] { "<tag1>" },
post_tags = new[] { "</tag1>" },
encoder = "html",
highlight_query = new
{
match = new Dictionary<string, object>
{
{
"name.standard", new Dictionary<string, object>
{
{ "query", "Upton Sons Shield Rice Rowe Roberts" }
}
}
}
},
fields = new Dictionary<string, object>
{
{
"name.standard", new Dictionary<string, object>
{
{ "type", "plain" },
{ "force_source", true },
{ "fragment_size", 150 },
{ "fragmenter", "span" },
{ "number_of_fragments", 3 },
{ "no_match_size", 150 }
}
},
{
"leadDeveloper.firstName", new Dictionary<string, object>
{
{ "type", "fvh" },
{ "phrase_limit", 10 },
{ "boundary_max_scan", 50 },
{ "pre_tags", new[] { "<name>" } },
{ "post_tags", new[] { "</name>" } },
{
"highlight_query", new Dictionary<string, object>
{
{
"match", new Dictionary<string, object>
{
{
"leadDeveloper.firstName", new Dictionary<string, object>
{
{ "query", "Kurt Edgardo Naomi Dariana Justice Felton" }
}
}
}
}
}
}
}
},
{
"leadDeveloper.lastName", new Dictionary<string, object>
{
{ "type", "unified" },
{ "pre_tags", new[] { "<name>" } },
{ "post_tags", new[] { "</name>" } },
{
"highlight_query", new Dictionary<string, object>
{
{
"match", new Dictionary<string, object>
{
{
"leadDeveloper.lastName", new Dictionary<string, object>
{
{ "query", LastNameSearch }
}
}
}
}
}
}
}
}
},
max_analyzer_offset = 1_000_000
}
};

protected override Func<SearchDescriptor<Project>, 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("<tag1>")
.PostTags("</tag1>")
.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("<name>")
.PostTags("</name>")
.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("<name>")
.PostTags("</name>")
.HighlightQuery(q => q
.Match(m => m
.Field(p => p.LeadDeveloper.LastName)
.Query(LastNameSearch)
)
)
)
.MaxAnalyzerOffset(1_000_000) //the default value
);

protected override SearchRequest<Project> Initializer =>
new SearchRequest<Project>
{
Query = new MatchQuery
{
Query = "Upton Sons Shield Rice Rowe Roberts",
Field = "name.standard"
},
Highlight = new Highlight
{
PreTags = new[] { "<tag1>" },
PostTags = new[] { "</tag1>" },
Encoder = HighlighterEncoder.Html,
HighlightQuery = new MatchQuery
{
Query = "Upton Sons Shield Rice Rowe Roberts",
Field = "name.standard"
},
Fields = new Dictionary<Field, IHighlightField>
{
{
"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[] { "<name>" },
PostTags = new[] { "</name>" },
HighlightQuery = new MatchQuery
{
Field = "leadDeveloper.firstName",
Query = "Kurt Edgardo Naomi Dariana Justice Felton"
}
}
},
{
"leadDeveloper.lastName", new HighlightField
{
Type = HighlighterType.Unified,
PreTags = new[] { "<name>" },
PostTags = new[] { "</name>" },
HighlightQuery = new MatchQuery
{
Field = "leadDeveloper.lastName",
Query = LastNameSearch
}
}
}
},
MaxAnalyzerOffset = 1_000_000
}
};

protected override void ExpectResponse(ISearchResponse<Project> 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("<tag1>");
highlight.Should().Contain("</tag1>");
}
}
else if (highlightField.Key == "leadDeveloper.firstName")
{
foreach (var highlight in highlightField.Value)
{
highlight.Should().Contain("<name>");
highlight.Should().Contain("</name>");
}
}
else if (highlightField.Key == "leadDeveloper.lastName")
{
foreach (var highlight in highlightField.Value)
{
highlight.Should().Contain("<name>");
highlight.Should().Contain("</name>");
}
}
else
Assert.True(false, $"highlights contains unexpected key {highlightField.Key}");
}
}
}
}
}

0 comments on commit 00776bb

Please sign in to comment.