text: (window.webpackJsonp=window.webpackJsonp||[]).push([[48],{708:function(s,n,e){"use strict";e.r(n);var a=e(2),v=Object(a.a)({},(function(){var s=this,n=s.$createElement,e=s._self._c||n;return e("ContentSlotsDistributor",{attrs:{"slot-key":s.$parent.slotKey}},[e("h4",{attrs:{id:"倒排索引-摘自es文档"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#倒排索引-摘自es文档"}},[s._v("#")]),s._v(" 倒排索引（摘自ES文档）")]),s._v(" "),e("p",[s._v("一个倒排索引由文档中所有不重复词的列表构成，对于其中每个词，有一个包含它的文档列表。")]),s._v(" "),e("p",[s._v("例如，假设我们有两个文档，每个文档的 "),e("code",[s._v("content")]),s._v(" 域包含如下内容：")]),s._v(" "),e("ol",[e("li",[s._v("The quick brown fox jumped over the lazy dog")]),s._v(" "),e("li",[s._v("Quick brown foxes leap over lazy dogs in summer")])]),s._v(" "),e("p",[s._v("为了创建倒排索引，我们首先将每个文档的 "),e("code",[s._v("content")]),s._v(" 域拆分成单独的 词（我们称它为 "),e("code",[s._v("词条")]),s._v(" 或 "),e("code",[s._v("tokens")]),s._v(" ），创建一个包含所有不重复词条的排序列表，然后列出每个词条出现在哪个文档。结果如下所示：")]),s._v(" "),e("div",{staticClass:"language- line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[s._v("Term      Doc_1  Doc_2\n-------------------------\nQuick   |       |  X\nThe     |   X   |\nbrown   |   X   |  X\ndog     |   X   |\ndogs    |       |  X\nfox     |   X   |\nfoxes   |       |  X\nin      |       |  X\njumped  |   X   |\nlazy    |   X   |  X\nleap    |       |  X\nover    |   X   |  X\nquick   |   X   |\nsummer  |       |  X\nthe     |   X   |\n------------------------\n")])]),s._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[s._v("1")]),e("br"),e("span",{staticClass:"line-number"},[s._v("2")]),e("br"),e("span",{staticClass:"line-number"},[s._v("3")]),e("br"),e("span",{staticClass:"line-number"},[s._v("4")]),e("br"),e("span",{staticClass:"line-number"},[s._v("5")]),e("br"),e("span",{staticClass:"line-number"},[s._v("6")]),e("br"),e("span",{staticClass:"line-number"},[s._v("7")]),e("br"),e("span",{staticClass:"line-number"},[s._v("8")]),e("br"),e("span",{staticClass:"line-number"},[s._v("9")]),e("br"),e("span",{staticClass:"line-number"},[s._v("10")]),e("br"),e("span",{staticClass:"line-number"},[s._v("11")]),e("br"),e("span",{staticClass:"line-number"},[s._v("12")]),e("br"),e("span",{staticClass:"line-number"},[s._v("13")]),e("br"),e("span",{staticClass:"line-number"},[s._v("14")]),e("br"),e("span",{staticClass:"line-number"},[s._v("15")]),e("br"),e("span",{staticClass:"line-number"},[s._v("16")]),e("br"),e("span",{staticClass:"line-number"},[s._v("17")]),e("br"),e("span",{staticClass:"line-number"},[s._v("18")]),e("br")])]),e("p",[s._v("现在，如果我们想搜索 "),e("code",[s._v("quick brown")]),s._v(" ，我们只需要查找包含每个词条的文档：")]),s._v(" "),e("div",{staticClass:"language- line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[s._v("Term      Doc_1  Doc_2\n-------------------------\nbrown   |   X   |  X\nquick   |   X   |\n------------------------\nTotal   |   2   |  1\n")])]),s._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[s._v("1")]),e("br"),e("span",{staticClass:"line-number"},[s._v("2")]),e("br"),e("span",{staticClass:"line-number"},[s._v("3")]),e("br"),e("span",{staticClass:"line-number"},[s._v("4")]),e("br"),e("span",{staticClass:"line-number"},[s._v("5")]),e("br"),e("span",{staticClass:"line-number"},[s._v("6")]),e("br")])]),e("p",[s._v("两个文档都匹配，但是第一个文档比第二个匹配度更高。如果我们使用仅计算匹配词条数量的简单 "),e("em",[s._v("相似性算法")]),s._v(" ，那么，我们可以说，对于我们查询的相关性来讲，第一个文档比第二个文档更佳。")]),s._v(" "),e("p",[s._v("但是，我们目前的倒排索引有一些问题：")]),s._v(" "),e("ul",[e("li",[e("code",[s._v("Quick")]),s._v(" 和 "),e("code",[s._v("quick")]),s._v(" 以独立的词条出现，然而用户可能认为它们是相同的词。")]),s._v(" "),e("li",[e("code",[s._v("fox")]),s._v(" 和 "),e("code",[s._v("foxes")]),s._v(" 非常相似, 就像 "),e("code",[s._v("dog")]),s._v(" 和 "),e("code",[s._v("dogs")]),s._v(" ；他们有相同的词根。")]),s._v(" "),e("li",[e("code",[s._v("jumped")]),s._v(" 和 "),e("code",[s._v("leap")]),s._v(", 尽管没有相同的词根，但他们的意思很相近。他们是同义词。")])]),s._v(" "),e("p",[s._v("使用前面的索引搜索 "),e("code",[s._v("+Quick +fox")]),s._v(" 不会得到任何匹配文档。（记住，"),e("code",[s._v("+")]),s._v(" 前缀表明这个词必须存在。）只有同时出现 "),e("code",[s._v("Quick")]),s._v(" 和 "),e("code",[s._v("fox")]),s._v(" 的文档才满足这个查询条件，但是第一个文档包含 "),e("code",[s._v("quick fox")]),s._v(" ，第二个文档包含 "),e("code",[s._v("Quick foxes")]),s._v(" 。")]),s._v(" "),e("p",[s._v("我们的用户可以合理的期望两个文档与查询匹配。我们可以做的更好。")]),s._v(" "),e("p",[s._v("如果我们将词条规范为标准模式，那么我们可以找到与用户搜索的词条不完全一致，但具有足够相关性的文档。例如：")]),s._v(" "),e("ul",[e("li",[e("code",[s._v("Quick")]),s._v(" 可以小写化为 "),e("code",[s._v("quick")]),s._v(" 。")]),s._v(" "),e("li",[e("code",[s._v("foxes")]),s._v(" 可以 "),e("em",[s._v("词干提取")]),s._v(" --变为词根的格式-- 为 "),e("code",[s._v("fox")]),s._v(" 。类似的， "),e("code",[s._v("dogs")]),s._v(" 可以为提取为 "),e("code",[s._v("dog")]),s._v(" 。")]),s._v(" "),e("li",[e("code",[s._v("jumped")]),s._v(" 和 "),e("code",[s._v("leap")]),s._v(" 是同义词，可以索引为相同的单词 "),e("code",[s._v("jump")]),s._v(" 。")])]),s._v(" "),e("p",[s._v("现在索引看上去像这样：")]),s._v(" "),e("div",{staticClass:"language- line-numbers-mode"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[s._v("Term      Doc_1  Doc_2\n-------------------------\nbrown   |   X   |  X\ndog     |   X   |  X\nfox     |   X   |  X\nin      |       |  X\njump    |   X   |  X\nlazy    |   X   |  X\nover    |   X   |  X\nquick   |   X   |  X\nsummer  |       |  X\nthe     |   X   |  X\n------------------------\n")])]),s._v(" "),e("div",{staticClass:"line-numbers-wrapper"},[e("span",{staticClass:"line-number"},[s._v("1")]),e("br"),e("span",{staticClass:"line-number"},[s._v("2")]),e("br"),e("span",{staticClass:"line-number"},[s._v("3")]),e("br"),e("span",{staticClass:"line-number"},[s._v("4")]),e("br"),e("span",{staticClass:"line-number"},[s._v("5")]),e("br"),e("span",{staticClass:"line-number"},[s._v("6")]),e("br"),e("span",{staticClass:"line-number"},[s._v("7")]),e("br"),e("span",{staticClass:"line-number"},[s._v("8")]),e("br"),e("span",{staticClass:"line-number"},[s._v("9")]),e("br"),e("span",{staticClass:"line-number"},[s._v("10")]),e("br"),e("span",{staticClass:"line-number"},[s._v("11")]),e("br"),e("span",{staticClass:"line-number"},[s._v("12")]),e("br"),e("span",{staticClass:"line-number"},[s._v("13")]),e("br")])]),e("p",[s._v("这还远远不够。我们搜索 "),e("code",[s._v("+Quick +fox")]),s._v(" "),e("em",[s._v("仍然")]),s._v(" 会失败，因为在我们的索引中，已经没有 "),e("code",[s._v("Quick")]),s._v(" 了。但是，如果我们对搜索的字符串使用与 "),e("code",[s._v("content")]),s._v(" 域相同的标准化规则，会变成查询 "),e("code",[s._v("+quick +fox")]),s._v(" ，这样两个文档都会匹配！")])])}),[],!1,null,null,null);n.default=v.exports}}]);