XF 2.1 Wondering if it's possible to add search to my add-on

Matt C.

Well-known member
I have Dev Tracker add-on that gets tweets from game developers for a game via a JSON url.

Here is an example of the text I get from the JSON that I want to be searchable:

Code:
<blockquote><div class="bb_quoteauthor">Originally posted by<b><a href="https://reddit.com/r/AnthemTheGame/comments/a2udf7/the_comunication_from_the_developers_so_far_has/eb1eryi">iGameYT</a></b></div><div class="md"><p><a href="https://reddit.com/u/BenIrvo">u/BenIrvo</a>, <a href="https://reddit.com/u/UNTdrew">u/UNTdrew</a> and the rest of the Anthem team have done an amazing job with transparency and communication in the community. Im excited to see what the future holds for this game and how the world comes to life when we finally drop in. </p></div></blockquote><div class="md"><p>I personally can’t wait for you all to drop in. I look forward to forming some bonds and taking on some Scars together! </p></div>

As you can see, it contains HTML, which would cause problems. Does anyone know how to work around that? Thank you.
 
Last edited:
I have not yet looked under the hood of searching and indexing in XF2, but I would assume that you apply a strip_tags funcion call before inserting your data to the search index. :)
 
Okay I've done that. I've added a "content_stripped" column in my table, where the the content is stripped of html. When I search, it returns no content found. I've rebuilt the search index.

Maybe someone can look this over?
Here is my Search\Data\Entry:
PHP:
<?php

namespace AH\DevTracker\Search\Data;

use XF\Mvc\Entity\Entity;
use XF\Search\Data\AbstractData;
use XF\Search\IndexRecord;
use XF\Search\MetadataStructure;

class Entry extends AbstractData
{
    public function getIndexData(Entity $entity)
    {
        /** @var \AH\DevTracker\Entity\Entry $entity */
        $index = IndexRecord::create('ah_dev_tracker_entry', $entity->entry_id, [
            'content_stripped' => $entity->content_stripped,
            'date' => $entity->timestamp,
            'metadata' => $this->getMetaData($entity)
        ]);

        return $index;
    }

    protected function getMetaData(\AH\DevTracker\Entity\Entry $entity)
    {
        return [
            'entry' => $entity->entry_id
        ];
    }

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

    public function getResultDate(Entity $entity)
    {
        return $entity->timestamp;
    }

    public function getTemplateData(Entity $entity, array $options = [])
    {
        return [
            'entry' => $entity,
        ];
    }

    public function getSearchableContentTypes()
    {
        return ['xf_ah_dev_tracker_entry'];
    }

    public function getSearchFormTab()
    {
        return [
            'title' => \XF::phrase('ah_dev_tracker_search_developer_entries'),
            'order' => 700
        ];
    }

    public function getSectionContext()
    {
        return 'ah_dev_tracker_entries';
    }

    public function getGroupByType()
    {
        return 'ah_dev_tracker_entries';
    }
}
 
Okay so I've fixed one problem. In the index, I had "content_stripped" instead of "message". I fixed that.

Code:
public function getIndexData(Entity $entity)
{
    /** @var \AH\DevTracker\Entity\Entry $entity */
    $index = IndexRecord::create('ah_dev_tracker_entry', $entity->entry_id, [
        'message' => $entity->content_stripped,
        'date' => $entity->timestamp,
        'metadata' => $this->getMetaData($entity)
    ]);

    return $index;
}

So when rebuilding the search index, I finally get content in the "xf_search_index" table.

190096

But when I search, it returns no content found.

Here is my search form template:
HTML:
<xf:title>{{ phrase('ah_dev_tracker_search_developer_entries') }}</xf:title>

<xf:set var="$controlId">{{ unique_id() }}</xf:set>
<xf:formrow rowtype="input" controlid="{$controlId}"
    label="{{ phrase('keywords') }}">

    <ul class="inputList">
        <li><xf:textbox type="search" name="keywords" value="{$input.keywords}" autofocus="autofocus" id="{$controlId}" /></li>
    </ul>
</xf:formrow>

<xf:macro template="search_form_macros" name="date" arg-input="{$input}" />

If anyone has any suggestions, it would be greatly appreciated.
 
Okay so I've fixed one problem. In the index, I had "content_stripped" instead of "message". I fixed that.

Code:
public function getIndexData(Entity $entity)
{
    /** @var \AH\DevTracker\Entity\Entry $entity */
    $index = IndexRecord::create('ah_dev_tracker_entry', $entity->entry_id, [
        'message' => $entity->content_stripped,
        'date' => $entity->timestamp,
        'metadata' => $this->getMetaData($entity)
    ]);

    return $index;
}

So when rebuilding the search index, I finally get content in the "xf_search_index" table.

View attachment 190096

But when I search, it returns no content found.

Here is my search form template:
HTML:
<xf:title>{{ phrase('ah_dev_tracker_search_developer_entries') }}</xf:title>

<xf:set var="$controlId">{{ unique_id() }}</xf:set>
<xf:formrow rowtype="input" controlid="{$controlId}"
    label="{{ phrase('keywords') }}">

    <ul class="inputList">
        <li><xf:textbox type="search" name="keywords" value="{$input.keywords}" autofocus="autofocus" id="{$controlId}" /></li>
    </ul>
</xf:formrow>

<xf:macro template="search_form_macros" name="date" arg-input="{$input}" />

If anyone has any suggestions, it would be greatly appreciated.

@Matt C. this has been a lot of help. Were you able to figure out why you were getting "no results"?
 
Back
Top Bottom