0
votes

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
exist a specific method: framework.zend.com/manual/en/zend.search.lucene.searching.html. Search "Search Results Highlighting"JellyBelly
Thanks for the hint but I already checked the method. However the highlightMatches method needs to be applied to each field or to an html document but how can I find out directly which field from my hit contains the match?Bill'o
looking around, from what I understand, you can not know which field is involved in research if using dell'hightlight! Read this for a example: ganeshhs.com/zend-framework/…JellyBelly
thanks, so to be clearer, here is my issue: let's assume that I'm searching in a directory of user profiles with user data (name, address, phone, etc...). As a search result, I have a hit object corresponding more or less at a "user object". I'm using this user object to create and display my directory. So when there is a search, I want to display the same directory but with the highlighted field. In other words, I would need to find and highlight the matching field in my "user object" from my "hit object"...Bill'o
Sorry, I do not know a way to accomplish what you ask! :SJellyBelly

1 Answers

0
votes

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
  }