查询倒排文件索引

问题描述 投票:0回答:1

我在学校有一个项目,我需要使用倒排索引创建一个搜索引擎,但我对如何继续感到有点困惑。

我使用倒排文件索引存储了文档(4 个文档)中的所有单词,但在某种程度上,特定文件中的每个单词都有一行,所以假设单词“like”可以在文档 2 中出现 3 次, doc 4 两次 - 所以它将有 2 行,word:like docid:2 hit:3 instoplist:0 和 word:like docid:4 hit:2 instopelist:0 (hit 是单词在文档中出现的次数,如果它是停止列表中单词的一部分,则为停止列表)。

现在我需要能够对该索引进行查询。 假设我需要找到 - 汽车和(摩托车或自行车) 这是最好的方法吗?我该如何写搜索的顺序?我怎么知道先拿摩托车和自行车,然后在它们之间做“或”,然后再用汽车做“与”?

*ps-使用php来编写代码

我将不胜感激任何形式的帮助,

谢谢

php search-engine querying inverted-index
1个回答
2
投票

您可以使用包含汽车的文档与(包含摩托车或自行车的文档的并集)的交集

汽车:doc1、doc2、doc3

摩托车:doc1、doc4

自行车:doc1、doc2

所以你的最终文档列表应该是 doc1, doc2

用于在 php 中查找交集和并集。 假设您有 3 个数组 $car 、 $motorcycle 和 $bicycle ,其中包含包含这些单词的文档

<?php

  $car = ['doc1','doc2','doc3'];

  $motorcycle = ['doc1','doc4'];

  $bicycle = ['doc1','doc2'];

  $intersect = array_merge($motorcycle, $bicycle);
  $result = array_intersect($car , $intersect);


  for($x = 0; $x < count($result); $x++) {
      echo $result[$x];
      echo "<br>";
  }

?>
© www.soinside.com 2019 - 2024. All rights reserved.