/* 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 Tests.Core.Extensions; using Tests.Core.ManagedOpenSearch.Clusters; using Tests.Domain; 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. */ public class HighlightingUsageTests : SearchUsageTestBase { public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { } public string LastNameSearch { get; } = Project.First.LeadDeveloper.LastName; 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 } } } } } } } } } } } }; 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) ) ) ) ); 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 } } } } } }; 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}"); } } } } }