• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How can I update Elastic Search index

AndyB

Well-known member
#1
In my add-on Change Author, I have a requirement to update the Elastic Search index. The add-on changes the author of a post. The only step I'm missing at this point is to update Elastic Search.

I would like to use this code with the appropriate code added:

PHP:
// check if ElasticSearch is enabled
if (XenForo_Application::get('options')->enableElasticsearch)
{ 
    // add code here to update ElasticSearch index
}
I would need Elastic Search to be updated as if I were to enter a new post or edit an existing one.

Thank you.
 

AndyB

Well-known member
#2
Got it.

PHP:
// check if ElasticSearch is enabled
if (XenForo_Application::get('options')->enableElasticsearch)
{
    // run query
    $data = $db->fetchRow("
    SELECT xf_thread.node_id, 
    xf_thread.thread_id, 
    xf_thread.title, 
    xf_post.message, 
    xf_post.post_date, 
    xf_post.user_id
    FROM xf_post
    INNER JOIN xf_thread ON xf_thread.thread_id = xf_post.thread_id 
    WHERE xf_post.post_id = ?
    ", $postId);				
                
    // get ElasticSearch index name
    $indexName = XenES_Api::getInstance()->getIndex();		
    
    // define variables
    $contentType = 'post';
    $contentId = $postId;
    
    // define record data
    $record = array(
        'node' => $data['node_id'],
        'thread' => $data['thread_id'],
        'title' => $data['title'], 
        'message' => $data['message'],
        'date' => $data['post_date'],
        'user' => $data['user_id'],
        'discussion_id' => $postId
    );

    // update Elastic Search index
    XenES_Api::index($indexName, $contentType, $contentId, $record);
}