Wildcard searching with Dismax

Events happening in the community are now at Drupal community events on www.drupal.org.
karma.code's picture

Hey all,
A wildcard question... I am trying to do an autocomplete text field implementation of SOLR. So, when the user starts typing in a term or a phrase, it will request SOLR results. For example, if a person types in 'car' it should return 'car', 'cars', 'cardigan'.

Now I try doing this:

<?php

function apachesolr_modify_query(){
 
$query->add_filter('fieldname', $value.'*');
}
?>

but no partial term results come back to me. Now I've heard that the DisMax could help but I cannot find any good documentation on this. Can you please lead me to this. Thank you.

Comments

Check out the

robertdouglass's picture

Check out the apachesolr_autocomplete module. Either it will already do what you want, or you can use it as a reference implementation: http://drupal.org/project/apachesolr_autocomplete

Drupal issues

ygerasimov's picture

You can also review issue on d.o regarding partial search http://drupal.org/node/270412

Another approach is to enable edismax search handler and do queries with wildcards. See http://drupal.org/node/713142

I looked at

karma.code's picture

I looked at apachesolr_autocomplete first but it only returns facets. I want it to return searches on terms and phrases. I'll take a look at the two issues above though.

What I did

karma.code's picture

One of my colleagues figured this one out and I wanted to share the solution:

<?php
 
// Query Solr for $keys so that suggestions will always return results.
 
$query = apachesolr_drupal_query($keys);
 
 
// Allow other modules to modify query.
 
apachesolr_modify_query($query, $params, 'solr_ui_autocomplete');
  if (!
$query) {
    return array();
  }
 
 
// Try to contact Solr.
 
try {
   
// coming from apachesolr.module
   
$solr = apachesolr_get_solr();
  }
  catch (
Exception $e) {
   
watchdog('Apache Solr', $e->getMessage(), NULL, WATCHDOG_ERROR);
    return array();
  }

 
// Query Solr and set the wildcard for SOLR
 
$params['qt'] = 'standard';
 
$params['defType'] = 'lucene';
 
 
$wildcard = "*";

 
// strip out all the usual crap
 
$keys = str_replace(array(':', '/'), '', $keys);
 
 
$keys = 'title:'.strtolower($keys);
 
$response = $solr->search($keys.$wildcard, $params['start'], $params['rows'], $params);
?>

Changing the params['qt'] to standard and defType to lucene was the critical part of this solution. Thanks to all who tried.

A better solution as

pwolanin's picture

A better solution as suggested above is to backport the edismax handler from Solr 3.1 to Solr 1.4 and recompile Solr - there are instructions at: http://drupal.org/node/713142

Your solution will not work as you expect, since only the default field will be searched, and the query-time boosts will not work.

Lucene, Nutch and Solr

Group organizers

Group categories

Projects

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: