Skip to content

Commit

Permalink
Merge branches 'develop', 'feature/analysis' and 'master' of github.c…
Browse files Browse the repository at this point in the history
…om:basdenooijer/solarium into develop
  • Loading branch information
basdenooijer committed Sep 26, 2011
3 parents cfac1ab + 94ca17a + 6d1f5d8 commit 18a6a87
Show file tree
Hide file tree
Showing 29 changed files with 2,669 additions and 0 deletions.
92 changes: 92 additions & 0 deletions examples/2.4.1-analysis-document.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

require('init.php');
htmlHeader();

// create a client instance
$client = new Solarium_Client($config);

// get an analysis document query
$query = $client->createAnalysisDocument();

$query->setShowMatch(true);
$query->setQuery('ipod');

$doc = new Solarium_Document_ReadWrite(
array(
'id' => 'MA147LL',
'name' => 'Apple 60 GB iPod with Video Playback Black',
'manu' => 'Apple Computer Inc.',
'cat' => 'electronics',
'cat' => 'music',
'features' => 'iTunes, Podcasts, Audiobooks',
'features' => 'Stores up to 15,000 songs, 25,000 photos, or 150 hours of video',
'features' => '2.5-inch, 320x240 color TFT LCD display with LED backlight',
'features' => 'Up to 20 hours of battery life',
'features' => 'Plays AAC, MP3, WAV, AIFF, Audible, Apple Lossless, H.264 video',
'features' => 'Notes, Calendar, Phone book, Hold button, Date display, Photo wallet, Built-in games, JPEG photo playback, Upgradeable firmware, USB 2.0 compatibility, Playback speed control, Rechargeable capability, Battery level indication',
'includes' => 'earbud headphones, USB cable',
'weight' => 5.5,
'price' => 399.00,
'popularity' => 10,
'inStock' => true,
)
);

$query->addDocument($doc);

// this executes the query and returns the result
$result = $client->analyze($query);

// show the results
foreach ($result as $document) {

echo '<hr><h2>Document: ' . $document->getName() . '</h2>';

foreach ($document as $field) {

echo '<h3>Field: ' . $field->getName() . '</h3>';

$indexAnalysis = $field->getIndexAnalysis();
if (!empty($indexAnalysis)) {
echo '<h4>Index Analysis</h4>';
foreach ($indexAnalysis as $classes) {

echo '<h5>'.$classes->getName().'</h5>';

foreach($classes as $result) {
echo 'Text: ' . $result->getText() . '<br/>';
echo 'Raw text: ' . $result->getRawText() . '<br/>';
echo 'Start: ' . $result->getStart() . '<br/>';
echo 'End: ' . $result->getEnd() . '<br/>';
echo 'Position: ' . $result->getPosition() . '<br/>';
echo 'Position history: ' . implode(', ',$result->getPositionHistory()) . '<br/>';
echo 'Type: ' . htmlspecialchars($result->getType()) . '<br/>';
echo '-----------<br/>';
}
}
}

$queryAnalysis = $field->getQueryAnalysis();
if (!empty($queryAnalysis)) {
echo '<h4>Query Analysis</h4>';
foreach ($queryAnalysis as $classes) {

echo '<h5>'.$classes->getName().'</h5>';

foreach($classes as $result) {
echo 'Text: ' . $result->getText() . '<br/>';
echo 'Raw text: ' . $result->getRawText() . '<br/>';
echo 'Start: ' . $result->getStart() . '<br/>';
echo 'End: ' . $result->getEnd() . '<br/>';
echo 'Position: ' . $result->getPosition() . '<br/>';
echo 'Position history: ' . implode(', ',$result->getPositionHistory()) . '<br/>';
echo 'Type: ' . htmlspecialchars($result->getType()) . '<br/>';
echo '-----------<br/>';
}
}
}
}
}

htmlFooter();
72 changes: 72 additions & 0 deletions examples/2.4.2-analysis-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

require('init.php');
htmlHeader();

// create a client instance
$client = new Solarium_Client($config);

// get an analysis document query
$query = $client->createAnalysisField();

$query->setShowMatch(true);
$query->setFieldName('cat,title');
$query->setFieldType('text_general');
$query->setFieldValue('Apple 60 GB iPod with Video Playback Black');
$query->setQuery('ipod');

// this executes the query and returns the result
$results = $client->analyze($query);

// show the results
foreach ($results as $result) {

echo '<hr><h2>Result list: ' . $result->getName() . '</h2>';

foreach ($result as $item) {

echo '<h3>Item: ' . $item->getName() . '</h3>';

$indexAnalysis = $item->getIndexAnalysis();
if (!empty($indexAnalysis)) {
echo '<h4>Index Analysis</h4>';
foreach ($indexAnalysis as $classes) {

echo '<h5>'.$classes->getName().'</h5>';

foreach($classes as $result) {
echo 'Text: ' . $result->getText() . '<br/>';
echo 'Raw text: ' . $result->getRawText() . '<br/>';
echo 'Start: ' . $result->getStart() . '<br/>';
echo 'End: ' . $result->getEnd() . '<br/>';
echo 'Position: ' . $result->getPosition() . '<br/>';
echo 'Position history: ' . implode(', ',$result->getPositionHistory()) . '<br/>';
echo 'Type: ' . htmlspecialchars($result->getType()) . '<br/>';
echo '-----------<br/>';
}
}
}

$queryAnalysis = $item->getQueryAnalysis();
if (!empty($queryAnalysis)) {
echo '<h4>Query Analysis</h4>';
foreach ($queryAnalysis as $classes) {

echo '<h5>'.$classes->getName().'</h5>';

foreach($classes as $result) {
echo 'Text: ' . $result->getText() . '<br/>';
echo 'Raw text: ' . $result->getRawText() . '<br/>';
echo 'Start: ' . $result->getStart() . '<br/>';
echo 'End: ' . $result->getEnd() . '<br/>';
echo 'Position: ' . $result->getPosition() . '<br/>';
echo 'Position history: ' . implode(', ',$result->getPositionHistory()) . '<br/>';
echo 'Type: ' . htmlspecialchars($result->getType()) . '<br/>';
echo '-----------<br/>';
}
}
}
}
}

htmlFooter();
6 changes: 6 additions & 0 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ <h1>Solarium examples</h1>
<li><a href="2.3.1-mlt-query.php">2.3.1 MoreLikeThis query</a></li>
<li><a href="2.3.2-mlt-stream.php">2.3.2 MoreLikeThis query input as stream</a></li>
</ul>

<li>2.4. Analysis queries</li>
<ul style="list-style:none;">
<li><a href="2.4.1-analysis-document.php">2.4.1 Analysis query for a document</a></li>
<li><a href="2.4.2-analysis-field.php">2.4.2 Analysis query for a field</a></li>
</ul>
</ul>

<li>4. Usage modes</li>
Expand Down
54 changes: 54 additions & 0 deletions library/Solarium/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ class Solarium_Client extends Solarium_Configurable
*/
const QUERYTYPE_MORELIKETHIS = 'mlt';

/**
* Querytype analysis field
*/
const QUERYTYPE_ANALYSIS_FIELD = 'analysis-field';

/**
* Querytype analysis document
*/
const QUERYTYPE_ANALYSIS_DOCUMENT = 'analysis-document';

/**
* Default options
*
Expand Down Expand Up @@ -112,6 +122,16 @@ class Solarium_Client extends Solarium_Configurable
'requestbuilder' => 'Solarium_Client_RequestBuilder_MoreLikeThis',
'responseparser' => 'Solarium_Client_ResponseParser_MoreLikeThis'
),
self::QUERYTYPE_ANALYSIS_DOCUMENT => array(
'query' => 'Solarium_Query_Analysis_Document',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Analysis_Document',
'responseparser' => 'Solarium_Client_ResponseParser_Analysis_Document'
),
self::QUERYTYPE_ANALYSIS_FIELD => array(
'query' => 'Solarium_Query_Analysis_Field',
'requestbuilder' => 'Solarium_Client_RequestBuilder_Analysis_Field',
'responseparser' => 'Solarium_Client_ResponseParser_Analysis_Field'
),
);

/**
Expand Down Expand Up @@ -587,6 +607,20 @@ public function moreLikeThis($query)
return $this->execute($query);
}

/**
* Execute an analysis query
*
* @internal This is a convenience method that forwards the query to the
* execute method, thus allowing for an easy to use and clean API.
*
* @param Solarium_Query_Analysis_Document|Solarium_Query_Analysis_Field $query
* @return Solarium_Result_Analysis_Document|Solarium_Result_Analysis_Field
*/
public function analyze($query)
{
return $this->execute($query);
}

/**
* Create a query instance
*
Expand Down Expand Up @@ -657,5 +691,25 @@ public function createPing($options = null)
return $this->createQuery(self::QUERYTYPE_PING, $options);
}

/**
* Create an analysis field query instance
*
* @param mixed $options
* @return Solarium_Query_Analysis_Field
*/
public function createAnalysisField($options = null)
{
return $this->createQuery(self::QUERYTYPE_ANALYSIS_FIELD, $options);
}

/**
* Create an analysis document query instance
*
* @param mixed $options
* @return Solarium_Query_Analysis_Document
*/
public function createAnalysisDocument($options = null)
{
return $this->createQuery(self::QUERYTYPE_ANALYSIS_DOCUMENT, $options);
}
}
66 changes: 66 additions & 0 deletions library/Solarium/Client/RequestBuilder/Analysis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
/**
* Copyright 2011 Bas de Nooijer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this listof conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of the copyright holder.
*
* @copyright Copyright 2011 Bas de Nooijer <[email protected]>
* @license http://github.com/basdenooijer/solarium/raw/master/COPYING
* @link http://www.solarium-project.org/
*
* @package Solarium
* @subpackage Client
*/

/**
* Build an analysis request
*
* @package Solarium
* @subpackage Client
*/
class Solarium_Client_RequestBuilder_Analysis extends Solarium_Client_RequestBuilder
{

/**
* Build request for an analysis query
*
* @param Solarium_Query_Analysis $query
* @return Solarium_Client_Request
*/
public function build($query)
{
$request = new Solarium_Client_Request;
$request->setHandler($query->getHandler());

$request->addParam('wt', 'json');
$request->addParam('analysis.query', $query->getQuery());
$request->addParam('analysis.showmatch', $query->getShowMatch());

return $request;
}

}
Loading

0 comments on commit 18a6a87

Please sign in to comment.