forked from solariumphp/solarium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.7.3.2-extract-query-pdo-lob.php
48 lines (37 loc) · 1.7 KB
/
2.7.3.2-extract-query-pdo-lob.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
require_once(__DIR__.'/init.php');
htmlHeader();
echo '<h2>Note: The <code>extraction</code> <a href="https://solr.apache.org/guide/solr/latest/configuration-guide/solr-modules.html" target="_blank">Solr Module</a> needs to be enabled to run this example!</h2>';
echo "<h2>Note: This example doesn't work in PHP < 8.1.0!</h2>";
echo "<h2>Note: This example requires the PDO_SQLITE PDO driver (enabled by default in PHP)</h2>";
// create a client instance
$client = new Solarium\Client($adapter, $eventDispatcher, $config);
// get an extract query instance and add settings
$query = $client->createExtract();
$query->addFieldMapping('content', 'text');
$query->setUprefix('attr_');
$query->setCommit(true);
$query->setOmitHeader(false);
// create a database & store content as an example
$db = new PDO('sqlite::memory:');
$db->exec("CREATE TABLE test (id INT, content TEXT)");
$insert = $db->prepare("INSERT INTO test (id, content) VALUES (:id, :content)");
$insert->execute(['id' => 1, 'content' => file_get_contents(__DIR__.'/index.html')]);
// get content from the database and map it as a stream
$select = $db->prepare("SELECT content FROM test WHERE id = :id");
$select->execute(['id' => 1]);
$select->bindColumn(1, $content, PDO::PARAM_LOB);
$select->fetch(PDO::FETCH_BOUND);
// add content as a stream resource
$query->setFile($content);
// add document
$doc = $query->createDocument();
$doc->id = 'extract-test';
$doc->some = 'more fields';
$query->setDocument($doc);
// this executes the query and returns the result
$result = $client->extract($query);
echo '<b>Extract query executed</b><br/>';
echo 'Query status: ' . $result->getStatus(). '<br/>';
echo 'Query time: ' . $result->getQueryTime();
htmlFooter();