-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
QueryCaption.cs
87 lines (77 loc) · 3.32 KB
/
QueryCaption.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using Azure.Core;
namespace Azure.Search.Documents.Models
{
/// <summary>
/// Configuration for how semantic search captions search results.
/// </summary>
public partial class QueryCaption
{
private const string QueryCaptionRawSplitter = "|highlight-";
/// <summary>
/// Initializes a new instance of the <see cref="QueryCaption"/> class.
/// </summary>
/// <param name="captionType">A value that specifies whether <see cref="SemanticSearchResult.Captions"/> should be returned as part of the search response.</param>
public QueryCaption(QueryCaptionType captionType)
{
Argument.AssertNotNull(captionType, nameof(captionType));
CaptionType = captionType;
}
/// <summary>
/// Initializes a new instance of the <see cref="QueryCaption"/> class.
/// </summary>
internal QueryCaption() { }
/// <summary>
/// A value that specifies whether <see cref="SemanticSearchResult.Captions"/> should be returned as part of the search response.
/// <para>The default value is <see cref="QueryCaptionType.None"/>.</para>
/// </summary>
public QueryCaptionType CaptionType { get; internal set; }
/// <summary>
/// If <see cref="CaptionType"/> is set to <see cref="QueryCaptionType.Extractive"/>, setting this to <c>true</c> enables highlighting of the returned captions.
/// It populates <see cref="QueryCaptionResult.Highlights"/>.
/// <para>The default value is <c>true</c>.</para>
/// </summary>
public bool HighlightEnabled { get; set; } = true;
/// <summary> Constructed from <see cref="CaptionType"/> and <see cref="HighlightEnabled"/>.</summary>
[CodeGenMember("Captions")]
internal string QueryCaptionRaw
{
get
{
if (CaptionType == QueryCaptionType.Extractive)
{
return $"{CaptionType}{QueryCaptionRawSplitter}{HighlightEnabled}";
}
else
{
return CaptionType.ToString();
}
}
set
{
if (string.IsNullOrEmpty(value))
{
CaptionType = null;
}
else
{
int splitIndex = value.IndexOf(QueryCaptionRawSplitter, StringComparison.OrdinalIgnoreCase);
if (splitIndex >= 0)
{
var queryCaptionPart = value.Substring(0, splitIndex);
var highlightPart = value.Substring(splitIndex + QueryCaptionRawSplitter.Length);
CaptionType = string.IsNullOrEmpty(queryCaptionPart) ? null : new QueryCaptionType(queryCaptionPart);
HighlightEnabled = bool.TryParse(highlightPart, out bool highlightValue) ? highlightValue : true;
}
else
{
CaptionType = new QueryCaptionType(value);
}
}
}
}
}
}