Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upstream issue #11961: alternative de-duplication approach #1

Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions sphinx/themes/basic/static/searchtools.js
Expand Up @@ -179,7 +179,8 @@ const Search = {
if (Search._queued_query !== null) {
const query = Search._queued_query;
Search._queued_query = null;
Search.query(query);
let { results, searchTerms, highlightTerms } = Search.query(query);
_displayNextItem(results, results.length, searchTerms, highlightTerms);
}
},

Expand Down Expand Up @@ -227,8 +228,12 @@ const Search = {
Search.startPulse();

// index already loaded, the browser was quick!
if (Search.hasIndex()) Search.query(query);
else Search.deferQuery(query);
if (Search.hasIndex()) {
let { results, searchTerms, highlightTerms } = Search.query(query);
_displayNextItem(results, results.length, searchTerms, highlightTerms);
} else {
Search.deferQuery(query);
}
},

/**
Expand Down Expand Up @@ -345,7 +350,8 @@ const Search = {
// note the reversing of results, so that in the case of duplicates, the highest-scoring entry is kept
let seen = new Set();
results = results.reverse().reduce((acc, result) => {
let resultStr = result.slice(0, 4).concat([result[5]]).map(v => String(v)).join(',');
const [docName, _title, anchor, descr, _score, filename] = result;
const resultStr = `${docName},${anchor},${descr},${filename}`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I don't see how this would pass the test below. 🤔 The body search and title search will have different resultStr's (since they have different anchors), so you'd see them both in the results.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test does succeed with that - could that imply a bug in the test?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's only one search result because my other fixes haven't been applied (I left them out for ease of review). If you rebase your branch on top of these pull requests the test should start failing:

sphinx-doc#11960
sphinx-doc#11958

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Are you sure? I can still get the test to pass with a docid of 1 instead of zero, and the partial-matching only affects the scoring, if I understand correctly?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took a look, I think it's actually sphinx-doc#11959 (the multiple matches) that allows multiple results to be generated as you'd expect. If you apply this squashed patch your tests should start failing: https://gist.github.com/wlach/74124608c2113d8eb9737cdced4762db

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug raised as sphinx-doc#11965.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A further change may be needed to duplicate between those client-side, yes, but I think it could be simplified (more like e2d07a1) if the duplicate titles index entries are removed.

Hmm, I'm confused-- that looks similar to my original solution which you rejected here:

sphinx-doc#11942 (comment)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not ideal, but it's smaller -- there's no isDocumentTitle, and no additional boolean passed down to the de-duplication phase.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and in fact, it might not even be required. if the index-construction issue is valid and fixed, then we could try removing it and relying purely on an updated de-duplication key)

Copy link
Owner

@wlach wlach Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not ideal, but it's smaller -- there's no isDocumentTitle, and no additional boolean passed down to the de-duplication phase.

Ok, I don't have a super strong opinion: I thought your original critique had a point but I don't think it matters all that much in the end.

(and in fact, it might not even be required. if the index-construction issue is valid and fixed, then we could try removing it and relying purely on an updated de-duplication key)

I am pretty sure we still have the problem of the title search / term search returning more-or-less the same result. The unit test uses a hand-constructed index without this problem and still reproduces the bug.

if (!seen.has(resultStr)) {
acc.push(result);
seen.add(resultStr);
Expand All @@ -359,8 +365,7 @@ const Search = {
//Search.lastresults = results.slice(); // a copy
// console.info("search results:", Search.lastresults);

// print the results
_displayNextItem(results, results.length, searchTerms);
return { results, searchTerms, highlightTerms };
},

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/js/documentation_options.js
@@ -1 +1,9 @@
const DOCUMENTATION_OPTIONS = {};

// stub Stemmer / stopWords
function Stemmer() {
this.stemWord = function (word) {
return word;
}
};
let stopwords = [];
25 changes: 25 additions & 0 deletions tests/js/searchtools.js
Expand Up @@ -29,6 +29,31 @@ describe('Basic html theme search', function() {

});

describe('query', function() {
it("should not duplicate on title and content", function() {
index = {
alltitles: {
'Main Page': [[0, 'main-page']],
},
docnames:["index"],
filenames:["index.rst"],
indexentries:{},
objects:{},
objtypes: {},
objnames: {},
terms:{main:0, page:0},
titles:["Main Page"],
titleterms:{ main:0, page:0 }
}
Search.setIndex(index);
let { results } = Search.query('main page');
// should only be one result
expect(results).toEqual([
[ 'index', 'Main Page', '#main-page', null, 100, 'index.rst' ],
]);
});
})

});

// This is regression test for https://github.com/sphinx-doc/sphinx/issues/3150
Expand Down