cultuurnet/culturefeed — pull request #12 by pfrenssen (2013-08-21 18:28:09+00:00)
Title: Allow easier altering and retrieving of search queries

I need to be able to alter the search query, as well as to retrieve the current search query terms to display them elsewhere on the page.

The current implementation has a few shortcomings, I'll explain below, marking my comments with `##`:

```php
/**
* Execute the search for current page.
*/
protected function execute($params) {

// ...

## Why is this being set here and not in $this->initialize(), like the others?
if ('' == $params['search']) {
$params['search'] = '*:*';
}
## If no search query was given, '*.*' is added to $query property here.
## I would have to strip this out again on my side if I want to retrieve the
## search parameters.
$this->query[] = $params['search'];

## At this point the search query is added to the parameters, before I
## had the chance to alter the query. This means I have to loop over the
## parameters and detect which one is a Parameter\Query class,
## strip it out, change it to my liking, then add it again. This is quite
## involved as I need to instantiate a new CultuurNet\Search\Parameter\Query
## and add it to $parameters in the alter hook. I just want to change the
## search query :(
$this->parameters[] = new Parameter\Query(implode(' AND ', $this->query));

## This boost is added here to $this->query. I also have to strip out this
## ugly string if I want to retrieve the original search keywords to display
## elsewhere on the page. Note that this code doesn't work. It is not being
## added to $this->parameters since the query has already been added
## to the parameters above.
// Add in a boost for sort-type "relevancy".
if ($params['sort'] == 'relevancy') {
## Eek! This is not developer friendly, I want an API to add boosts
## and other Solr local parameters, without having to extend this class,
## and I don't like the idea of having to juggle with strings in the $query array.
$this->query[0] = '{!boost%20b=sum(recommend_count,product(comment_count,10))}' . $this->query[0];
}

## What's the point of passing $this->query here if it is not going to be used?
drupal_alter('culturefeed_search_query', $this->parameters, $this->query);

$searchService = culturefeed_get_search_service();
$this->result = $searchService->search($this->parameters);
$this->facetComponent->obtainResults($this->result);

}
```

To solve this I propose the following:

* Don't mess with $this->query. Leave it pristine, and add a preparation method that will get it ready for inclusion in the search parameters without changing it. Provide getters and setters so it can be retrieved and set from the procedural side.
* Only add the query to the parameters after invoking the alter hook. Developers can then easily alter the query array.
* Set the search query during initialize() instead of execute().
* Provide an API to manipulate the Solr local parameters (e.g. boost) that can be used from the procedural side.
* Fix the boost. It is not added to the search at the moment.

What I did not touch because I'm afraid that it might break existing implementations:
* I did not change the alter hook. It would be handy if the local parameters could be altered too.
* The execute() method should be decoupled from initialize(), so that the object can be manipulated easily before executing the actual query. The alter hook covers my needs for the moment.