Posted by markus_petrux on October 30, 2008 at 7:02pm
Jump to:
| Project: | Sphinx search |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | task |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
This task is part of #306959: Porting sphinxsearch module to D6 (and Battle Plan).
Post #1 will be holding the draft on how hook_sphinxsearch_api() will work. Next posts may contain updates on progress, ideas, etc. Feel free to chime in if you wish.
Comments
#1
--- DRAFT ---
hook_sphinxsearch_api() is that little thing that should help us deal with almost any kind of content, and of course, allow external modules extend the default sphinxseacrh module behaviour. We need this to:
- Describe the schema for XMLPipe documents. Status: DONE.
- Generate the data for fields and attributes used to generate XMLPipe documents. Status: DONE.
- Generate the form element for the advanced search page. Status: TODO.
- Parse GET/POST request to convert values into search options. Status: TODO.
- Convert search options into GET arguments. Status: TODO.
- Convert search options into something that can be used to build Sphinx search queries. Status: TODO.
- Define if and how Sphinx document attributes appear in faceted blocks (guided search). Status: TODO.
To be continued...
PS: I'm coding these features in parallell, and then commit to CVS when I feel something is stable enough.
#2
I have commited today the first attempt to implement hook_sphinxsearch_api(). For the moment, it basically works like nodeapi, but applied to what we need here.
Hook operation 'sphinx_schema' and 'sphinx_document' are implemented already. And the XMLPipe generator is using these to build indexes. So that means, we could implement modules that expose node language, workflow attributes, CCK fields, etc. and it all would be indexed automagically. :)
That was the easy part of it. :( lol
I have updated the draft above to include a description about the rest of the things the API should do for us.
#3
I use this module on my website http://www.yilar.com and like it very much, thank you for your great work.
to make the module more flexible, i suggest to replace sphinxsearch_invoke_api with:
1) module_invoke_all
2)drupal_alter
for function call part:
a. sphinxsearch_xmlpipe_header() modified as follow:
function sphinxsearch_xmlpipe_header() {
// Collect information about Sphinx document fields and attributes.
$sphinx_schema = module_invoke_all('sphinx_schema') ;
drupal_alter('sphinx_schema', $sphinx_schema);
$GLOBALS['sphinxsearch_xmlpipe_schema'] = array_merge($sphinx_schema, array(
// Flag to take care of deleted documents until main indexes are rebuilt.
'is_deleted' => array('sphinx_type' => 'bool', 'sphinx_default' => 0),
// Main Index ID is used for delta index processing.
'main_index_id' => array('sphinx_type' => 'int'),
));
...
b. function sphinxsearch_xmlpipe_document($main_index_id, $nid) modified as follow
function sphinxsearch_xmlpipe_document($main_index_id, $nid) {
// Load the node (reseting the node_load cache).
if (!($node = node_load($nid, NULL, TRUE))) {
return FALSE;
}
// Collect Sphinx document data.
$document = module_invoke_all('sphinx_document', $node);
drupal_alter('sphinx_document', $document, $node);
$document = array_merge($document, array(
'main_index_id' => $main_index_id,
));
...
c. function sphinxsearch_generate_xmlpipe_main($main_index_id, $first_nid, $last_nid) change the NODE NIDS QUERY to an alterable way:
...// Load the nids we are about to process within current loop.
$range_end = min($range_start + $range_step, $last_nid);
$nids = array();
$index_condition = module_invoke_all('sphinx_index_condition');
drupal_alter('sphinx_index_condition', $index_condition);
$index_condition = implode(' AND ', $index_condition);
$result = db_query('SELECT nid FROM {node}
WHERE '. $index_condition .'
AND nid >= %d AND nid <= %d
ORDER BY nid ASC', array($range_start, $range_end));
...
for hooks, they will looks like:
/**
* Implementation of hook_sphinx_schema().
*/
function sphinxsearch_sphinx_schema(){
return array(...);
}
/**
** Implementation of hook_sphinx_document()
**/
function sphinxsearch_sphinx_document(){
...
return array(...);
...
}
/**
**Implementation of hook_sphinx_index_condition()
**/
function sphinxsearch_sphinx_index_condition(){
return array('status'=>'status = 1', 'node_type' => sphinxsearch_get_enabled_node_types_condition());
}
example of alter hooks
/**
* Implementation of hook_sphinx_schema_alter().
*/
function classified_ad_sphinx_schema_alter(&$sphinx_schema){
if($GLOBALS['sphinxsearch_index_schema']=='classified_ad'){
$sphinx_schema['offer'] = array('sphinx_type' => 'bool', 'sphinx_default' => 1);
}
}
/**
* Implementation of hook_sphinx_document_alter().
*/
function classified_ad_sphinx_document_alter(&$document, $node){
if($GLOBALS['sphinxsearch_index_schema']=='classified_ad'){
$document['offer'] = $node->offering;
}
}
/**
* Implementation of hook_sphinx_index_condition_alter().
*/
function classified_ad_sphinx_index_condition_alter(&$index_condition){
if($GLOBALS['sphinxsearch_index_schema']=='classified_ad'){
$index_condition['node_type'] = "type = 'classified_ad'";
}
}
above idea popup while i am thinking to make customize index and search based on content type.
#4
good job!
I am thinking, if blow can be done in one step instead of 3 by changing the search form to GET method?
- Parse GET/POST request to convert values into search options. Status: TODO.
- Convert search options into GET arguments. Status: TODO.
- Convert search options into something that can be used to build Sphinx search queries. Status: TODO.