Zend 搜索 Lucene 匹配

发布于 2024-12-10 15:55:22 字数 184 浏览 1 评论 0原文

好吧,假设我有一个搜索查询,它返回一些 Zend_Search_Lucene_Search_QueryHit 对象,其中包含与查询匹配的 Zend_Search_Lucene_Document 对象。 我有一个小问题,关于如何从与查询 str 匹配的文档中检索字段的名称以突出显示它? 我希望一切都清楚,并且解决起来不是那么明显:)... 多谢 亚历克斯

OK so let's say I have a search query giving me back some Zend_Search_Lucene_Search_QueryHit objects containing the Zend_Search_Lucene_Document object matching the query.
I have a small question about how to retrieve simply the name of the field from the document matching the query str in order to highlight it??
I hope everything's clear and not to obvious to resolve :)...
Thanks a lot
Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞梦里开花 2024-12-17 15:55:22

假设“name”、“address”、“phone”是 Zend_Search_Lucene_Field::Text 类型的字段,并且您有一个名为“content”的 Zend_Search_Lucene_Field::Unstored 字段 这可以使用“strpos”轻松解决":

    $hits = $index->find($query);

    foreach ($hits as $hit) {
      $result = array();
      if (strpos($hit->name , $query)) {
        $result[$hit->id]['name'] = $query->highlightMatches($hit->name)
      } else {
        $result[$hit->id]['name'] = $hit->name
      }

      if (strpos($hit->address, $query)) {
        $result[$hit->id]['address'] = $query->highlightMatches($hit->address)
      } else {
        $result[$hit->id]['address'] = $hit->address
      }

      if (strpos($hit->phone, $query)) {
        $result[$hit->id]['phone'] = $query->highlightMatches($hit->phone)
      } else {
        $result[$hit->id]['phone'] = $hit->phone
      }
   }

您的电话字段可能是 Zend_Search_Lucene_Field::keyword 或其他内容。如果这些前面的字段都没有突出显示,则意味着您在内容中找到了 $query 字符串,但该字符串未保存,因为它是 Zend_Search_Lucene_Field::Unstored 字段。然后你应该做的是将另一种类型的 Zend_Search_Lucene_Field::Text 字段添加到你的 $doc 中,并将其称为“摘录”,

$doc->addField(Zend_Search_Lucene_Field::Text('excerpt', substr($content, 0, 100)));

并将其添加到你之前的“foreach”循环中

if (strpos($hit->excerpt, $query)) {
    $result[$hit->id]['excerpt'] = $query->highlightMatches($hit->excerpt)
  } else {
    $result[$hit->id]['excerpt'] = $hit->excerpt
  }

Assuming "name", "address", "phone" are your fields of type Zend_Search_Lucene_Field::Text and you have one single field Zend_Search_Lucene_Field::Unstored called "content" This could be easily solved using "strpos":

    $hits = $index->find($query);

    foreach ($hits as $hit) {
      $result = array();
      if (strpos($hit->name , $query)) {
        $result[$hit->id]['name'] = $query->highlightMatches($hit->name)
      } else {
        $result[$hit->id]['name'] = $hit->name
      }

      if (strpos($hit->address, $query)) {
        $result[$hit->id]['address'] = $query->highlightMatches($hit->address)
      } else {
        $result[$hit->id]['address'] = $hit->address
      }

      if (strpos($hit->phone, $query)) {
        $result[$hit->id]['phone'] = $query->highlightMatches($hit->phone)
      } else {
        $result[$hit->id]['phone'] = $hit->phone
      }
   }

your phone field might be a Zend_Search_Lucene_Field::keyword or something else. if none of these previous fields was highlighted it means that you $query string was found in your content which is not saved because it's a Zend_Search_Lucene_Field::Unstored field. what you should do then is add another type Zend_Search_Lucene_Field::Text field to your $doc and call it "excerpt"

$doc->addField(Zend_Search_Lucene_Field::Text('excerpt', substr($content, 0, 100)));

and add this to you previous "foreach" loop

if (strpos($hit->excerpt, $query)) {
    $result[$hit->id]['excerpt'] = $query->highlightMatches($hit->excerpt)
  } else {
    $result[$hit->id]['excerpt'] = $hit->excerpt
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文