Zend Search Lucene Matches

早过忘川 提交于 2019-12-12 02:05:34

问题


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


回答1:


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
  }


来源:https://stackoverflow.com/questions/7805996/zend-search-lucene-matches

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!