简体   繁体   中英

Zend Search Lucene Matches

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

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
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM