How to debug indexing problems

If your site is not indexing posts, here’s how you can debug it.

First, see if fetching content works. Find this:

foreach ($content as $post) {
	$n += relevanssi_index_doc($post->ID, false, $custom_fields);
	// n calculates the number of insert queries
}

and change it to

$wpdb->print_error();
foreach ($content as $post) {
	$n += relevanssi_index_doc($post->ID, false, $custom_fields);
	var_dump($post->ID);
	// n calculates the number of insert queries
}

Then build the index. If everything works, you should see a list of all post IDs Relevanssi will index. If it doesn’t work, there should be a helpful error message.

If you do get a list of post IDs, but indexing still doesn’t work, find this:

if (!empty($values)) {
	$values = implode(', ', $values);
	$query = "INSERT IGNORE INTO $relevanssi_table (doc, term, content, title, comment, tag, link, author, category, excerpt, taxonomy, customfield, type, taxonomy_detail, customfield_detail, mysqlcolumn)
		VALUES $values";
	$wpdb->query($query);
}

and change to this:

if (!empty($values)) {
	$values = implode(', ', $values);
	$query = "INSERT IGNORE INTO $relevanssi_table (doc, term, content, title, comment, tag, link, author, category, excerpt, taxonomy, customfield, type, taxonomy_detail, customfield_detail, mysqlcolumn)
		VALUES $values";
	var_dump($query);
	$wpdb->query($query);
	$wpdb->print_error();
}

This prints all the INSERT queries that put stuff in the database and also shows possible errors.

If this too shows lots of INSERT queries and no errors, yet indexing doesn’t work, things are seriously odd. Contact me for further assistance.

  • Bruce Hollingdrake

    The index is not getting built. I had continued using an older version – then tried the latest tonight.
    Here part of the result from Step 2 on the troubleshooting build index
    *************

    [cut the INSERTs]

  • http://www.mikkosaari.fi/ Mikko Saari

    Well, is it inserting those rows in the database? Does it print out any errors?

  • Deborah Gray

    For some reason the database is not being created, and the result is that I am getting the message that indexing is complete, but there are 0 documents.

    Can I manually create the table?

    • http://www.mikkosaari.fi/ Mikko Saari

      Yes, you can. Just find the CREATE TABLE commands in the relevanssi.php file to see the correct structure, or copy it from another installation of Relevanssi.

  • Shanx

    Thank you for this plugin, but I’ve now tried Relevanssi on three live blogs (all WP 3.3, but different plugins activated and different themes), plus my own localhost on a fresh install with just 5 posts and 5 pages.

    It does *not* work on any single blog.

    What am I missing. Is there more to it than activating the plugin and then “Build the Index”?

    • http://www.mikkosaari.fi/ Mikko Saari

      It shouldn’t need anything but activating and indexing.

  • Javier Bernad Fuentes

    Why I can not index automatically all the posts?

    • http://www.mikkosaari.fi/ Mikko Saari

      I don’t know. Relevanssi does indexing automatically when you edit posts, you just need to do the initial indexing manually.

  • http://micahgilman.com Micah

    This plugin sounds great. I haven’t got it to work yet though. I’ve built the index, and did the first suggestion on this page, which returned a bunch of IDs, but I can’t locate the second piece you mentioned above starting with ‘if (!empty($values))’ in the relevanssi.pnp file. After looking at my DB, the tables were built but they all seem to be empty. After turning on ‘$wpdb->show_errors();’ there appears to be a problem with the query when I try to build the index:
    WordPress database error: []
    SELECT *,parent.post_status as post_status FROM wp_posts parent, wp_posts post WHERE (parent.post_status=’publish’ OR parent.post_status=’private’) AND ( (post.post_status=’inherit’ AND post.post_parent=parent.ID) OR (parent.ID=post.ID) ) AND post.post_type!=’nav_menu_item’ AND post.post_type!=’revision’ AND post.post_type!=’attachment’ AND (post.post_type = ‘post’)

    • http://www.mikkosaari.fi/ Mikko Saari

      You could try usig that query directly in the database with phpMyAdmin or similar program, perhaps that would show the actual error, which WordPress doesn’t seem to show. The query seems correct to me.

  • http://typo3vagabond.com/ Michael Cannon

    Any ideas why private posts are NOT indexed or the index is deleted upon saving the post as private?

    • http://www.mikkosaari.fi/ Mikko Saari

      Have you tried the steps above? Take note of a post ID for a private post and add some conditional checks to see what happens to that particular post in the indexing process. Those are some of the most critical steps in the indexing process.

  • http://www.mjdigital.co.uk/blog/ MJ

    I had an issue trying to re-index a site after making some changes to the options – it would index the first 30 or so posts and then no more. I did a little debugging and managed to work out that in relevanssi_index_doc() it appears that for some posts the global $post object was pre-populated with the previous post’s information so the “if (!isset($post)) {” condition was returning false and the current post was not populated by the $indexpost variable. I did not dig much further as I found that adding:

    if(is_object($post)) { unset($post); }

    directly under the “global $wpdb, $relevanssi_table, $post;” at the top of the function relevanssi_index_doc worked for me when I rebuilt the index.

    After successful rebuild I commented out my code just in case there was a reason the global $post object needed to be available to the function.

    No idea if that helps anyone or not!

    MJ

    • http://www.mikkosaari.fi/ Mikko Saari

      Hhm, looks like I have to check that part of the plugin with a tight comb before the next update. The global $post is causing lots of troubles there. However, I’m fairly sure I put it there for a reason. The global $post object is needed for some plugins (shortcodes) to index properly, that much I know for sure.

  • http://www.columbianeurosurgery.org/ Bob Matsuoka

    Our index keeps disappearing. We have hundreds of articles that will be indexed, then suddenly it will revert to 1 document. Any suggestions?

  • http://www.visitfortwayne.com/blog alex Souders

    Thank you for the support, after doing the first code change my results came back looking like the following:

    string(2) “29″ string(2) “31″ string(2) “34″ string(2) “42″ string(2) “47″ string(2) “61″ string(2) “62″ string(2) “63″ string(2) “70″ string(2) “77″ string(2) “88″ string(3) “115″ string(3) “117″ string(3) “122″ string(3) “129″ string(3) “130″ string(3) “131″ string(3) “136″ string(3) “146″ string(3) “147″ string(3) “184″ string(3) “185″ string(3) “186″ string(3) “188″ string(3) “189″ string(3) “194″ string(3) “195″ string(3) “210″ string(3) “211″ string(3) “219″ string(3) “243″ string(3) “244″ string(3) “282″ string(3) “283″ string(3) “294″ string(3) “295″ string(3) “288″ string(3) “289″ string(3) “291″ string(3) “292″ string(3) “319″ string(3) “320″ string(3) “339″ string(3) “340″ string(3) “359″ string(3) “360″ string(3) “367″ string(3) “368″ string(3) “371″ string(3) “372″ string(3) “409″ string(3) “410″ string(3) “413″ string(3) “416″ string(3) “419″ string(3) “421″ string(3) “432″ string(3) “433″ string(3) “438″ string(3) “439″ string(3) “474″ string(3) “475″ string(3) “482″ string(3) “488″ string(3) “489″ string(3) “490″ string(3) “494″ string(3) “495″ string(3) “497″ string(3) “498″ string(3) “499″ string(3) “504″ string(3) “505″ string(3) “506″ string(3) “508″ string(3) “509″ string(3) “510″ string(3) “548″ string(3) “609″ string(3) “610″ string(3) “626″ string(3) “627″ string(3) “630″ string(3) “633″ string(3) “634″ string(3) “637″ string(3) “639″ string(3) “641″ string(3) “647″ string(3) “654″ string(3) “668″ string(3) “676″ string(3) “677″ string(3) “691″ string(3) “692″ string(3) “728″ string(3) “745″ string(3) “746″ string(3) “749″ string(3) “753″ string(3) “756″ string(3) “759″ string(3) “764″ string(3) “765″ string(3) “771″ string(3) “781″ string(3) “783″ string(3) “784″ string(3) “800″ string(3) “803″ string(3) “812″ string(3) “856″ string(3) “864″ string(3) “865″ string(3) “871″ string(3) “875″ string(3) “879″ string(3) “883″

    What does this tell me? Thank you!

    • http://www.mikkosaari.fi/ Mikko Saari

      This means the indexer is indexing the posts with these post ID’s. Looks good to me.

  • Nick

    I’ve got some problems when trying to index a site.

    De-activated and re-activated the plugin – nothing.
    Cleared the database (via phpmyadmin) and re-installed – nothing
    Followed the code changes here on this page to produce errors. No errors given, no list of post IDs number given and an “Indexing complete!” message given, but nothing is indexed after each of the changes.

    Any ideas on why this is failing?
    I’ve asked the hosting to see if any server settings could potentially be stopping it and they asked if the plugin uses HTTP loopback calls to index the site?

    • http://www.mikkosaari.fi/ Mikko Saari

      Relevanssi doesn’t use HTTP loopback.

      Check that you’ve set Relevanssi to actually index something – in some cases the post types to index may be empty.

      • Nick

        All the boxes are ticked that should be. Posts are selected, as well as pages. I cant see anything in the settings that would prevent posts or categories from been indexed. I’ve looked over them several times, checking and double checking everything.

        • http://www.mikkosaari.fi/ Mikko Saari

          For some reason Relevanssi isn’t seeing your posts. Very difficult to say why without actually seeing your site.

          I would suggest going backwards in the indexing function to see where it fails. Add var_dump()’s in the function, until you find out a place where you still have the indexed content.

          • Nick

            Not sure where I should be putting the var_dump()’s. I can give you access to the site if that would make it easier?

  • http://www.mikkosaari.fi/ Mikko Saari

    Nick, unfortunately at the moment I am busy enough that I can only support paying customers at that level.

    • Nick

      Ok.
      I’ve done a bit of reading through the indexing file. I’ve done this:

      var_dump($wpdb->get_results($q)); // generates array(0) { }

      Which is right before this bit:
      foreach ($content as $post) {
      $n += relevanssi_index_doc($post->ID, false, $custom_fields);
      // n calculates the number of insert queries
      }

      Does that mean its not finding any posts at all? The posts were generated via uploading a CSV file using “WP Ultimate CSV Importer”. They are all ‘uncategorized’, but other than that, all normal wordpress posts. All with titles, descriptions and a couple of custom fields. There are about 6,400 posts. But seen people with about that number of posts with it indexing. I’m not aware of any post titles/content having odd characters in (its all in English).

      The hosting company are unable to work it out as it involves an indepth knowledge of the script.

  • Nick

    Almost forgot to add that it doesn’t index any posts when adding a new post manually within WordPress.

  • http://www.mikkosaari.fi/ Mikko Saari

    That sounds like a pretty complex conflict. The number of posts or them being uncategorized is definitely not a problem.

    There’s something funny going on. I would suggest echoing out the SQL query that fetches the posts from the database and running it on phpMyAdmin to see if there’s some problem with it. What does the SQL query say?

    • Nick

      Not quite sure what you mean, but I did this in phpmyadmin:

      SELECT * FROM wp_posts WHERE post_type = ‘post’ LIMIT 0 , 100;

      And it returned 100 results no problem at all.

      • http://www.mikkosaari.fi/ Mikko Saari

        You did a var_dump() of the results of the query previously. Instead of that, do a var_dump() of the query itself and run that on the database.

        • Nick

          Ok..not sure if I’m doing this right. I’m not too experienced or clued up on actual SQL commands inside phpmyadmin.

          This is put in the ‘query’ tab.

          $result = mysql_fetch_array(mysql_query(“SELECT * FROM wp_posts WHERE post_type = ‘post’ LIMIT 0 , 100″));
          var_dump($result);

          Gives me an error “You have to choose at least one column to display”.
          Google results aren’t too helpful with that from what I’ve found.

          Putting the code into the ‘SQL’ tab, and it gives me an SQL syntax error.

          • http://www.mikkosaari.fi/ Mikko Saari

            I’m talking about this query:

            $q = “SELECT post.ID
            FROM $wpdb->posts parent, $wpdb->posts post WHERE
            (parent.post_status IN (‘publish’, ‘draft’, ‘private’, ‘pending’, ‘future’))
            AND (
            (post.post_status=’inherit’
            AND post.post_parent=parent.ID)
            OR
            (parent.ID=post.ID)
            )
            AND post.ID NOT IN (SELECT DISTINCT(doc) FROM $relevanssi_table) $restriction $limit”;

            So, before this “$content = $wpdb->get_results($q);”, add “var_dump($q);” and then run that query on phpMyAdmin.

  • http://www.postjobsnow.com Paul A.

    I had the same problem when I updated to version 3.0.5 and here is what I did to fix it:

    1. I ran the plugin “Clean Options” and removed anything listed that said Relevanssi. For me it was the following:
    relevanssi_hidesponsor
    relevanssi_index
    relevanssi_indexed
    relevanssi_show_matches_txt

    2. I then uninstalled the Relevanssi plugin ver 3.0.5

    3. Then I reinstalled it. Chose my settings again and then clicked “Save indexing options and build the index” at the bottom of the page.

    This worked for me. I tried to uninstall and reinstall before without using the “Clean Options” plugin and it didn’t work. When you are removing these options you will see that “relevanssi_indexed” is filled with the word “done”. I think Relevanssi would check that option before starting a new index and see “done and stop. I could be wrong with the reasoning of why this happens but one thing is for sure….it worked for me. Hopefully it will help someone else also.

    Good luck,
    Paul

  • Justin Bell

    Hi I am having trouble getting this plugin to work on some of my sites. I think the problem may be because I am using a MSSQL database and not MYSQL. I followed the steps above and got the results that you said I should get but the fields stay blank:

    Documents in the index:
    Terms in the index:

    Highest post ID indexed:

    I have another site set up running on MYSQL and it indexes perfectly. Could you verify that this is the problem and do you know if there is any work around?

    Thank you!

    • http://www.mikkosaari.fi/ Mikko Saari

      Justin, is it even possible to get WordPress to work with MS SQL server? Anyway, Relevanssi is not MS SQL compatible.

      • Justin Bell

        Yeah there is a way to modify the WP core code to allow for MS Sql queries to work properly, but they mention that it will not work with many plugins which is why I figured that was the issue. It seems like every search plugin out there relies on the user having My Sql.

        I am currently working with my server team to change over to running on my sql mainly b/c I really like your plugin.

        • http://www.mikkosaari.fi/ Mikko Saari

          It is quite hard to imagine a search plugin that wouldn’t need to interact directly with the database, yes.