Jump to content

Displaying search results with the text surrounding the query


Recommended Posts

Posted

Hi all, I'm not sure how to proceed on the following.

I have a ajax-driven search working on my site, which works like a charm. My site is a historic overview and as such content-rich site about cities/villages (template: areas) and it's children: locations (homes, churches, etc) located in those areas. (Sadly I can't show off the site yet, but the release is getting closer and closer.)

My query is the following:

$matches = $pages->find("title|body|short|keyword|photos.description|remarks*=$q, limit=50, template=area|location, sort=template"); 

I currently loop through the pages, displaying the title and the short fields. However, the result of this may be that a user has no idea how his search for city A can yield city B showing up in the list, while not showing as to why this happened. (Technically this can happen when city A is mentioned in the body text of city B.) This can be very confusing for people, who are very used to Google's way of returning results. 

So I would like to be able to show the bit of the body text which shows the query instead of the short, whenever this is applicable. Is there a way to know which part of full query resulted it showing up in the search results? Or should I split up my query into separate ones? And/or should I do some regex magic on all the returned fields to figure out which bit contains the query?

Thank you for your thoughts.

  • 1 month later...
Posted

I'd go with something like this:

foreach($matches as $match){

  foreach(["title", "body", "short", "keyword", …] as $field){

    if($match->is("$field*=$q")){
      $textposition_in_field[] = array($field => strpos($match->$field, $q));
    }
  }
}

After that one can decide which textposition will be the most important.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...