You are here

protected function FuzzySearchService::createFieldTable in Fuzzy Search 7

Helper method for creating the table for a field.

2 calls to FuzzySearchService::createFieldTable()
FuzzySearchService::addIndex in includes/service.inc
Implements SearchApiServiceInterface::__construct().
FuzzySearchService::fieldsUpdated in includes/service.inc
Implements SearchApiServiceInterface::__construct().

File

includes/service.inc, line 160

Class

FuzzySearchService
Search service class using the database for storing index information.

Code

protected function createFieldTable(SearchApiIndex $index, $field, $name) {
  $table = array(
    'name' => $name,
    'module' => 'fuzzysearch',
    'fields' => array(
      'item_id' => array(
        'description' => 'The primary identifier of the entity.',
        'not null' => TRUE,
      ),
    ),
  );

  // The type of the item_id field depends on the ID field's type.
  $id_field = $index
    ->datasource()
    ->getIdFieldInfo();
  $table['fields']['item_id'] += $this
    ->sqlType($id_field['type'] == 'text' ? 'string' : $id_field['type']);
  if (isset($table['fields']['item_id']['length'])) {

    // A length of 255 is overkill for IDs. 50 should be more than enough.
    $table['fields']['item_id']['length'] = 50;
  }
  $type = search_api_extract_inner_type($field['type']);
  if ($type == 'text') {
    $table['fields']['id'] = array(
      'description' => 'The ngram id.',
      'type' => 'serial',
      'unsigned' => TRUE,
      'not null' => TRUE,
    );
    $table['fields']['word_id'] = array(
      'description' => 'The word id.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
    );
    $table['fields']['ngram'] = array(
      'description' => 'The ngram.',
      'type' => 'varchar',
      'length' => 50,
      'not null' => TRUE,
    );
    $table['fields']['completeness'] = array(
      'description' => 'Completeness',
      'type' => 'float',
      'not null' => TRUE,
    );
    $table['fields']['score'] = array(
      'description' => 'The score associated with this ngram.',
      'type' => 'float',
      'not null' => TRUE,
    );
    $table['primary key'] = array(
      'id',
    );
    $table['indexes']['ngram'] = array(
      array(
        'ngram',
        10,
      ),
    );
    $table['indexes']['completeness'] = array(
      'completeness',
    );
  }
  else {
    $table['fields']['value'] = $this
      ->sqlType($type);
    $table['fields']['value'] += array(
      'description' => "The field's value for this item.",
    );
    if ($type != $field['type']) {

      // This is a list type.
      $table['fields']['value']['not null'] = TRUE;
      $table['primary key'] = array(
        'item_id',
        'value',
      );
    }
    else {
      $table['primary key'] = array(
        'item_id',
      );
    }
    $table['indexes']['value'] = $table['fields']['value'] == 'varchar' ? array(
      array(
        'value',
        10,
      ),
    ) : array(
      'value',
    );
  }
  $set = $this
    ->setDb();
  db_create_table($name, $table);
  if ($set) {
    $this
      ->resetDb();
  }
}