Answers.Body: <p>Craft will only extract the search keywords for an element during the element saving process.</p>
  
  <p>If you have existing custom elements already saved, then you go in and add the <code>defineSearchableAttributes()</code> to your element type, you'll need to re-save the existing elements in order for their search keywords to be indexed.</p>
  
  <p>Or now that I think about it... the easier way would probably be to just run the "Update Search Indexes" tool in the Control Panel under Settings->Tools. It will do the same thing for you.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-06-14T18:09:17.230Z
Answers.LastActivityDate: 2017-06-14T18:09:17.230Z
Answers.UserDisplayName: Brad Bell
Body: <p>I have a custom element type with 2 custom fields, 'name' and 'email'.
  I can't seem to get the index search working for this element type.
  I defined defineSearchableAttributes and returned an array with these 2 attributes.
  Searching on the title field works though.</p>
  
  <pre><code>&lt;?php
  namespace Craft;
  
  class Newsletter_SubscriptionElementType extends BaseElementType {
  
  public function getName() {
      return Craft::t('Subscriptions');
  }
  
  public function hasContent() {
      return true;
  }
  
  public function hasTitles() {
      return true;
  }
  
  public function populateElementModel($row) {
      return Newsletter_SubscriptionModel::populateModel($row);
  }
  
  public function defineCriteriaAttributes() {
      return array(
          'name'      =&gt; AttributeType::String,
          'email'     =&gt; AttributeType::Email
      );
  }
  
  public function defineSearchableAttributes() {
      return array('name','email');
  }
  
  public function defineTableAttributes() {
      $attributes = array(
          'title'         =&gt; Craft::t('Title'),
          'name'          =&gt; Craft::t('Name'),
          'email'         =&gt; Craft::t('Email'),
          'dateCreated'   =&gt; Craft::t('Date Created')
      );
  
      return $attributes;
  }
  
  public function getTableAttributeHtml(BaseElementModel $element, $attribute) {
      return parent::getTableAttributeHtml($element, $attribute);
  }
  
  public function getSources($context = null) {
      $sources = array(
          '*' =&gt; array(
              'label'    =&gt; Craft::t('All subscriptions'),
          )
      );
  
      return $sources;
  }
  
  public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria) {
      $query 
          -&gt;addSelect('newsletter_subscriptions.name, newsletter_subscriptions.email')
          -&gt;join('newsletter_subscriptions newsletter_subscriptions', 'newsletter_subscriptions.id = elements.id');
  }
  }
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-12T23:30:56.213Z
Comments.Text: That method only comes into play when you're saving your custom element... is that how you're testing?
Comments.UserDisplayName: Brad Bell
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-13T07:52:27.150Z
Comments.Text: You mean that a new entry has to be posted after I defined that method before craft can search that entry?
Comments.UserDisplayName: Thomas Vansteelandt
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-06-13T21:08:31.913Z
Comments.Text: Correct... Craft will extract the search keywords for an element after it is saved.  If you've added that method after you have existing elements, they'll need to be resaved.
Comments.UserDisplayName: Brad Bell
Tags: <plugin-development><search><elementtype>
Title: Custom element type defineSearchableAttributes
ThreadText: # Custom element type defineSearchableAttributes
  
  (Asked by: username_0 on 2017-06-12)
  
  I have a custom element type with 2 custom fields, 'name' and 'email'.
  I can't seem to get the index search working for this element type.
  I defined defineSearchableAttributes and returned an array with these 2 attributes.
  Searching on the title field works though.
  
  ```
  <?php
  namespace Craft;
  
  class Newsletter_SubscriptionElementType extends BaseElementType {
  
  public function getName() {
      return Craft::t('Subscriptions');
  }
  
  public function hasContent() {
      return true;
  }
  
  public function hasTitles() {
      return true;
  }
  
  public function populateElementModel($row) {
      return Newsletter_SubscriptionModel::populateModel($row);
  }
  
  public function defineCriteriaAttributes() {
      return array(
          'name'      => AttributeType::String,
          'email'     => AttributeType::Email
      );
  }
  
  public function defineSearchableAttributes() {
      return array('name','email');
  }
  
  public function defineTableAttributes() {
      $attributes = array(
          'title'         => Craft::t('Title'),
          'name'          => Craft::t('Name'),
          'email'         => Craft::t('Email'),
          'dateCreated'   => Craft::t('Date Created')
      );
  
      return $attributes;
  }
  
  public function getTableAttributeHtml(BaseElementModel $element, $attribute) {
      return parent::getTableAttributeHtml($element, $attribute);
  }
  
  public function getSources($context = null) {
      $sources = array(
          '*' => array(
              'label'    => Craft::t('All subscriptions'),
          )
      );
  
      return $sources;
  }
  
  public function modifyElementsQuery(DbCommand $query, ElementCriteriaModel $criteria) {
      $query 
          ->addSelect('newsletter_subscriptions.name, newsletter_subscriptions.email')
          ->join('newsletter_subscriptions newsletter_subscriptions', 'newsletter_subscriptions.id = elements.id');
  }
  }
  
  ```
  
  **Tags:** plugin-development, search, elementtype
  
  > **Comments:**
  > **username_1**: That method only comes into play when you're saving your custom element... is that how you're testing?  
  > **username_0**: You mean that a new entry has to be posted after I defined that method before craft can search that entry?  
  > **username_1**: Correct... Craft will extract the search keywords for an element after it is saved.  If you've added that method after you have existing elements, they'll need to be resaved.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-06-14. Score: 2
  
  Craft will only extract the search keywords for an element during the element saving process.
  
  If you have existing custom elements already saved, then you go in and add the `defineSearchableAttributes()` to your element type, you'll need to re-save the existing elements in order for their search keywords to be indexed.
  
  Or now that I think about it... the easier way would probably be to just run the "Update Search Indexes" tool in the Control Panel under Settings->Tools. It will do the same thing for you.
  
  
  ---
  
  