示例#1
0
 /**
  * Adds a document to Solr Index
  * 
  * Applies index-time boosting (optional) 
  * 
  * @param array $data associative array of key value
  * @param bool $overwrite
  * @param int $commitWithin commit within (in milliseconds)
  * @return SolrUpdateResponse
  */
 public function indexDocument(array $data, $overwrite = true, $commitWithin = false)
 {
     $doc = new SolrInputDocument();
     foreach ($data as $field => $value) {
         if (isset($this->_boosts[$field])) {
             $boost = $this->_boosts[$field];
         } else {
             $boost = NULL;
         }
         if (is_array($value)) {
             for ($i = 0, $count = count($value); $i < $count; $i++) {
                 $enc = mb_detect_encoding($value[$i]);
                 $value[$i] = @iconv($enc, 'UTF-8//IGNORE', $value[$i]);
                 $doc->addField($field, $value[$i], $boost);
             }
         } else {
             $enc = mb_detect_encoding($value);
             $value = @iconv($enc, 'UTF-8//IGNORE', $value);
             $doc->addField($field, $value, $boost);
         }
     }
     try {
         return $this->addDocument($doc, $overwrite, $commitWithin);
     } catch (Exception $e) {
         error_log($this->getDebug());
         return FALSE;
     }
 }
 /**
  * @param object $entity
  * @return \SolrDocument
  */
 public function createDocument(MetaInformation $meta)
 {
     $document = new \SolrInputDocument();
     $document->addField('id', $meta->getEntityId());
     $document->addField('document_name_s', $meta->getDocumentName());
     $document->setBoost($meta->getBoost());
     return $document;
 }
 /**
  * (non-PHPdoc)
  * @see \FS\SolrBundle\Query\AbstractQuery::getQueryString()
  */
 public function getQueryString()
 {
     $documentNameField = $this->document->getField('document_name_s');
     if ($documentNameField == null) {
         throw new \RuntimeException('documentName should not be null');
     }
     $this->solrQuery->addFilterQuery(sprintf('document_name_s:%s', $documentNameField->values[0]));
     return '';
 }
示例#4
0
 public function addDocumentToSearch($id, $txt)
 {
     $this->solrConnect();
     $doc = new \SolrInputDocument();
     $doc->addField('id', $id);
     $doc->addField('text', $txt);
     $updateResponse = $this->client->addDocument($doc);
     $response = $updateResponse->getResponse();
     $this->client->commit();
     return $response;
 }
 public function testGetQuery_DocumentNameMissing()
 {
     $document = new \SolrInputDocument();
     $document->addField('id', '1');
     $query = new FindByIdentifierQuery($document);
     try {
         $queryString = $query->getQueryString();
         $this->fail('an exception should be thrown');
     } catch (\RuntimeException $e) {
         $this->assertEquals('documentName should not be null', $e->getMessage());
     }
 }
 public function testGetQuery_SearchInAllFields()
 {
     $document = new \SolrInputDocument();
     $document->addField('document_name_s', 'validtestentity');
     $expectedQuery = '';
     $query = new FindByDocumentNameQuery($document);
     $filterQueries = $query->getSolrQuery()->getFilterQueries();
     $queryString = $query->getQueryString();
     $this->assertEquals($expectedQuery, $queryString, 'query');
     $this->assertEquals(1, count($filterQueries));
     $actualFilterQuery = array_pop($filterQueries);
     $this->assertEquals('document_name_s:validtestentity', $actualFilterQuery, 'filter query');
 }
示例#7
0
 public function testFindAll()
 {
     $document = new \SolrInputDocument();
     $document->addField('id', 2);
     $document->addField('document_name_s', 'post');
     $metaFactory = $this->getMock('FS\\SolrBundle\\Doctrine\\Mapper\\MetaInformationFactory', array(), array(), '', false);
     $metaFactory->expects($this->once())->method('loadInformation')->will($this->returnValue(MetaTestInformationFactory::getMetaInformation()));
     $mapper = $this->getMock('FS\\SolrBundle\\Doctrine\\Mapper\\EntityMapper');
     $mapper->expects($this->once())->method('toDocument')->will($this->returnValue($document));
     $solr = $this->getMock('FS\\SolrBundle\\SolrFacade', array(), array(), '', false);
     $solr->expects($this->once())->method('getMapper')->will($this->returnValue($mapper));
     $solr->expects($this->once())->method('getCommandFactory')->will($this->returnValue(CommandFactoryStub::getFactoryWithAllMappingCommand()));
     $solr->expects($this->once())->method('getMetaFactory')->will($this->returnValue($metaFactory));
     $entity = new ValidTestEntity();
     $solr->expects($this->once())->method('query')->will($this->returnValue(array($entity)));
     $repo = new Repository($solr, $entity);
     $actual = $repo->findAll();
     $this->assertTrue(is_array($actual));
     $this->assertFalse($document->fieldExists('id'), 'id was removed');
 }
示例#8
0
 /**
  * @param CActiveModel $model
  * @param bool $exportTasks
  * @param bool $isMain
  * @return SolrInputDocument
  */
 private static function createSolrInputDocument(CActiveModel $model, $exportTasks = false, $isMain = false)
 {
     $doc = new SolrInputDocument();
     $doc->addField("id", $model->getRecord()->getTable() . "_" . $model->getId());
     $doc->addField("_doc_id_", $model->getId());
     /**
      * Выгружаем дополнительные выгружаемые поля
      */
     $metaModel = CCoreObjectsManager::getCoreModel(get_class($model));
     if (!is_null($metaModel)) {
         foreach ($metaModel->fields->getItems() as $field) {
             if ($field->isExportable()) {
                 $name = $field->field_name;
                 $doc->addField($name, $model->{$name});
             }
         }
     }
     /**
      * Выгружаем список задач, с которыми связана модель
      */
     if ($exportTasks) {
         foreach ($metaModel->tasks->getItems() as $task) {
             $doc->addField("_tasks_", $task->getId());
         }
     }
     /**
      * Класс модели
      */
     $doc->addField("_class_", $metaModel->class_name);
     /**
      * Модель является основной
      */
     if ($isMain) {
         $doc->addField("_is_main_", "1");
     } else {
         $doc->addField("_is_main_", "0");
     }
     return $doc;
 }
示例#9
0
 /**
  * Gets the solr input document
  * @return SolrInputDocument the solr document
  */
 public function getInputDocument()
 {
     if ($this->_inputDocument !== null) {
         return $this->_inputDocument;
     }
     $this->_inputDocument = new SolrInputDocument();
     foreach ($this->attributeNames() as $attribute) {
         if ($this->{$attribute} !== null) {
             if (is_array($this->{$attribute})) {
                 foreach ($this->{$attribute} as $value) {
                     $this->_inputDocument->addField($attribute, $value);
                 }
             } else {
                 $this->_inputDocument->addField($attribute, $this->prepareAttribute($attribute));
             }
         }
     }
     return $this->_inputDocument;
 }
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_STORE_PATH);
$client = new SolrClient($options);
$product = new SolrInputDocument();
$product->addField('id', 'black');
$product->addField('cat', 'tshirt');
$product->addField('cat', 'polo');
$product->addField('content_type', 'product');
$small = new SolrInputDocument();
$small->addField('id', 'TS-BLK-S');
$small->addField('content_type', 'sku');
$small->addField('size', 'S');
$small->addField('inventory', 100);
$medium = new SolrInputDocument();
$medium->addField('id', 'TS-BLK-M');
$medium->addField('content_type', 'sku');
$medium->addField('size', 'M');
$medium->addField('inventory', 200);
$large = new SolrInputDocument();
$large->addField('id', 'TS-BLK-L');
$large->addField('content_type', 'sku');
$large->addField('size', 'L');
$large->addField('inventory', 300);
// add child documents
$product->addChildDocument($small);
$product->addChildDocument($medium);
$product->addChildDocument($large);
// or
// $skus = [$small, $medium, $large];
示例#11
0
 protected function assertFieldCount($expectedCount, \SolrInputDocument $document, $message = '')
 {
     $this->assertEquals($expectedCount + self::FIELDS_ALWAYS_MAPPED, $document->getFieldCount(), $message);
 }
示例#12
0
 /**
  * Adds a document to the search engine.
  *
  * This does not commit to the search engine.
  *
  * @param array $doc
  * @return void
  */
 public function add_document($doc)
 {
     $solrdoc = new \SolrInputDocument();
     foreach ($doc as $field => $value) {
         $solrdoc->addField($field, $value);
     }
     try {
         $result = $this->get_search_client()->addDocument($solrdoc, true, static::AUTOCOMMIT_WITHIN);
     } catch (\SolrClientException $e) {
         debugging('Solr client error adding document with id ' . $doc['id'] . ': ' . $e->getMessage(), DEBUG_DEVELOPER);
     }
 }
 /**
  * Index a Resource.
  *
  * @param array $fields
  * @return boolean
  */
 public function index(array $fields = array())
 {
     if (isset($fields['searchable']) && empty($fields['searchable'])) {
         return false;
     }
     if (isset($fields['published']) && empty($fields['published'])) {
         return false;
     }
     if (isset($fields['deleted']) && !empty($fields['deleted'])) {
         return false;
     }
     $document = new SolrInputDocument();
     $dateFields = array('createdon', 'editedon', 'deletedon', 'publishedon');
     foreach ($fields as $fieldName => $value) {
         if (is_string($fieldName) && !is_array($value) && !is_object($value)) {
             if (in_array($fieldName, $dateFields)) {
                 $value = '' . strftime('%Y-%m-%dT%H:%M:%SZ', strtotime($value));
                 $fields[$fieldName] = $value;
             }
             $document->addField($fieldName, $value);
         }
     }
     $this->modx->log(modX::LOG_LEVEL_DEBUG, '[SimpleSearch] Indexing Resource: ' . print_r($fields, true));
     $response = false;
     try {
         $response = $this->client->addDocument($document);
     } catch (Exception $e) {
         $this->modx->log(xPDO::LOG_LEVEL_ERROR, 'Error adding Document to index on Solr server: ' . $e->getMessage());
     }
     $this->commit();
     return $response;
 }
示例#14
0
 public function addKeywordsintoSolrDocument($id, $name, $alias, $priority, $id_news, $type)
 {
     try {
         if (empty($id) === FALSE && is_numeric($id)) {
             $this->config->load('solr');
             $solr = $this->config->item('solr');
             $options = array('hostname' => $solr['SOLR_SERVER_HOSTNAME'], 'login' => $solr['SOLR_SERVER_USERNAME'], 'password' => $solr['SOLR_SERVER_PASSWORD'], 'port' => $solr['SOLR_SERVER_PORT'], 'path' => 'solr/keywords');
             $client = new SolrClient($options);
             $doc = new SolrInputDocument();
             $doc->addField('id', $id);
             $doc->addField('name', $name);
             $doc->addField('alias', $alias);
             $doc->addField('type', $type);
             $doc->addField('priority', $priority);
             $doc->addField('id_news', $id_news);
             $updateResponse = $client->addDocument($doc);
             $client->commit();
             return $updateResponse->getResponse();
         } else {
             return FALSE;
         }
     } catch (Exception $e) {
         return FALSE;
     }
 }
示例#15
0
 /**
  * Index the current search result.
  *
  * @param array $fields
  * @return bool
  */
 public function index(array $fields = array(), array $options = array())
 {
     $document = new SolrInputDocument();
     $document->addField('id', $fields['id']);
     $document->addField('private', $fields['private']);
     $document->addField('username', $fields['username']);
     $document->addField('createdon', '' . strftime('%Y-%m-%dT%H:%M:%SZ', strtotime($fields['createdon'])));
     $document->addField('board', $fields['board']);
     $document->addField('author', $fields['author']);
     $document->addField('thread', $fields['thread']);
     $document->addField('title', $fields['title'], 2);
     $document->addField('message', $fields['message'], 2);
     $document->addField('url', $fields['url']);
     if (!empty($fields['replies'])) {
         $document->addField('replies', $fields['replies']);
     }
     if (!empty($fields['board_name'])) {
         $document->addField('board_name', $fields['board_name']);
     }
     if (!empty($fields['category'])) {
         $document->addField('category', $fields['category']);
     }
     if (!empty($fields['category_name'])) {
         $document->addField('category_name', $fields['category_name']);
     }
     if (!empty($fields['answered_question'])) {
         $document->addField('answered_question', $fields['answered_question']);
     }
     $response = false;
     try {
         $response = $this->client->addDocument($document);
         if (isset($options['commit']) && $options['commit'] !== false) {
             $this->commit();
         }
     } catch (Exception $e) {
         $response = $e->getMessage();
     }
     return $response;
 }
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$doc = new SolrInputDocument();
$doc->addField('id', 334455);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');
$doc2 = clone $doc;
$doc2->deleteField('id');
$doc2->addField('id', 334456);
$docs = array($doc, $doc2);
$updateResponse = $client->addDocuments($docs, true, 1024);
print $updateResponse->getRawRequest();
示例#17
0
 public function testQuery_OneDocumentFound()
 {
     $arrayObj = new SolrDocumentStub(array('title_s' => 'title'));
     $responseArray['response']['docs'][] = $arrayObj;
     $this->solrClientFake->setResponse(new SolrResponseFake($responseArray));
     $solr = new SolrFacade($this->connectionFactory, $this->commandFactory, $this->eventManager, $this->metaFactory);
     $document = new \SolrInputDocument();
     $document->addField('document_name_s', 'name');
     $query = new FindByDocumentNameQuery($document);
     $query->setEntity(new ValidTestEntity());
     $entities = $solr->query($query);
     $this->assertEquals(1, count($entities));
 }
 public function addDocument($arrdoc, $use_autocommit = 0)
 {
     //--
     $connect = $this->solr_connect();
     //--
     if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
         //--
         SmartFrameworkRegistry::setDebugMsg('db', 'solr|total-queries', 1, '+');
         //--
         $time_start = microtime(true);
         //--
     }
     //end if
     //--
     if (!is_array($arrdoc)) {
         Smart::log_warning('Solr ERROR # addDocument # ' . 'Document is not Array');
         return -100;
     }
     //end if
     //--
     if (Smart::array_size($arrdoc) <= 0) {
         Smart::log_warning('Solr ERROR # addDocument # ' . 'Document Array is empty !');
         return -101;
     }
     //end if
     //--
     $doc = new SolrInputDocument();
     //--
     foreach ($arrdoc as $key => $val) {
         //--
         if (is_array($val)) {
             foreach ($val as $k => $v) {
                 $doc->addField((string) $key, (string) $v);
             }
             //end foreach
         } else {
             $doc->addField((string) $key, (string) $val);
         }
         //end if
         //--
     }
     //end foreach
     //--
     try {
         //--
         if ((int) $use_autocommit > 0) {
             $updateResponse = $this->instance->addDocument($doc, true, (int) $use_autocommit);
         } else {
             $updateResponse = $this->instance->addDocument($doc, true, 0);
             $this->instance->commit();
             // save
         }
         //end if else
         //--
     } catch (Exception $e) {
         //--
         Smart::log_warning('Solr ERROR # addDocument # EXCEPTION: ' . $e->getMessage() . "\n" . print_r($arrdoc, 1));
         return -201;
         //--
     }
     //end try catch
     //--
     $response = $updateResponse->getResponse();
     // get answer message
     //print_r($response);
     //--
     if ((string) SMART_FRAMEWORK_DEBUG_MODE == 'yes') {
         //--
         $time_end = (double) (microtime(true) - (double) $time_start);
         //--
         SmartFrameworkRegistry::setDebugMsg('db', 'solr|total-time', $time_end, '+');
         //--
         SmartFrameworkRegistry::setDebugMsg('db', 'solr|log', ['type' => 'nosql', 'data' => 'ADD-UPDATE-QUERY', 'command' => $arrdoc, 'time' => Smart::format_number_dec($time_end, 9, '.', '')]);
         //--
     }
     //end if
     //--
     if (is_object($response)) {
         if ($response instanceof SolrObject) {
             if (is_object($response['responseHeader'])) {
                 if ($response['responseHeader'] instanceof SolrObject) {
                     if ($response['responseHeader']->status === 0) {
                         // OK
                     } else {
                         Smart::log_warning('Solr ERROR # addDocument # Invalid Status (' . $response['responseHeader']->status . ') : ' . print_r($arrdoc, 1));
                         return -206;
                     }
                     //end if else
                 } else {
                     Smart::log_warning('Solr ERROR # addDocument # Invalid responseHeader / Not instanceof SolrObject: ' . print_r($arrdoc, 1));
                     return -205;
                 }
                 //end if else
             } else {
                 Smart::log_warning('Solr ERROR # addDocument # Invalid responseHeader / Invalid Object: ' . print_r($arrdoc, 1));
                 return -204;
             }
             //end if else
         } else {
             Smart::log_warning('Solr ERROR # addDocument # Invalid Answer / Not instanceof SolrObject: ' . print_r($arrdoc, 1));
             return -203;
         }
         //end if else
     } else {
         Smart::log_warning('Solr ERROR # addDocument # Not Object: ' . print_r($arrdoc, 1));
         return -202;
     }
     //end if else
     //--
     return 0;
     // OK
     //--
 }
示例#19
0
 /**
  * Adds a document to the solr index
  * @param ASolrDocument|SolrInputDocument $document the document to add to the index
  * @param integer $commitWithin the number of milliseconds to commit within after indexing the document
  * @return boolean true if the document was indexed successfully
  */
 public function index($document, $commitWithin = null)
 {
     // When we add documents we want to overwrite existing documents and avoid duplicates (several documents with the same ID).
     $overwrite = true;
     if (version_compare(solr_get_version(), '2.0.0', '<')) {
         // PECL Solr < 2.0 $allowDups was used instead of $overwrite, which does the same functionality with exact opposite bool flag.
         // See http://www.php.net/manual/en/solrclient.adddocument.php
         $overwrite = false;
         // Equivalent of $allowDups = false;
     }
     if ($document instanceof IASolrDocument) {
         if ($commitWithin === null && $document->getCommitWithin() > 0) {
             $commitWithin = $document->getCommitWithin();
         }
         $document = $document->getInputDocument();
     } elseif (is_array($document) || $document instanceof Traversable) {
         if ($commitWithin === null) {
             $commitWithin = 0;
         }
         $document = (array) $document;
         foreach ($document as $key => $value) {
             if ($value instanceof IASolrDocument) {
                 $document[$key] = $value->getInputDocument();
             }
         }
         Yii::trace('Adding ' . count($document) . " documents to the solr index", 'packages.solr.ASolrConnection');
         return $this->getClient()->addDocuments($document, $overwrite, $commitWithin)->success();
     }
     if ($commitWithin === null) {
         $commitWithin = 0;
     }
     Yii::trace('Adding 1 document to the solr index', 'packages.solr.ASolrConnection');
     $response = $this->getClient()->addDocument($document, $overwrite, $commitWithin);
     return $response->success();
 }
示例#20
0
 private function _process_import_order($order_table, $order_id, $start_time, $end_time, $limit, $offset)
 {
     $total = $this->solr_order_model->fetch_order_count_by_updated($order_table, $start_time, $end_time);
     $successful = TRUE;
     $options = array('hostname' => SOLR_SERVER_HOSTNAME, 'port' => SOLR_SERVER_PORT);
     echo 'table: ' . $order_table, "\n";
     echo 'total: ' . $total, "\n";
     do {
         echo 'off set: ' . $offset, "\n";
         $orders = $this->solr_order_model->fetch_orders_by_updated($order_table, $start_time, $end_time, $limit, $offset);
         $docs = array();
         foreach ($orders as $order) {
             $client = new SolrClient($options);
             $doc = new SolrInputDocument();
             $doc->addField('id', $order_table . ':' . $order->{$order_id});
             $skus = explode(',', $order->sku_str);
             foreach ($skus as $sku) {
                 $doc->addField('skus', $sku);
             }
             $doc->addField('list_datetime', $order->list_date . 'T' . $order->list_time . "Z");
             $doc->addField('buyer_name', $order->name);
             $doc->addField('buyer_id', $order->buyer_id);
             $doc->addField('list_type', $order->list_type);
             $doc->addField('payment_status', $order->payment_status);
             $doc->addField('subject', $order->subject);
             $doc->addField('currency', $order->currency);
             $doc->addField('gross', make_number($order->gross, 0));
             $doc->addField('fee', make_number($order->fee, 0));
             $doc->addField('net', make_number($order->net, 0));
             $doc->addField('time_zone', $order->time_zone);
             $doc->addField('note', $order->note);
             $doc->addField('buyer_email', $order->from_email);
             $doc->addField('company_email', $order->to_email);
             $doc->addField('transaction_id', $order->transaction_id);
             $doc->addField('payment_type', $order->payment_type);
             $doc->addField('shipping_address', $order->shipping_address);
             $doc->addField('address_status', $order->address_status);
             $item_titles = explode_item_title($order->item_title_str);
             foreach ($item_titles as $item_title) {
                 $doc->addField('item_titles', $item_title);
             }
             $item_ids = explode(',', $order->item_id_str);
             foreach ($item_ids as $item_id) {
                 $doc->addField('item_ids', $item_id);
             }
             $doc->addField('item_url', $order->item_url);
             $doc->addField('closing_date', $order->closing_date);
             $doc->addField('invoice_number', $order->invoice_number);
             $doc->addField('address_line_1', $order->address_line_1);
             $doc->addField('address_line_2', $order->address_line_2);
             $doc->addField('town_city', $order->town_city);
             $doc->addField('state_province', $order->state_province);
             $doc->addField('country', $order->country);
             $doc->addField('zip_code', $order->zip_code);
             $doc->addField('contact_phone_number', $order->contact_phone_number);
             $doc->addField('income_type', $order->income_type);
             $qties = explode(',', $order->qty_str);
             foreach ($qties as $qty) {
                 //$doc->addField('qties', make_number($qty));
             }
             $doc->addField('description', $order->descript);
             $doc->addField('input_datetime', to_utc_format($order->input_date));
             $doc->addField('input_user', $order->input_user);
             $doc->addField('order_status', make_number($order->order_status));
             $doc->addField('check_date', to_utc_format($order->check_date));
             $doc->addField('check_user', $order->check_user);
             $doc->addField('print_label_date', to_utc_format($order->print_label_date));
             $doc->addField('label_content', $order->label_content);
             $doc->addField('item_no', $order->item_no);
             $doc->addField('print_label_user', $order->print_label_user);
             $doc->addField('ship_confirm_date', to_utc_format($order->ship_confirm_date));
             $doc->addField('ship_confirm_user', $order->ship_confirm_user);
             $doc->addField('ship_weight', make_number($order->ship_weight, 0));
             $doc->addField('ship_remark', $order->ship_remark);
             $doc->addField('track_number', $order->track_number);
             $doc->addField('shipping_code', $order->is_register, 0);
             $doc->addField('cost', make_number($order->cost, 0));
             $doc->addField('cost_date', to_utc_format($order->cost_date));
             $doc->addField('cost_user', $order->cost_user);
             $product_costs = explode(',', trim($order->product_cost, ','));
             foreach ($product_costs as $product_cost) {
                 $doc->addField('product_costs', make_number($product_cost, 0));
             }
             $doc->addField('product_total_cost', $order->product_cost_all);
             $doc->addField('shipping_cost', make_number($order->shipping_cost, 0));
             $doc->addField('return_date', to_utc_format($order->return_date));
             $doc->addField('return_remark', $order->return_remark);
             $doc->addField('return_user', $order->return_user);
             $doc->addField('return_why', $order->return_why);
             $doc->addField('return_order', $order->return_order);
             $doc->addField('return_cost', make_number($order->return_cost, 0));
             $doc->addField('sys_remark', $order->sys_remark);
             $doc->addField('order_receive_date', $order->order_receive_date);
             $doc->addField('email_status', $order->email_status);
             $doc->addField('stock_user_id', make_number($order->stock_user_id));
             $doc->addField('saler_id', make_number($order->saler_id));
             $purchaser_ids = explode(',', $order->purchaser_id_str);
             foreach ($purchaser_ids as $purchaser_id) {
                 $doc->addField('purchaser_ids', make_number($purchaser_id));
             }
             $developer_ids = explode(',', $order->developer_id);
             foreach ($developer_ids as $developer_id) {
                 $doc->addField('developer_ids', make_number($developer_id));
             }
             $doc->addField('trade_fee', make_number($order->trade_fee, 0));
             $doc->addField('listing_fee', make_number($order->listing_fee, 0));
             $doc->addField('profit_rate', make_number($order->profit_rate, 0));
             $doc->addField('refund_verify_status', $order->refund_verify_status);
             $doc->addField('refund_verify_type', $order->refund_verify_type);
             $doc->addField('refund_verify_content', $order->refund_verify_content);
             $refund_duties = explode(',', $order->refund_duty);
             foreach ($refund_duties as $refund_duty) {
                 $doc->addField('refund_duties', $refund_duty);
             }
             $refund_skus = explode(',', $order->refund_sku_str);
             foreach ($refund_skus as $refund_sku) {
                 $doc->addField('refund_skus', $refund_sku);
             }
             $docs[] = $doc;
         }
         try {
             if (!empty($docs)) {
                 $response = $client->addDocuments($docs);
                 $client->commit();
             }
         } catch (SolrException $e) {
             $successful = FALSE;
             var_dump($e);
             break;
         }
         $offset += $limit;
     } while ($offset < $total);
     return $successful;
 }
示例#21
0
 function add($docs)
 {
     $this->getClient();
     $doc = new SolrInputDocument();
     foreach ($docs as $field => $value) {
         $doc->addField($field, $value);
     }
     $this->_response = $this->_client->addDocument($doc, $overwrite = true);
 }
<?php 
function commit2()
{
    $solrAddress = 'http://' . SOLR_SERVER_HOSTNAME . ':' . SOLR_SERVER_PORT . '/solr/collection1';
    $response = file_get_contents($solrAddress . '/update?commit=true');
}
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', 334456);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');
$updateResponse = $client->addDocument($doc);
print_r($updateResponse->getResponse());
// Broken!
//$client->commit();
// Use our own solution instead!
commit2();
示例#23
0
 /**
  * Tests performing a facetted search without getting results at the same time
  */
 public function testFacetsWithoutResults()
 {
     $connection = new ASolrConnection();
     $connection->clientOptions->hostname = SOLR_HOSTNAME;
     $connection->clientOptions->port = SOLR_PORT;
     $connection->clientOptions->path = SOLR_PATH;
     $doc = new SolrInputDocument();
     $doc->addField('id', 334455);
     $doc->addField('cat', 'Software');
     $doc->addField('cat', 'Lucene');
     $doc->addField("popularity", 20);
     $doc->addField("incubationdate_dt", date("Y-m-d\\TH:i:s\\Z"));
     $this->assertTrue($connection->index($doc));
     $this->assertTrue($connection->commit());
     $criteria = new ASolrCriteria();
     $criteria->query = "lucene";
     $criteria->offset = 0;
     $criteria->limit = 0;
     $criteria->facet = true;
     $criteria->addFacetField("cat")->addFacetField("name");
     $criteria->addFacetDateField("incubationdate_dt");
     $criteria->facetDateStart = "2005-10-10T00:00:00Z";
     $criteria->facetDateEnd = "2015-10-10T00:00:00Z";
     $criteria->facetDateGap = "+12MONTH";
     $criteria->addFacetQuery("popularity:[* TO 10]");
     $criteria->addFacetQuery("popularity:[10 TO 20]");
     $criteria->addFacetQuery("popularity:[20 TO *]");
     $response = $connection->search($criteria);
     $this->assertTrue($response instanceof ASolrQueryResponse);
     $this->assertTrue(isset($response->getDateFacets()->incubationdate_dt));
     $this->assertTrue($response->getDateFacets()->incubationdate_dt instanceof ASolrFacet);
     $results = $response->getResults();
     $this->assertEquals(0, count($results));
     $this->assertTrue($connection->delete(334455));
     $this->assertTrue($connection->commit());
 }
示例#24
0
 function query($args, $document)
 {
     $this->validate($args);
     $xml = new xml();
     $solr = $this->datasource->get($this->core);
     switch ($this->method) {
         case 'add':
             !empty($args) or runtime_error('Solr add method should accept parameters');
             if (is_array(reset($args))) {
                 $docs = array();
                 foreach (reset($args) as $document) {
                     $doc = new SolrInputDocument();
                     foreach ($document as $name => $value) {
                         if (is_array($value)) {
                             foreach ($value as $element) {
                                 $doc->addField($name, $element);
                             }
                         } else {
                             $doc->addField($name, $value);
                         }
                     }
                     $docs[] = $doc;
                 }
                 $solr->addDocuments($docs);
             } else {
                 $doc = new SolrInputDocument();
                 foreach ($args as $name => $value) {
                     $doc->addField($name, $value);
                 }
                 $solr->addDocument($doc);
             }
             $solr->request("<commit/>");
             break;
         case 'delete':
             $solr->deleteByQuery(vars::apply_assoc($this->body, $args));
             $solr->request("<commit/>");
             break;
         case 'query':
             $root = $xml->element($this->root[0]);
             $xml->append($root);
             $query = new SolrQuery(vars::apply_assoc($this->body, $args));
             foreach ($this->order_by as $name => $order) {
                 $query->addSortField($name, $order == 'desc' ? SolrQuery::ORDER_DESC : SolrQuery::ORDER_ASC);
             }
             if (!is_null($this->offset)) {
                 $query->setStart(vars::apply_assoc($this->offset, $args));
             }
             is_null($this->count) or $query->setRows(vars::apply_assoc($this->count, $args));
             $response = $solr->query($query);
             $object = $response->getResponse();
             if (is_array($object['response']['docs'])) {
                 $root['@matched'] = $object['response']['numFound'];
                 foreach ($object['response']['docs'] as $doc) {
                     $item = $xml->element($this->item[0]);
                     $root->append($item);
                     foreach ($doc as $name => $value) {
                         if (is_array($value)) {
                             $array = $xml->element($name);
                             $item->append($array);
                             foreach ($value as $element) {
                                 $element = $xml->element('element', $element);
                                 $array->append($element);
                             }
                         } else {
                             $node = $this->transform($xml, $name, $value);
                             $item->append($node);
                         }
                     }
                 }
             } else {
                 $this->empty or runtime_error('Procedure returned an empty result: ' . $this->mangled());
             }
             break;
         default:
             runtime_error('Unknown Solr method: ' . $this->method);
     }
     return $xml;
 }
示例#25
0
文件: engine.php 项目: janeklb/moodle
 /**
  * Adds a text document to the search engine.
  *
  * @param array $doc
  * @return bool
  */
 protected function add_solr_document($doc)
 {
     $solrdoc = new \SolrInputDocument();
     foreach ($doc as $field => $value) {
         $solrdoc->addField($field, $value);
     }
     try {
         $result = $this->get_search_client()->addDocument($solrdoc, true, static::AUTOCOMMIT_WITHIN);
         return true;
     } catch (\SolrClientException $e) {
         debugging('Solr client error adding document with id ' . $doc['id'] . ': ' . $e->getMessage(), DEBUG_DEVELOPER);
     } catch (\SolrServerException $e) {
         // We only use the first line of the message, as it's a fully java stacktrace behind it.
         $msg = strtok($e->getMessage(), "\n");
         debugging('Solr server error adding document with id ' . $doc['id'] . ': ' . $msg, DEBUG_DEVELOPER);
     }
     return false;
 }
示例#26
0
 public function addListKeywordtoSolr()
 {
     try {
         $keywords = $this->m_backend->jqxGets('keywords');
         if (empty($keywords) === FALSE) {
             $this->config->load('solr');
             $solr = $this->config->item('solr');
             $options = array('hostname' => $solr['SOLR_SERVER_HOSTNAME'], 'login' => $solr['SOLR_SERVER_USERNAME'], 'password' => $solr['SOLR_SERVER_PASSWORD'], 'port' => $solr['SOLR_SERVER_PORT'], 'path' => 'solr/keywords');
             $client = new SolrClient($options);
             foreach ($keywords as $keyword) {
                 $doc = new SolrInputDocument();
                 $doc->addField('id', $keyword['id']);
                 $doc->addField('name', $keyword['name']);
                 $doc->addField('alias', $keyword['alias']);
                 $doc->addField('id_news', $keyword['id_news']);
                 $doc->addField('type', $keyword['type']);
                 $doc->addField('priority', $keyword['priority']);
                 $updateResponse = $client->addDocument($doc);
                 $client->commit();
             }
         } else {
             return FALSE;
         }
     } catch (Exception $e) {
         return FALSE;
     }
 }
示例#27
0
 public function toSolrDocument()
 {
     $doc = new SolrInputDocument();
     $uri = Zotero_Solr::getItemURI($this->libraryID, $this->key);
     $doc->addField("uri", $uri);
     // Primary fields
     foreach (Zotero_Items::$primaryFields as $field) {
         switch ($field) {
             case 'itemID':
             case 'numAttachments':
             case 'numNotes':
                 continue 2;
             case 'itemTypeID':
                 $xmlField = 'itemType';
                 $xmlValue = Zotero_ItemTypes::getName($this->{$field});
                 break;
             case 'dateAdded':
             case 'dateModified':
             case 'serverDateModified':
                 $xmlField = $field;
                 $xmlValue = Zotero_Date::sqlToISO8601($this->{$field});
                 break;
             default:
                 $xmlField = $field;
                 $xmlValue = $this->{$field};
         }
         $doc->addField($xmlField, $xmlValue);
     }
     // Title for sorting
     $title = $this->getDisplayTitle(true);
     $title = $title ? $title : '';
     // Strip HTML from note titles
     if ($this->isNote()) {
         // Clean and strip HTML, giving us an HTML-encoded plaintext string
         $title = strip_tags($GLOBALS['HTMLPurifier']->purify($title));
         // Unencode plaintext string
         $title = html_entity_decode($title);
     }
     // Strip some characters
     $sortTitle = preg_replace("/^[\\[\\'\"]*(.*)[\\]\\'\"]*\$/", "\$1", $title);
     if ($sortTitle) {
         $doc->addField('titleSort', $sortTitle);
     }
     // Item data
     $fieldIDs = $this->getUsedFields();
     foreach ($fieldIDs as $fieldID) {
         $val = $this->getField($fieldID);
         if ($val == '') {
             continue;
         }
         $fieldName = Zotero_ItemFields::getName($fieldID);
         switch ($fieldName) {
             // As is
             case 'title':
                 $val = $title;
                 break;
                 // Date fields
             // Date fields
             case 'date':
                 // Add user part as text
                 $doc->addField($fieldName . "_t", Zotero_Date::multipartToStr($val));
                 // Add as proper date, if there is one
                 $sqlDate = Zotero_Date::multipartToSQL($val);
                 if (!$sqlDate || $sqlDate == '0000-00-00') {
                     continue 2;
                 }
                 $fieldName .= "_tdt";
                 $val = Zotero_Date::sqlToISO8601($sqlDate);
                 break;
             case 'accessDate':
                 if (!Zotero_Date::isSQLDateTime($val)) {
                     continue 2;
                 }
                 $fieldName .= "_tdt";
                 $val = Zotero_Date::sqlToISO8601($val);
                 break;
             default:
                 $fieldName .= "_t";
         }
         $doc->addField($fieldName, $val);
     }
     // Deleted item flag
     if ($this->getDeleted()) {
         $doc->addField('deleted', true);
     }
     if ($this->isNote() || $this->isAttachment()) {
         $sourceItemID = $this->getSource();
         if ($sourceItemID) {
             $sourceItem = Zotero_Items::get($this->libraryID, $sourceItemID);
             if (!$sourceItem) {
                 throw new Exception("Source item {$sourceItemID} not found");
             }
             $doc->addField('sourceItem', $sourceItem->key);
         }
     }
     // Group modification info
     $createdByUserID = null;
     $lastModifiedByUserID = null;
     switch (Zotero_Libraries::getType($this->libraryID)) {
         case 'group':
             $createdByUserID = $this->createdByUserID;
             $lastModifiedByUserID = $this->lastModifiedByUserID;
             break;
     }
     if ($createdByUserID) {
         $doc->addField('createdByUserID', $createdByUserID);
     }
     if ($lastModifiedByUserID) {
         $doc->addField('lastModifiedByUserID', $lastModifiedByUserID);
     }
     // Note
     if ($this->isNote()) {
         $doc->addField('note', $this->getNote());
     }
     if ($this->isAttachment()) {
         $doc->addField('linkMode', $this->attachmentLinkMode);
         $doc->addField('mimeType', $this->attachmentMIMEType);
         if ($this->attachmentCharset) {
             $doc->addField('charset', $this->attachmentCharset);
         }
         // TODO: get from a constant
         if ($this->attachmentLinkMode != 3) {
             $doc->addField('path', $this->attachmentPath);
         }
         $note = $this->getNote();
         if ($note) {
             $doc->addField('note', $note);
         }
     }
     // Creators
     $creators = $this->getCreators();
     if ($creators) {
         foreach ($creators as $index => $creator) {
             $c = $creator['ref'];
             $doc->addField('creatorKey', $c->key);
             if ($c->fieldMode == 0) {
                 $doc->addField('creatorFirstName', $c->firstName);
             }
             $doc->addField('creatorLastName', $c->lastName);
             $doc->addField('creatorType', Zotero_CreatorTypes::getName($creator['creatorTypeID']));
             $doc->addField('creatorIndex', $index);
         }
     }
     // Tags
     $tags = $this->getTags();
     if ($tags) {
         foreach ($tags as $tag) {
             $doc->addField('tagKey', $tag->key);
             $doc->addField('tag', $tag->name);
             $doc->addField('tagType', $tag->type);
         }
     }
     // Related items
     /*$related = $this->relatedItems;
     		if ($related) {
     			$related = Zotero_Items::get($this->libraryID, $related);
     			$keys = array();
     			foreach ($related as $item) {
     				$doc->addField('relatedItem', $item->key);
     			}
     		}*/
     return $doc;
 }
示例#28
0
 public function save($commit = false)
 {
     $doc = new SolrInputDocument();
     foreach ($this->doc_fields as $field => $value) {
         if (!is_array($value)) {
             $doc->addField($field, $value);
         } else {
             foreach ($value as $v) {
                 $doc->addField($field, $v, 1);
             }
         }
     }
     $client = MPF_Solr_Factory::get_instance()->get_client($this->mapping['instance']);
     try {
         $response = $client->addDocument($doc);
     } catch (Exception $e) {
         return false;
     }
     if (!$response instanceof SolrUpdateResponse || $response->getHttpStatus() != 200) {
         //TODO debug info
         return false;
     }
     if ($commit == true) {
         $response = $client->commit();
     }
     return true;
 }
<?php

include "bootstrap.php";
$options = array('hostname' => SOLR_SERVER_HOSTNAME, 'login' => SOLR_SERVER_USERNAME, 'password' => SOLR_SERVER_PASSWORD, 'port' => SOLR_SERVER_PORT, 'path' => SOLR_SERVER_PATH);
$client = new SolrClient($options);
$doc = new SolrInputDocument();
$doc->addField('id', 334455);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');
// No need to call commit() because $commitWithin is passed, so Solr Server will auto commit within 10 seconds
$updateResponse = $client->addDocument($doc, false, 10000);
print_r($updateResponse->getResponse());
示例#30
0
 private static function add($document)
 {
     $doc = new SolrInputDocument();
     foreach ($document as $name => $value) {
         if (is_array($value)) {
             foreach ($value as $element) {
                 $doc->addField($name, $element);
             }
         } else {
             $doc->addField($name, $value);
         }
     }
     return $doc;
 }