public function SearchApiSolrTest::testRetrieveData in Search API Solr 8.2
Tests retrieve_data options.
File
- tests/
src/ Kernel/ SearchApiSolrTest.php, line 499
Class
- SearchApiSolrTest
- Tests index and search capabilities using the Solr search backend.
Namespace
Drupal\Tests\search_api_solr\KernelCode
public function testRetrieveData() {
$server = $this
->getIndex()
->getServerInstance();
$config = $server
->getBackendConfig();
$backend = $server
->getBackend();
$this
->insertExampleContent();
$this
->indexItems($this->indexId);
// Retrieve just required fields.
$query = $this
->buildSearch('foobar');
$results = $query
->execute();
$this
->assertEquals(1, $results
->getResultCount(), 'Search for »foobar« returned correct number of results.');
/** @var \Drupal\search_api\Item\ItemInterface $result */
foreach ($results as $result) {
/** @var \Solarium\QueryType\Select\Result\Document $solr_document */
$solr_document = $result
->getExtraData('search_api_solr_document', NULL);
$fields = $solr_document
->getFields();
$this
->assertEquals('entity:entity_test_mulrev_changed/3:en', $fields['ss_search_api_id']);
$this
->assertEquals('en', $fields['ss_search_api_language']);
$this
->assertArrayHasKey('score', $fields);
$this
->assertArrayNotHasKey('tm_body', $fields);
$this
->assertArrayNotHasKey('id', $fields);
$this
->assertArrayNotHasKey('its_id', $fields);
$this
->assertArrayNotHasKey('twm_suggest', $fields);
}
// Retrieve all fields.
$config['retrieve_data'] = TRUE;
$server
->setBackendConfig($config);
$server
->save();
$query = $this
->buildSearch('foobar');
$results = $query
->execute();
$this
->assertEquals(1, $results
->getResultCount(), 'Search for »foobar« returned correct number of results.');
/** @var \Drupal\search_api\Item\ItemInterface $result */
foreach ($results as $result) {
/** @var \Solarium\QueryType\Select\Result\Document $solr_document */
$solr_document = $result
->getExtraData('search_api_solr_document', NULL);
$fields = $solr_document
->getFields();
$this
->assertEquals('entity:entity_test_mulrev_changed/3:en', $fields['ss_search_api_id']);
$this
->assertEquals('en', $fields['ss_search_api_language']);
$this
->assertArrayHasKey('score', $fields);
$this
->assertArrayHasKey('tm_body', $fields);
$this
->assertContains('search_index-entity:entity_test_mulrev_changed/3:en', $fields['id']);
$this
->assertEquals('3', $fields['its_id']);
$this
->assertArrayHasKey('twm_suggest', $fields);
}
// Retrieve list of fields in addition to required fields.
$query = $this
->buildSearch('foobar');
$query
->setOption('search_api_retrieved_field_values', [
'body' => 'body',
]);
$results = $query
->execute();
$this
->assertEquals(1, $results
->getResultCount(), 'Search for »foobar« returned correct number of results.');
/** @var \Drupal\search_api\Item\ItemInterface $result */
foreach ($results as $result) {
/** @var \Solarium\QueryType\Select\Result\Document $solr_document */
$solr_document = $result
->getExtraData('search_api_solr_document', NULL);
$fields = $solr_document
->getFields();
$this
->assertEquals('entity:entity_test_mulrev_changed/3:en', $fields['ss_search_api_id']);
$this
->assertEquals('en', $fields['ss_search_api_language']);
$this
->assertArrayHasKey('score', $fields);
$this
->assertArrayHasKey('tm_body', $fields);
$this
->assertArrayNotHasKey('id', $fields);
$this
->assertArrayNotHasKey('its_id', $fields);
$this
->assertArrayNotHasKey('twm_suggest', $fields);
}
$this
->assertEquals([
0 => 'name',
1 => 'body',
2 => 'body_unstemmed',
// body_suggest should be removed by getQueryFulltextFields().
// 3 => 'body_suggest',
4 => 'category_ngram',
], $this
->invokeMethod($backend, 'getQueryFulltextFields', [
$query,
]));
}