source: sandbox/expresso-solr/expressoMail1_2/solrclient/library/Solarium/Query/Select/Component/DistributedSearch.php @ 7588

Revision 7588, 5.8 KB checked in by adir, 11 years ago (diff)

Ticket #000 - Adicionando a integracao de buscas com Solr na base a ser isnerida na comunidade

Line 
1<?php
2/**
3 * Copyright 2011 Bas de Nooijer. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 *    this listof conditions and the following disclaimer in the documentation
13 *    and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 *
27 * The views and conclusions contained in the software and documentation are
28 * those of the authors and should not be interpreted as representing official
29 * policies, either expressed or implied, of the copyright holder.
30 *
31 * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl>
32 * @license http://github.com/basdenooijer/solarium/raw/master/COPYING
33 * @link http://www.solarium-project.org/
34 *
35 * @package Solarium
36 * @subpackage Query
37 */
38
39/**
40 * Distributed Search (sharding) component
41 *
42 * @link http://wiki.apache.org/solr/DistributedSearch
43 *
44 * @package Solarium
45 * @subpackage Query
46 */
47class Solarium_Query_Select_Component_DistributedSearch extends Solarium_Query_Select_Component
48{
49
50    /**
51     * Component type
52     *
53     * @var string
54     */
55    protected $_type = Solarium_Query_Select::COMPONENT_DISTRIBUTEDSEARCH;
56
57    /**
58     * Request to be distributed across all shards in the list
59     *
60     * @var array
61     */
62    protected $_shards = array();
63
64    /**
65     * Initialize options
66     *
67     * Several options need some extra checks or setup work, for these options
68     * the setters are called.
69     *
70     * @return void
71     */
72    protected function _init()
73    {
74        foreach ($this->_options AS $name => $value) {
75            switch ($name) {
76                case 'shards':
77                    $this->setShards($value);
78                    break;
79            }
80        }
81    }
82
83    /**
84     * Add a shard
85     *
86     * @param string $key unique string
87     * @param string $shard  The syntax is host:port/base_url
88     * @return Solarium_Query_Select Provides fluent interface
89     * @link http://wiki.apache.org/solr/DistributedSearch
90     */
91    public function addShard($key, $shard)
92    {
93        $this->_shards[$key] = $shard;
94        return $this;
95    }
96
97    /**
98     * Add multiple shards
99     *
100     * Example usage:
101     * <code>
102     * $client = new Solarium_Client;
103     * $query = $client->createSelect();
104     * $distributedSearch = $query->getDistributedSearch();
105     * $distributedSearch->addShards(array(
106     *     'core0' => 'localhost:8983/solr/core0',
107     *     'core1' => 'localhost:8983/solr/core1'
108     * ));
109     * $result = $client->select($query);
110     * </code>
111     * @param array $shards
112     * @return Solarium_Query_Select Provides fluent interface
113     */
114    public function addShards(array $shards)
115    {
116        foreach ($shards as $key => $shard) {
117            $this->addShard($key, $shard);
118        }
119
120        return $this;
121    }
122
123    /**
124     * Remove a shard
125     *
126     * @param string $key
127     * @return Solarium_Query_Select Provides fluent interface
128     */
129    public function removeShard($key)
130    {
131        if (isset($this->_shards[$key])) {
132            unset($this->_shards[$key]);
133        }
134
135        return $this;
136    }
137
138    /**
139     * Remove all shards
140     *
141     * @return Solarium_Query_Select Provides fluent interface
142     */
143    public function clearShards()
144    {
145        $this->_shards = array();
146        return $this;
147    }
148
149    /**
150     * Set multiple shards
151     *
152     * This overwrites any existing shards
153     *
154     * Example usage:
155     * <code>
156     * $client = new Solarium_Client;
157     * $query = $client->createSelect();
158     * $distributedSearch = $query->getDistributedSearch();
159     * $distributedSearch->setShards(array(
160     *     'core0' => 'localhost:8983/solr/core0',
161     *     'core1' => 'localhost:8983/solr/core1'
162     * ));
163     * $result = $client->select($query);
164     * </code>
165     *
166     * @param array $shards Associative array of shards
167     * @return Solarium_Query_Select Provides fluent interface
168     */
169    public function setShards(array $shards)
170    {
171        $this->clearShards();
172        $this->addShards($shards);
173
174        return $this;
175    }
176
177    /**
178     * Get a list of the shards
179     *
180     * @return array
181     */
182    public function getShards()
183    {
184        return $this->_shards;
185    }
186
187    /**
188     *  A sharded request will go to the standard request handler
189     *  (not necessarily the original); this can be overridden via shards.qt
190     *
191     * @param string
192     * @return Solarium_Query_Select Provides fluent interface
193     */
194    public function setShardRequestHandler($handler)
195    {
196        $this->_setOption('shardhandler', $handler);
197        return $this;
198    }
199
200    /**
201     * Get a shard request handler (shards.qt)
202     *
203     * @param string
204     * @return Solarium_Query_Select Provides fluent interface
205     */
206    public function getShardRequestHandler()
207    {
208        return $this->getOption('shardhandler');
209    }
210}
Note: See TracBrowser for help on using the repository browser.