Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict drupal search from indexing all content types?

Tags:

php

search

drupal

I only want certain nodes to be indexed. The "search config" module claims to have this capability, but it doesn't work. So, how do I either edit the node module to only index certain nodes, or better yet, impliment a module that can do this for me?

like image 923
coderama Avatar asked Nov 01 '09 14:11

coderama


2 Answers

This is a long standing feature request, but it looks like it has already been pushed to at least Drupal 8 :/

You can find some workaround suggestions in the feature request discussion linked above, but the 'standard' approach used by the search config module is described here. It does not prevent the nodes from being indexed, but manipulates the search queries to ignore certain entries (e.g. node types) so that they won't show up on the search result pages.

As there is no obviously better solution at the moment (afaik), I agree with ceejayoz´ comment that you should first check why the search config module does not work for you before embarking on custom coding your own solution.

If you have to resort to editing the node module itself, node_update_index() would be the place to start.

like image 111
Henrik Opel Avatar answered Oct 21 '22 03:10

Henrik Opel


Here's the query that determines what needs to be indexed, from node_update_index():

SELECT n.nid FROM {node} n 
  LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid 
  WHERE d.sid IS NULL OR d.reindex <> 0
  ORDER BY d.reindex ASC, n.nid ASC

You can't prevent the indexing script from running. But you can trick that query into thinking content is already indexed by inserting dummy entries into {search_dataset}.

For example, if you're using MySQL, do this on cron:

INSERT INTO {search_dataset} 
  (sid, type, data, reindex)
  SELECT nid, 'node', '', 0 FROM {node} WHERE node.type IN (RESTRICTED_TYPES)
  ON DUPLICATE KEY UPDATE reindex = 0, data = ''

Replace "RESTRICTED_TYPES" with your list of node types, and do a similar query for each entity you want to restrict from search.

like image 45
aaronbauman Avatar answered Oct 21 '22 02:10

aaronbauman