Skip to content

Commit 5d174f2

Browse files
mw-dingzfy0701
authored andcommitted
[Code] Show some content for file name matching (#32958)
1 parent d3061e9 commit 5d174f2

File tree

2 files changed

+70
-23
lines changed

2 files changed

+70
-23
lines changed

x-pack/plugins/code/server/search/document_search_client.test.ts

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ function initSearchClient() {
2222
}
2323

2424
const mockSearchResults = [
25-
// 1. The first response is a valid DocumentSearchResult with 1 doc
25+
// 1. The first response is a valid DocumentSearchResult with 2 docs
2626
{
2727
took: 1,
2828
hits: {
2929
total: {
30-
value: 1,
30+
value: 2,
3131
},
3232
hits: [
33+
// File content matching
3334
{
3435
_source: {
3536
repoUri: 'github.com/Microsoft/TypeScript-Node-Starter',
@@ -45,20 +46,34 @@ const mockSearchResults = [
4546
],
4647
},
4748
},
49+
// File path matching
50+
{
51+
_source: {
52+
repoUri: 'github.com/Microsoft/TypeScript-Node-Starter',
53+
path: 'src/types/string.d.ts',
54+
content:
55+
'no query in content;\nno query in content;\nno query in content;\nno query in content;\nno query in content;\n',
56+
language: 'typescript',
57+
qnames: ['express-flash'],
58+
},
59+
highlight: {
60+
content: [],
61+
},
62+
},
4863
],
4964
},
5065
aggregations: {
5166
repoUri: {
5267
buckets: [
5368
{
54-
'github.com/Microsoft/TypeScript-Node-Starter': 1,
69+
'github.com/Microsoft/TypeScript-Node-Starter': 2,
5570
},
5671
],
5772
},
5873
language: {
5974
buckets: [
6075
{
61-
typescript: 1,
76+
typescript: 2,
6277
},
6378
],
6479
},
@@ -105,12 +120,12 @@ beforeEach(() => {
105120
initSearchClient();
106121
});
107122

108-
test('Repository search', async () => {
123+
test('Document search', async () => {
109124
// 1. The first response should have 1 result.
110125
const responseWithResult = await docSearchClient.search({ query: 'string', page: 1 });
111126
expect(responseWithResult).toEqual(
112127
expect.objectContaining({
113-
total: 1,
128+
total: 2,
114129
totalPage: 1,
115130
page: 1,
116131
query: 'string',
@@ -136,15 +151,29 @@ test('Repository search', async () => {
136151
language: 'typescript',
137152
hits: 1,
138153
},
154+
{
155+
uri: 'github.com/Microsoft/TypeScript-Node-Starter',
156+
filePath: 'src/types/string.d.ts',
157+
compositeContent: {
158+
// Content is shorted
159+
content: 'no query in content;\nno query in content;\nno query in content;\n',
160+
// Line mapping data is populated
161+
lineMapping: ['1', '2', '3', '..'],
162+
// Highlight ranges are calculated
163+
ranges: [],
164+
},
165+
language: 'typescript',
166+
hits: 0,
167+
},
139168
],
140169
repoAggregations: [
141170
{
142-
'github.com/Microsoft/TypeScript-Node-Starter': 1,
171+
'github.com/Microsoft/TypeScript-Node-Starter': 2,
143172
},
144173
],
145174
langAggregations: [
146175
{
147-
typescript: 1,
176+
typescript: 2,
148177
},
149178
],
150179
})
@@ -156,12 +185,12 @@ test('Repository search', async () => {
156185
expect(responseWithEmptyResult.total).toEqual(0);
157186
});
158187

159-
test('Repository suggest', async () => {
188+
test('Document suggest', async () => {
160189
// 1. The first response should have 1 result.
161190
const responseWithResult = await docSearchClient.suggest({ query: 'string', page: 1 });
162191
expect(responseWithResult).toEqual(
163192
expect.objectContaining({
164-
total: 1,
193+
total: 2,
165194
totalPage: 1,
166195
page: 1,
167196
query: 'string',
@@ -178,6 +207,18 @@ test('Repository suggest', async () => {
178207
language: 'typescript',
179208
hits: 0,
180209
},
210+
{
211+
uri: 'github.com/Microsoft/TypeScript-Node-Starter',
212+
filePath: 'src/types/string.d.ts',
213+
// compositeContent field is intended to leave empty.
214+
compositeContent: {
215+
content: '',
216+
lineMapping: [],
217+
ranges: [],
218+
},
219+
language: 'typescript',
220+
hits: 0,
221+
},
181222
],
182223
})
183224
);

x-pack/plugins/code/server/search/document_search_client.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const MAX_HIT_NUMBER = 5;
3131

3232
export class DocumentSearchClient extends AbstractSearchClient {
3333
private HIGHLIGHT_TAG = '_@_';
34+
private LINE_SEPARATOR = '\n';
3435

3536
constructor(protected readonly client: EsClient, protected readonly log: Logger) {
3637
super(client, log);
@@ -302,24 +303,29 @@ export class DocumentSearchClient extends AbstractSearchClient {
302303
}
303304

304305
private getSourceContent(hitsContent: SourceHit[], doc: Document) {
306+
const docInLines = doc.content.split(this.LINE_SEPARATOR);
307+
let slicedRanges: LineRange[] = [];
305308
if (hitsContent.length === 0) {
306-
return {
307-
content: '',
308-
lineMapping: [],
309-
ranges: [],
310-
};
309+
// Always add a placeholder range of the first line so that for filepath
310+
// matching search result, we will render some file content.
311+
slicedRanges = [
312+
{
313+
startLine: 0,
314+
endLine: 0,
315+
},
316+
];
317+
} else {
318+
const slicedHighlights = hitsContent.slice(0, MAX_HIT_NUMBER);
319+
slicedRanges = slicedHighlights.map(hit => ({
320+
startLine: hit.range.startLoc.line,
321+
endLine: hit.range.endLoc.line,
322+
}));
311323
}
312324

313-
const slicedHighlights = hitsContent.slice(0, MAX_HIT_NUMBER);
314-
const slicedRanges: LineRange[] = slicedHighlights.map(hit => ({
315-
startLine: hit.range.startLoc.line,
316-
endLine: hit.range.endLoc.line,
317-
}));
318-
319325
const expandedRanges = expandRanges(slicedRanges, HIT_MERGE_LINE_INTERVAL);
320326
const mergedRanges = mergeRanges(expandedRanges);
321327
const lineMapping = new LineMapping();
322-
const result = extractSourceContent(mergedRanges, doc.content.split('\n'), lineMapping);
328+
const result = extractSourceContent(mergedRanges, docInLines, lineMapping);
323329
const ranges: IRange[] = hitsContent
324330
.filter(hit => lineMapping.hasLine(hit.range.startLoc.line))
325331
.map(hit => ({
@@ -329,7 +335,7 @@ export class DocumentSearchClient extends AbstractSearchClient {
329335
endLineNumber: lineMapping.lineNumber(hit.range.endLoc.line),
330336
}));
331337
return {
332-
content: result.join('\n'),
338+
content: result.join(this.LINE_SEPARATOR),
333339
lineMapping: lineMapping.toStringArray(),
334340
ranges,
335341
};

0 commit comments

Comments
 (0)