0

所以我将 Lunr.js 用于我的搜索功能。一切都很好,但当我搜索“上帝”和“教堂”等某些关键字时,我的搜索时间长得令人无法接受。我的意思是,30 秒甚至超过 1 分钟。我确信一定是我使用的逻辑导致了这种情况,至少我认为是这样。我的搜索功能的目的是搜索用户输入并仅返回找到关键字的句子以及书名和找到 searchQuery 的页面。我的目标是有一个可以离线工作的搜索功能。这是一个科尔多瓦项目。我已经评论了所有内容,以便您可以轻松地遵循逻辑。感谢您的时间和投入!

function searchFunction() {
//Clears results-wrapper Html element
document.querySelector(".results-wrapper").innerHTML = "";

//Store query in localDB
searchQuery = document.querySelector("#search-id").value;
localStorage.setItem("searchQuery", searchQuery);

//Returns from the index only the books which contains the query
var results = idx.search(searchQuery);

//Run through "results" and return all the text where the query is found
results.forEach(function (entry) {

//documents contains the database of all the books from my json file
documents.find(findSearchQuery);

//Searches only the text of the doc/books found in results variable 
function findSearchQuery(doc) {

  //searchQuery is the users input
  let re = new RegExp(searchQuery, "i");
  
  //if the book's name matches reference in "results"
  if (doc.name == entry.ref) {
    //And if the searchQuery is found in Books text
    if (doc.text.match(re)) {
      
      //Break up the block of text into sentences
      var sentences = doc.text.match(/[^\.!\?]+[\.!\?]+/g);

      sentences.forEach(function (sentence) {
        //Find the sentence inside the sentences array and return the one with the searchQuery
        //Populate HTML element "results-wrapper" with the results
        if (sentence.match(re)) {
          var anchor = document.createElement("a");
          anchor.className = "anchorSearchResult";
          anchor.href = doc.href;

          //Create "div" element
          var div = document.createElement("div");
          div.className = "div-test";

          //Creates "h4" element for title
          var h4 = document.createElement("h4");
          var title = document.createTextNode(doc.name);
          h4.className = "title-results";

          //Creates "p" element for sentence
          var textElement = document.createElement(p);
          var searchResult = document.createTextNode(sentence);
          textElement.className = "text-results";

          //Creates "p" element for page
          var p = document.createElement("p");
          var pageResult = document.createTextNode(doc.page);
          p.className = "page-results";

          h4.appendChild(title);
          textElement.appendChild(searchResult);
          p.appendChild(pageResult);

          div.appendChild(h4);
          div.appendChild(textElement);
          div.appendChild(p);

          anchor.appendChild(div);

          document.querySelector(".results-wrapper").appendChild(anchor);

          anchor.addEventListener("click", returnSearchResultId);

          function returnSearchResultId(e) {
            //store selectorId value of document
            localStorage.setItem("selectorId", doc.selectorId);
          }

          // Highlight Function
          var instance = new Mark(
            document.querySelector(".results-wrapper")
          );

          instance.mark(searchQuery, {
            element: "span",
            className: "highlight",
          });
        }
      });
    }
  }
}
});
}

这只是我的“文档”变量中加载的 json 文件的片段。

[
  {
    "name": "Pre-Eleventh-Hour Extra",
    "year": "1941",
    "text": "Pre-\"Eleventh Hour\" Extra MYSTERY OF MYSTERIES EXPOSED!",
    "page": "1TR 2",
    "href": "tracks/tr1.html#page-2.subHeading",
    "selectorId": "#page-2\\.subHeading"
  },
  {
    "name": "Pre-Eleventh-Hour Extra",
    "year": "1941",
    "text": "In the interest of reaching every truth-seeking mind that desires to escape the path that leads to destruction of both body and soul, this tract will be distributed free of charge as long as this issue lasts.",
    "page": "1TR 2",
    "href": "tracks/tr1.html#page-2.1",
    "selectorId": "#page-2\\.1"
  },
  {
    "name": "Pre-Eleventh-Hour Extra",
    "year": "1941",
    "text": "PREFACE PERSONALLY WATCHING FOR EVERY RAY OF LIGHT.",
    "page": "1TR 3",
    "href": "tracks/tr1.html#page-3.preface",
    "selectorId": "#page-3\\.preface"
  },
  {
    "name": "Pre-Eleventh-Hour Extra",
    "year": "1941",
    "text": "One who entrusts to another the investigation of a message from the Lord, is making flesh his arm, and thus is foolishly acting as without a mind of his own. And  ”the mind that depends upon the judgment of others is certain, sooner or later, to be misled. ” -- Education, p. 231.",
    "page": "1TR 3",
    "href": "tracks/tr1.html#page-3.1",
    "selectorId": "#page-3\\.1"
  },
  {
    "name": "Pre-Eleventh-Hour Extra",
    "year": "1941",
    "text": "Similarly, one who allows prejudice to bar him from a candid investigation of anything new, coming in the name of the Lord, is unwittingly an infidel.",
    "page": "1TR 3",
    "href": "tracks/tr1.html#page-3.2",
    "selectorId": "#page-3\\.2"
  },
  {
    "name": "Pre-Eleventh-Hour Extra",
    "year": "1941",
    "text": "Likewise he who is satisfied with his present attainments in the Word of God, says in effect: \"I am rich, and increased with goods, and have need of nothing.",
    "page": "1TR 3",
    "href": "tracks/tr1.html#page-3.3",
    "selectorId": "#page-3\\.3"
  },
  {
    "name": "Pre-Eleventh-Hour Extra",
    "year": "1941",
    "text": "All these, in variously acting out the part which provoked the condemnation written against the Laodiceans, thereby fulfilling the prophecy which they ought not fulfill, are preparing themselves to be spued out (Rev. 3:14-18). And if they continue in their self-satisfied attitude that they have all the truth, and so have need of nothing more, they will spurn every new claimant to truth and toss the message into the discard because it comes through an unexpected channel. Certainly, then, were this tract not the unfolding of prophecy, the fact is inevitable that when the unfoldment did come, they would treat it in like manner, and consequently toss away their salvation!",
    "page": "1TR 3",
    "href": "tracks/tr1.html#page-3.4",
    "selectorId": "#page-3\\.4"
  },
  {
    "name": "Pre-Eleventh-Hour Extra",
    "year": "1941",
    "text": "Throughout the ages, all who have put their trust in the so-called wise men, and foremost Christians of the day, all reputedly godly men, have by these very ones been bereft of the crown of eternal life, as were the Jewish laity in the days of Christ because of their failing to assume full responsibility for their own salvation. Presumptuously trusting in the wisdom of their so-called \"great men,\" they declined to believe in Christ's words \"O Father, Lord of heaven and earth,...Thou hast hid these things from the wise and prudent, and hast revealed them unto babes.\" Matt. 11:25 \"Where is the wise? where is the scribe?...hath not God made foolish the wisdom of this world?\" 1 Cor 1:20.",
    "page": "1TR 4",
    "href": "tracks/tr1.html#page-4.1",
    "selectorId": "#page-4\\.1"
  }
]
4

1 回答 1

1

弄清楚了!正是我从 Mark.js 库中获得的突出功能导致了巨大的延迟。将其注释掉后,一切又变得快速起来。

于 2020-09-06T23:47:02.937 回答