php-doc-en/reference/solr/examples.xml

380 lines
8.5 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="solr.examples" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
&reftitle.examples;
<para>
Examples of how to use the Apache Solr extension in PHP
</para>
<example>
<title>Adding a document to the index</title>
<programlisting role="php">
<![CDATA[
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$doc = new SolrInputDocument();
$doc->addField('id', 334455);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');
$updateResponse = $client->addDocument($doc);
print_r($updateResponse->getResponse());
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
SolrObject Object
(
[responseHeader] => SolrObject Object
(
[status] => 0
[QTime] => 446
)
)
]]>
</screen>
</example>
<example>
<title>Merging one document into another document</title>
<programlisting role="php">
<![CDATA[
<?php
include "bootstrap.php";
$doc = new SolrDocument();
$second_doc = new SolrDocument();
$doc->addField('id', 1123);
$doc->features = "PHP Client Side";
$doc->features = "Fast development cycles";
$doc['cat'] = 'Software';
$doc['cat'] = 'Custom Search';
$doc->cat = 'Information Technology';
$second_doc->addField('cat', 'Lucene Search');
$second_doc->merge($doc, true);
print_r($second_doc->toArray());
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
Array
(
[document_boost] => 0
[field_count] => 3
[fields] => Array
(
[0] => SolrDocumentField Object
(
[name] => cat
[boost] => 0
[values] => Array
(
[0] => Software
[1] => Custom Search
[2] => Information Technology
)
)
[1] => SolrDocumentField Object
(
[name] => id
[boost] => 0
[values] => Array
(
[0] => 1123
)
)
[2] => SolrDocumentField Object
(
[name] => features
[boost] => 0
[values] => Array
(
[0] => PHP Client Side
[1] => Fast development cycles
)
)
)
)
]]>
</screen>
</example>
<example>
<title>Searching for documents - SolrObject responses</title>
<programlisting role="php">
<![CDATA[
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('lucene');
$query->setStart(0);
$query->setRows(50);
$query->addField('cat')->addField('features')->addField('id')->addField('timestamp');
$query_response = $client->query($query);
$response = $query_response->getResponse();
print_r($response);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
SolrObject Object
(
[responseHeader] => SolrObject Object
(
[status] => 0
[QTime] => 1
[params] => SolrObject Object
(
[wt] => xml
[rows] => 50
[start] => 0
[indent] => on
[q] => lucene
[fl] => cat,features,id,timestamp
[version] => 2.2
)
)
[response] => SolrObject Object
(
[numFound] => 3
[start] => 0
[docs] => Array
(
[0] => SolrObject Object
(
[cat] => Array
(
[0] => Software
[1] => Lucene
)
[id] => 334456
)
[1] => SolrObject Object
(
[cat] => Array
(
[0] => Software
[1] => Lucene
)
[id] => 334455
)
[2] => SolrObject Object
(
[cat] => Array
(
[0] => software
[1] => search
)
[features] => Array
(
[0] => Advanced Full-Text Search Capabilities using Lucene
[1] => Optimized for High Volume Web Traffic
[2] => Standards Based Open Interfaces - XML and HTTP
[3] => Comprehensive HTML Administration Interfaces
[4] => Scalability - Efficient Replication to other Solr Search Servers
[5] => Flexible and Adaptable with XML configuration and Schema
[6] => Good unicode support: héllo (hello with an accent over the e)
)
[id] => SOLR1000
[timestamp] => 2009-09-04T20:38:55.906
)
)
)
)
]]>
</screen>
</example>
<example>
<title>Searching for documents - SolrDocument responses</title>
<programlisting role="php">
<![CDATA[
<?php
include "bootstrap.php";
$options = array
(
'hostname' => SOLR_SERVER_HOSTNAME,
'login' => SOLR_SERVER_USERNAME,
'password' => SOLR_SERVER_PASSWORD,
'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('lucene');
$query->setStart(0);
$query->setRows(50);
$query->addField('cat')->addField('features')->addField('id')->addField('timestamp');
$query_response = $client->query($query);
$query_response->setParseMode(SolrQueryResponse::PARSE_SOLR_DOC);
$response = $query_response->getResponse();
print_r($response);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
SolrObject Object
(
[responseHeader] => SolrObject Object
(
[status] => 0
[QTime] => 1
[params] => SolrObject Object
(
[wt] => xml
[rows] => 50
[start] => 0
[indent] => on
[q] => lucene
[fl] => cat,features,id,timestamp
[version] => 2.2
)
)
[response] => SolrObject Object
(
[numFound] => 3
[start] => 0
[docs] => Array
(
[0] => SolrDocument Object
(
[_hashtable_index:SolrDocument:private] => 19740
)
[1] => SolrDocument Object
(
[_hashtable_index:SolrDocument:private] => 25485
)
[2] => SolrDocument Object
(
[_hashtable_index:SolrDocument:private] => 25052
)
)
)
)
]]>
</screen>
</example>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->