10

I'm trying to replace the default search with a LIKE clause in Drupal 7. I tried to alter the query according to Adding an OR condition to an existing query:

function MYMODULE_query_node_access_alter(QueryAlterableInterface $query) {
  foreach ($query->getTables() as $table) {
    // LIKE for search results.
    if ($table['table'] == 'search_index') {
      // Get the query args and then the search term
      $args =& $query->getArguments();
      $search = $args[':db_condition_placeholder_1'];

      // Get a reference to the existing query conditions.
      $conditions =& $query->conditions();

      // Save the former conditions
      $former_conditions = $conditions;

      // Reset the condition array. It needs a default #conjunction for which AND is fine
      $conditions = array('#conjunction' => array_shift($former_conditions));

      // Replace the search condition in the query
      foreach ($former_conditions as $key => $condition) {
        if ($key != 1) {
          $query->condition($condition['field'], $condition['value'], $condition['operator']);
        }
        else {
          $query->condition('i.word', '%' . db_like($search) . '%', 'LIKE');
        }
      }
    }
  }
}

Searching with the word "declaration" displays the same results as the default drupal search, but searching with "decl" doesn't find any results.

Any ideas why my code isn't working?

  • 1
    drupal.org/project/fuzzysearch module should solve your problem. You can give it a try.. – Anil Sagar Sep 12 '12 at 8:18
  • Thank you. I would prefer a solution with hook_query_alter (if possible) as the search form and result page are already set up. I'ld also like to know why my code isn't working for other use cases. – user9932 Sep 12 '12 at 9:15
  • It might seem like you're 90% of the way there and just need this final piece to make it work, but I think you're going at it the wrong way. There are lots of search modules to deal with all kinds of use cases, and I'm sure you'll find one to solve yours. Using this kind of intervention will likely lead to confusion and unmaintainability. – Alan Dixon Feb 13 '15 at 14:33
  • Have you ever tried by printing what is carried in $search variable? $search = $args[':db_condition_placeholder_1']; If it is from views go for hook_views_query_alter() it would be simple. – user46183 Apr 20 '15 at 13:51
0

Have you tried changing the hook you are using to MYMODULE_query_alter?

You are implementing hook_query_TAG_alter(), and I don't see where the Search query is tagged as such.

Per the Node API:

This is the hook_query_alter() for queries tagged with 'node_access'. It adds node access checks for the user account given by the 'account' meta-data (or global $user if not provided), for an operation given by the 'op' meta-data (or 'view' if not provided; other possible values are 'update' and 'delete')

0
module_query_tagName_tag_alter(QueryAlterableInterface $query)

Use this and put a tag name in view->query setting. By using this you can differentiates views too.

0

To add a new condition you can also try the above with add_where

$query->add_where(1,'i.word', '%' . db_like($search) . '%', 'LIKE');

I hope this will give the result you are looking for.

Not the answer you're looking for? Browse other questions tagged or ask your own question.