Fix various problems with fulltextContent searches

Including finding items in the wrong library and not finding any items
when paired with the checkboxes in ANY mode
This commit is contained in:
Dan Stillman 2017-07-26 05:35:58 -04:00
parent 36371630b5
commit 61f8a2c3c5
2 changed files with 114 additions and 33 deletions

View file

@ -649,16 +649,21 @@ Zotero.Search.prototype.search = Zotero.Promise.coroutine(function* (asTempTable
}; };
// Regexp mode -- don't use fulltext word index // Regexp mode -- don't use fulltext word index
if (condition.mode && condition.mode.indexOf('regexp') == 0) { if (condition.mode && condition.mode.startsWith('regexp')) {
// In an ANY search, only bother scanning items that // In an ANY search with other conditions that alter the results, only bother
// haven't already been found by the main search // scanning items that haven't already been found by the main search, as long as
if (joinMode == 'any') { // they're in the right library
if (joinMode == 'any' && this._hasPrimaryConditions) {
if (!tmpTable) { if (!tmpTable) {
tmpTable = yield Zotero.Search.idsToTempTable(ids); tmpTable = yield Zotero.Search.idsToTempTable(ids);
} }
var sql = "SELECT GROUP_CONCAT(itemID) FROM items WHERE " var sql = "SELECT GROUP_CONCAT(itemID) FROM items WHERE "
+ "itemID NOT IN (SELECT itemID FROM " + tmpTable + ")"; + "itemID NOT IN (SELECT itemID FROM " + tmpTable + ")";
if (this.libraryID) {
sql += " AND libraryID=" + parseInt(this.libraryID);
}
var res = yield Zotero.DB.valueQueryAsync(sql); var res = yield Zotero.DB.valueQueryAsync(sql);
var scopeIDs = res ? res.split(",").map(id => parseInt(id)) : []; var scopeIDs = res ? res.split(",").map(id => parseInt(id)) : [];
} }
@ -672,6 +677,9 @@ Zotero.Search.prototype.search = Zotero.Promise.coroutine(function* (asTempTable
else { else {
Zotero.debug('Running subsearch against fulltext word index'); Zotero.debug('Running subsearch against fulltext word index');
var s = new Zotero.Search(); var s = new Zotero.Search();
if (this.libraryID) {
s.libraryID = this.libraryID;
}
// Add any necessary conditions to the fulltext word search -- // Add any necessary conditions to the fulltext word search --
// those that are required in an ANY search and any outside the // those that are required in an ANY search and any outside the
@ -748,8 +756,7 @@ Zotero.Search.prototype.search = Zotero.Promise.coroutine(function* (asTempTable
// //
// We only do this if there are primary conditions that alter the // We only do this if there are primary conditions that alter the
// main search, since otherwise all items will match // main search, since otherwise all items will match
if (this._hasPrimaryConditions && if (this._hasPrimaryConditions && (joinMode == 'any' || hasQuicksearch)) {
(joinMode == 'any' || hasQuicksearch) && ids) {
//Zotero.debug("Adding filtered IDs to main set"); //Zotero.debug("Adding filtered IDs to main set");
for (let i=0; i<filteredIDs.length; i++) { for (let i=0; i<filteredIDs.length; i++) {
let id = filteredIDs[i]; let id = filteredIDs[i];
@ -900,13 +907,21 @@ Zotero.Search.idsToTempTable = Zotero.Promise.coroutine(function* (ids) {
var tmpTable = "tmpSearchResults_" + Zotero.randomString(8); var tmpTable = "tmpSearchResults_" + Zotero.randomString(8);
Zotero.debug(`Creating ${tmpTable} with ${ids.length} item${ids.length != 1 ? 's' : ''}`); Zotero.debug(`Creating ${tmpTable} with ${ids.length} item${ids.length != 1 ? 's' : ''}`);
var sql = "CREATE TEMPORARY TABLE " + tmpTable + " AS " var sql = "CREATE TEMPORARY TABLE " + tmpTable;
if (ids.length) {
sql += " AS "
+ "WITH cte(itemID) AS (" + "WITH cte(itemID) AS ("
+ "VALUES " + ids.map(id => "(" + parseInt(id) + ")").join(',') + "VALUES " + ids.map(id => "(" + parseInt(id) + ")").join(',')
+ ") " + ") "
+ "SELECT * FROM cte"; + "SELECT * FROM cte";
}
else {
sql += " (itemID INTEGER PRIMARY KEY)";
}
yield Zotero.DB.queryAsync(sql, false, { debug: false }); yield Zotero.DB.queryAsync(sql, false, { debug: false });
yield Zotero.DB.queryAsync(`CREATE UNIQUE INDEX ${tmpTable}_itemID ON ${tmpTable}(itemID)`); if (ids.length) {
yield Zotero.DB.queryAsync(`CREATE UNIQUE INDEX ${tmpTable}_itemID ON ${tmpTable}(itemID)`);
}
return tmpTable; return tmpTable;
}); });

View file

@ -116,9 +116,12 @@ describe("Zotero.Search", function() {
}); });
describe("#search()", function () { describe("#search()", function () {
let win; var win;
let fooItem; var userLibraryID;
let foobarItem; var fooItem;
var foobarItem;
var fooItemGroup;
var foobarItemGroup;
before(function* () { before(function* () {
// Hidden browser, which requires a browser window, needed for charset detection // Hidden browser, which requires a browser window, needed for charset detection
@ -126,6 +129,11 @@ describe("Zotero.Search", function() {
win = yield loadBrowserWindow(); win = yield loadBrowserWindow();
fooItem = yield importFileAttachment("search/foo.html"); fooItem = yield importFileAttachment("search/foo.html");
foobarItem = yield importFileAttachment("search/foobar.html"); foobarItem = yield importFileAttachment("search/foobar.html");
userLibraryID = fooItem.libraryID;
let group = yield getGroup();
fooItemGroup = yield importFileAttachment("search/foo.html", { libraryID: group.libraryID });
foobarItemGroup = yield importFileAttachment("search/foobar.html", { libraryID: group.libraryID });
}); });
after(function* () { after(function* () {
@ -134,31 +142,11 @@ describe("Zotero.Search", function() {
} }
yield fooItem.eraseTx(); yield fooItem.eraseTx();
yield foobarItem.eraseTx(); yield foobarItem.eraseTx();
yield fooItemGroup.eraseTx();
yield foobarItemGroup.eraseTx();
}); });
describe("Conditions", function () { describe("Conditions", function () {
describe("attachmentContent", function () {
it("should find text in HTML files", function* () {
var s = new Zotero.Search();
s.libraryID = foobarItem.libraryID;
s.addCondition('fulltextContent', 'contains', 'foo bar');
var matches = yield s.search();
assert.sameMembers(matches, [foobarItem.id]);
});
it("should work in subsearch", function* () {
var s = new Zotero.Search();
s.libraryID = foobarItem.libraryID;
s.addCondition('fulltextContent', 'contains', 'foo bar');
var s2 = new Zotero.Search();
s2.setScope(s);
s2.addCondition('title', 'contains', 'foobar');
var matches = yield s2.search();
assert.sameMembers(matches, [foobarItem.id]);
});
});
describe("collection", function () { describe("collection", function () {
it("should find item in collection", function* () { it("should find item in collection", function* () {
var col = yield createDataObject('collection'); var col = yield createDataObject('collection');
@ -210,15 +198,89 @@ describe("Zotero.Search", function() {
describe("fileTypeID", function () { describe("fileTypeID", function () {
it("should search by attachment file type", function* () { it("should search by attachment file type", function* () {
let s = new Zotero.Search(); let s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('fileTypeID', 'is', Zotero.FileTypes.getID('webpage')); s.addCondition('fileTypeID', 'is', Zotero.FileTypes.getID('webpage'));
let matches = yield s.search(); let matches = yield s.search();
assert.sameMembers(matches, [fooItem.id, foobarItem.id]); assert.sameMembers(matches, [fooItem.id, foobarItem.id]);
}); });
}); });
describe("fulltextContent", function () {
it("should find text in HTML files", function* () {
var s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('fulltextContent', 'contains', 'foo bar');
var matches = yield s.search();
assert.sameMembers(matches, [foobarItem.id]);
});
it("should work in subsearch", function* () {
var s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('fulltextContent', 'contains', 'foo bar');
var s2 = new Zotero.Search();
s2.setScope(s);
s2.addCondition('title', 'contains', 'foobar');
var matches = yield s2.search();
assert.sameMembers(matches, [foobarItem.id]);
});
it("should find matching item with joinMode=ANY and non-matching other condition", function* () {
var s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('joinMode', 'any');
s.addCondition('fulltextContent', 'contains', 'foo bar');
s.addCondition('title', 'contains', 'nomatch');
var matches = yield s.search();
assert.sameMembers(matches, [foobarItem.id]);
});
it("should find matching items in regexp mode with joinMode=ANY with matching other condition", function* () {
var s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('joinMode', 'any');
s.addCondition('fulltextContent/regexp', 'contains', 'foo.+bar');
s.addCondition('title', 'is', fooItem.getField('title'));
var matches = yield s.search();
assert.sameMembers(matches, [fooItem.id, foobarItem.id]);
});
it("should find matching item in regexp mode with joinMode=ANY and non-matching other condition", function* () {
var s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('joinMode', 'any');
s.addCondition('fulltextContent/regexp', 'contains', 'foo.+bar');
s.addCondition('title', 'contains', 'nomatch');
var matches = yield s.search();
assert.sameMembers(matches, [foobarItem.id]);
});
it("should find item matching other condition in regexp mode when joinMode=ANY", function* () {
var s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('joinMode', 'any');
s.addCondition('fulltextContent/regexp', 'contains', 'nomatch');
s.addCondition('title', 'is', foobarItem.getField('title'));
var matches = yield s.search();
assert.sameMembers(matches, [foobarItem.id]);
});
it("should find matching item in regexp mode with joinMode=ANY and recursive mode flag", function* () {
var s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('joinMode', 'any');
s.addCondition('fulltextContent/regexp', 'contains', 'foo.+bar');
s.addCondition('recursive', 'true');
var matches = yield s.search();
assert.sameMembers(matches, [foobarItem.id]);
});
});
describe("fulltextWord", function () { describe("fulltextWord", function () {
it("should return matches with full-text conditions", function* () { it("should return matches with full-text conditions", function* () {
let s = new Zotero.Search(); let s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('fulltextWord', 'contains', 'foo'); s.addCondition('fulltextWord', 'contains', 'foo');
let matches = yield s.search(); let matches = yield s.search();
assert.lengthOf(matches, 2); assert.lengthOf(matches, 2);
@ -227,6 +289,7 @@ describe("Zotero.Search", function() {
it("should not return non-matches with full-text conditions", function* () { it("should not return non-matches with full-text conditions", function* () {
let s = new Zotero.Search(); let s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('fulltextWord', 'contains', 'baz'); s.addCondition('fulltextWord', 'contains', 'baz');
let matches = yield s.search(); let matches = yield s.search();
assert.lengthOf(matches, 0); assert.lengthOf(matches, 0);
@ -234,6 +297,7 @@ describe("Zotero.Search", function() {
it("should return matches for full-text conditions in ALL mode", function* () { it("should return matches for full-text conditions in ALL mode", function* () {
let s = new Zotero.Search(); let s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('joinMode', 'all'); s.addCondition('joinMode', 'all');
s.addCondition('fulltextWord', 'contains', 'foo'); s.addCondition('fulltextWord', 'contains', 'foo');
s.addCondition('fulltextWord', 'contains', 'bar'); s.addCondition('fulltextWord', 'contains', 'bar');
@ -243,6 +307,7 @@ describe("Zotero.Search", function() {
it("should not return non-matches for full-text conditions in ALL mode", function* () { it("should not return non-matches for full-text conditions in ALL mode", function* () {
let s = new Zotero.Search(); let s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('joinMode', 'all'); s.addCondition('joinMode', 'all');
s.addCondition('fulltextWord', 'contains', 'mjktkiuewf'); s.addCondition('fulltextWord', 'contains', 'mjktkiuewf');
s.addCondition('fulltextWord', 'contains', 'zijajkvudk'); s.addCondition('fulltextWord', 'contains', 'zijajkvudk');
@ -252,6 +317,7 @@ describe("Zotero.Search", function() {
it("should return a match that satisfies only one of two full-text condition in ANY mode", function* () { it("should return a match that satisfies only one of two full-text condition in ANY mode", function* () {
let s = new Zotero.Search(); let s = new Zotero.Search();
s.libraryID = userLibraryID;
s.addCondition('joinMode', 'any'); s.addCondition('joinMode', 'any');
s.addCondition('fulltextWord', 'contains', 'bar'); s.addCondition('fulltextWord', 'contains', 'bar');
s.addCondition('fulltextWord', 'contains', 'baz'); s.addCondition('fulltextWord', 'contains', 'baz');