Skip to content

Commit 9aaf192

Browse files
authored
Exclude hidden documents from search (#2277)
* Exclude hidden documents from search * Add mapping
1 parent 4a1c7f3 commit 9aaf192

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

src/Elastic.Documentation/Search/DocumentationDocument.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ public record DocumentationDocument
6565

6666
[JsonPropertyName("parents")]
6767
public ParentDocument[] Parents { get; set; } = [];
68+
69+
[JsonPropertyName("hidden")]
70+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
71+
public bool Hidden { get; set; }
6872
}

src/Elastic.Markdown/Exporters/Elasticsearch/ElasticsearchExporter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ protected static string CreateMapping(string? inferenceId) =>
199199
"prefix": { "type": "text", "analyzer" : "hierarchy_analyzer" }
200200
}
201201
},
202+
"hidden" : {
203+
"type" : "boolean"
204+
},
202205
"applies_to" : {
203206
"type" : "nested",
204207
"properties" : {

src/Elastic.Markdown/Exporters/Elasticsearch/ElasticsearchMarkdownExporter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ public async ValueTask<bool> ExportAsync(MarkdownExportFileContext fileContext,
451451
Title = i.NavigationTitle,
452452
Url = i.Url
453453
}).Reverse().ToArray(),
454-
Headings = headings
454+
Headings = headings,
455+
Hidden = fileContext.NavigationItem.Hidden
455456
};
456457

457458
AssignDocumentMetadata(doc);

src/api/Elastic.Documentation.Api.Infrastructure/Adapters/Search/ElasticsearchGateway.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ internal sealed record DocumentDto
4848

4949
[JsonPropertyName("applies_to")]
5050
public ApplicableTo? Applies { get; init; }
51+
52+
[JsonPropertyName("hidden")]
53+
public bool Hidden { get; init; } = false;
5154
}
5255

5356
internal sealed record ParentDocumentDto
@@ -100,7 +103,8 @@ private static Query BuildLexicalQuery(string searchQuery) =>
100103
|| new MatchQuery(Infer.Field<DocumentDto>(f => f.Parents.First().Title), searchQuery) { Boost = 2.0f }
101104
|| new MatchQuery(Infer.Field<DocumentDto>(f => f.Title), searchQuery) { Fuzziness = 1, Boost = 1.0f }
102105
)
103-
&& !(Query)new TermsQuery(Infer.Field<DocumentDto>(f => f.Url.Suffix("keyword")), new TermsQueryField(["/docs", "/docs/", "/docs/404", "/docs/404/"]));
106+
&& !(Query)new TermsQuery(Infer.Field<DocumentDto>(f => f.Url.Suffix("keyword")), new TermsQueryField(["/docs", "/docs/", "/docs/404", "/docs/404/"]))
107+
&& !(Query)new TermQuery { Field = Infer.Field<DocumentDto>(f => f.Hidden), Value = true };
104108

105109
/// <summary>
106110
/// Builds the semantic search query for the given search term.
@@ -110,7 +114,8 @@ private static Query BuildSemanticQuery(string searchQuery) =>
110114
|| new SemanticQuery("abstract.semantic_text", searchQuery) { Boost = 3.0f }
111115
)
112116
&& !(Query)new TermsQuery(Infer.Field<DocumentDto>(f => f.Url.Suffix("keyword")),
113-
new TermsQueryField(["/docs", "/docs/", "/docs/404", "/docs/404/"]));
117+
new TermsQueryField(["/docs", "/docs/", "/docs/404", "/docs/404/"]))
118+
&& !(Query)new TermQuery { Field = Infer.Field<DocumentDto>(f => f.Hidden), Value = true };
114119

115120
/// <summary>
116121
/// Normalizes the search query by replacing "dotnet" with "net".

0 commit comments

Comments
 (0)