- Issue created by @kevinquillen
- πΊπΈUnited States kevinquillen
This is the first rough pass to getting this unblocked. Rough and by no means pretty. I modified the code to do the following:
- Per Drupal field in the index, call the API and check if it exists in Coveo. Unfortunately, the field search API is 'loose' and not strict, so we have to loop every result returned
- If the field does not exist and is not a system field, proceed normally and add it to the 'to be created' array
- If the field does exist, add it to the 'to be updated' array
/** * {@inheritdoc} */ public function updateIndex(IndexInterface $index) { /** @var \Drupal\search_api\Item\FieldInterface[] $fields*/ $fields = $index->getFields(TRUE); $fields_to_add = []; $fields_to_update = []; $this->connect(); // todo: the prefix of the fields needs to be specified in a config file. foreach ($fields as $field_name => $field) { $exists = $this->coveoClient->listFields($field_name, 'USER'); foreach ($exists as $key => $item) { if ($item['name'] !== $field_name) { continue; } $coveo_field = $item; } // skip fields title(a coveo system field) and render_item, the html content is not a field in coveo. // todo: create helper to check if is system/internal coveo field. if ($field_name === self::CONTENT_FIELD_ID || $field_name === self::TITLE) { continue; } if (!empty($coveo_field) && $coveo_field['system'] === TRUE) { continue; } $description = $field->getLabel(); $coveo_type = $this->mapCoveoType($field->getType()); // @see https://docs.coveo.com/en/8/cloud-v2-api-reference/field-api#operation/createFieldUsingPOST_2 $field_info = [ 'name' => $field_name, 'description' => $description, 'type' => $coveo_type, ]; if (!empty($coveo_field)) { $fields_to_update[$field_name] = $field_info; } else { $fields_to_add[$field_name] = $field_info; } } if ($fields_to_add) { $fields_created = $this->coveoClient->createFields(array_values($fields_to_add)); // Check fields are created in coveo. if ($fields_created) { $t_args = ['%field_ids' => implode(", ", array_keys($fields_to_add))]; \Drupal::messenger()->addStatus($this->t('New fields added in coveo: %field_ids.', $t_args)); // watchdog log the message. $this->logMessage('New fields added in coveo: %field_ids.', $t_args); } } if ($fields_to_update) { $fields_updated = $this->coveoClient->updateFields(array_values($fields_to_update)); // Check fields are created in coveo. if ($fields_updated) { $t_args = ['%field_ids' => implode(", ", array_keys($fields_to_update))]; \Drupal::messenger()->addStatus($this->t('Fields updated in coveo: %field_ids.', $t_args)); // watchdog log the message. $this->logMessage('Fields updated in coveo: %field_ids.', $t_args); } } }
This caused a brand new field to appear in Coveo (without using the Coveo admin), which is what we want:
When the other issue is fixed about using the correct API URLs according to the connection settings, the "Configure" link will jump right to the Coveo admin to change flags on the field (facet, sortable, etc).
- πΊπΈUnited States kevinquillen
Some small tweaks.
// todo: the prefix of the fields needs to be specified in a config file. foreach ($fields as $field_name => $field) { $coveo_field = NULL; $exists = $this->coveoClient->listFields($field_name, 'USER'); foreach ($exists as $item) { if ($item['name'] !== $field_name) { continue; } $coveo_field = $item; } // skip fields title(a coveo system field) and render_item, the html content is not a field in coveo. // todo: create helper to check if is system/internal coveo field. if ($field_name === self::TITLE) { continue; } if (!empty($coveo_field) && $coveo_field['system'] === TRUE) { continue; } $description = $field->getLabel(); $coveo_type = $this->mapCoveoType($field->getType()); $field_info = [ 'name' => $field_name, 'description' => $description, 'type' => $coveo_type, ]; if (!empty($coveo_field)) { $fields_to_update[$field_name] = $field_info; } else { $fields_to_add[$field_name] = $field_info; } }
I have been able to add any number of fields from Drupal and see them appear in the Coveo source, even 'rendered item'. Not sure why that was blocked, but it works just fine. I can index it and use it for ranking along with a number of others.
- First commit to issue fork.
- Assigned to fathershawn
- Status changed to Needs review
12 months ago 3:10pm 14 December 2023 - πΊπΈUnited States fathershawn New York
I've improved this for type safety and readability. Passed manual testing.
-
FatherShawn β
committed 826802b2 on 1.0.x
Issue #3388680 by FatherShawn, kevinquillen, furamag: Creating or...
-
FatherShawn β
committed 826802b2 on 1.0.x
- Issue was unassigned.
- Status changed to Fixed
11 months ago 2:53pm 18 December 2023 Automatically closed - issue fixed for 2 weeks with no activity.