Jump to content

Search results order/sort?


The G
 Share

Recommended Posts

Hi,

I have a bunch of pages and I want to search for words or word parts in their "title" and "tags" field.

The "tags" field is a textarea containing a comma separated list. It's a long story, I tried first using pages for tags, but text search was slow and I don't need them to have their own URL anyway, because I'm using query strings to bundle together all the search+filters+sort+pagination parts.

Anyways... let me give an example: for instance somebody searches for "rome city". I need to get the results in this specific order:

  1. all full words in the title (i.e. both "rome" and "city in the title)
  2. all full words in the title or tags
  3. all word parts in the title (i.e. parts of both "rome" and "city in the title)
  4. all word parts in the title or tags

Right now, I'm using merged PageArrays and it's quite a PITA:

$results = new PageArray();

// this is computed, but to simplify I'm defining it here directly
$searches = array (
  'title~=rome city',
  'title|tags~=rome city',
  'title%=rome,title%=city',
  'title|tags%=rome,title|tags%=city',
);

// the 'filter' part comes from a filter form via the whitelist
// I'm leaving it in place to show that is precomputed
// I took out the sorting and pagination parts
$base_selector = array(
  'base'   => 'parent=1025',
  'search' => '',
  'filter' => implode(',', filterToSelector($filter)),
);

foreach ($searches as $search_lap) {

  // $search_lap_selector is a temporary array of selectors for this round of search
  $search_lap_selector = $base_selector;

  // the current search is inserted into the selectors array
  $search_lap_selector['search'] = $search_lap;

  // this gets the ids of the pages already in $results PageArray
  $cids = array_flip($results->explode('id'));

  // implodeSelector implodes the selector parts in the correct order during conversion to and from the whitelist
  // this loop tests if $clip it's already present in $results PageArray and skips it if so,
  // to preserve the order
  // I tried with import, but every $results->import messes the results order
  foreach ($pages->find(implodeSelector($search_lap_selector)) as $clip)
    if (!isset($cids[$clip->id]))
      $results->add($clip);

}

This gets me the results, but, as I said, it's a PITA and somehow it doesn't look right.

It would have been very nice if I could use selector groups and attach somehow the results count to the selector, so I could use the results count for sorting. Something like

parent=1025, ('title~=rome city'), ('title|tags~=rome city'), ('title%=rome,title%=city'), ('title|tags%=rome,title|tags%=city'), sort=-title_full_matches_count, sort=-title_tags_full_matches_count, sort=-title_part_matches_count, sort=-title_tags_part_matches_count

Does anything like this exists?

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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