XF 2.2 Enhanced Search: index issue with "new" content type

Scandal

Active member
Well, xF Nodes entity doesn't have a search handler.
So I added my own for a custom function:
PHP:
class Node extends AbstractData
{
    public function getEntityWith($forView = false)
    {
        $get = [];
       
        if ($forView)
        {
            $visitor = \XF::visitor();
            $get[] = 'Permissions|' . $visitor->permission_combination_id;
        }
       
        return $get;
    }

    public function getIndexData(Entity $entity)
    {
        if ($entity->node_type_id != 'Forum' && $entity->node_type_id !== null)
        {
            return null;
        }

        $index = IndexRecord::create('node', $entity->node_id, [
            'title' => strip_tags($entity->title_),
            'message' => strip_tags($entity->description_),
            'date' => \XF::$time,
            'user_id' => 0,
            'discussion_id' => 0,           
            'metadata' => $this->getMetaData($entity)
        ]);

        return $index;
    }

    protected function getMetaData(\XF\Entity\Node $entity)
    {
        $metadata = [
            'node' => $entity->node_id,
            'node_type_id' => $entity->node_type_id
        ];

        return $metadata;
    }

    public function setupMetadataStructure(MetadataStructure $structure)
    {
        $structure->addField('node', MetadataStructure::INT);
        $structure->addField('node_type_id', MetadataStructure::KEYWORD);
    }

    public function getResultDate(Entity $entity)
    {
        return \XF::$time;
    }

    public function getTemplateData(Entity $entity, array $options = [])
    {
        return [
            'node' => $entity,
            'options' => $options
        ];
    }
}
I have also add this on Node structure:
PHP:
$structure->behaviors['XF:Indexable'] = ['checkForUpdates' => ['title', 'description']];

But I have an unstable issue with Enhanced Search:
1. For example if I create a new Node (Forum) and then try to use search to detect it, it is not be shown.
2. If I run 2 - 3 times the Rebuild Search Index on admin for the - new - content type Nodes, then it appears.
3. I do the search with a similar way the Thread Suggestion works:

PHP:
// $title is the search query
$node = \XF::em()->create('XF:Node');
$node->title = $title;
$suggestedNodeIds = $this->getSimilarNodeIds($node, 10);
// for the new added forum, no results! If I run rebuild search index 2 - 3 times, then it appears!
PHP:
    public function getSimilarNodeIds(
        \XF\Entity\Node $node,
        $maxResults = null,
        bool $applyVisitorPermissions = true
    ): array
    {
        /** @var \XFES\XF\Search\Search $searcher */
        $searcher = $this->app()->search();

        $results = $searcher->moreLikeThis(
            $this->getSimilarThreadsMltQuery($node),
            $maxResults,
            $applyVisitorPermissions
        );
       
        $nodeIds = [];
        foreach ($results AS $result)
        {
           
            $nodeIds[] = $result[1];
        }

        return $nodeIds;
    }
   
    public function getSimilarThreadsMltQuery(
        \XF\Entity\Node $node
    ): \XFES\Search\Query\MoreLikeThisQuery
    {
        /** @var \XFES\XF\Search\Search $searcher */
        $searcher = $this->app()->search();

        $query = $searcher->getMoreLikeThisQuery();
        $query
            ->like($node)
            ->inType('node');

        return $query;
    }

Why this unstability? What I miss?
 
Top