示例#1
0
文件: Solr.php 项目: emilymwang8/ibug
 /**
  * add a document
  * @param array
  */
 public function addDocument($doc)
 {
     $document = new Apache_Solr_Document();
     foreach ($doc as $key => $val) {
         if (is_array($val)) {
             foreach ($val as $_val) {
                 $document->addField($key, $_val);
             }
         } else {
             $document->addField($key, $val);
         }
     }
     $this->service->addDocument($document);
 }
示例#2
0
 function set_search($data = array())
 {
     $CommonConfig = (require dirname(dirname(dirname(dirname(__FILE__)))) . "/CommonConfig/commonConfig.php");
     if (!class_exists('Apache_Solr_Service', false)) {
         require dirname(dirname(dirname(__FILE__))) . '/lib/Third/SolrPhpClient/Apache/Solr/Service.php';
     }
     $document = new Apache_Solr_Document();
     foreach ($data as $key => $val) {
         $document->{$key} = $val;
     }
     if (is_array($CommonConfig['SOLR_DOMAIN'])) {
         foreach ($CommonConfig['SOLR_DOMAIN'] as $solrDomain) {
             $solr = new Apache_Solr_Service($solrDomain, $CommonConfig['SOLR_PORT'], '/solr');
             $set_search = $solr->addDocument($document);
             //$solr->commit(); //commit to see the deletes and the document
             file_get_contents('http://' . $solrDomain . ':' . $CommonConfig['SOLR_PORT'] . '/solr/update/json?commit=true');
         }
         return $set_search;
     } else {
         $solr = new Apache_Solr_Service($CommonConfig['SOLR_DOMAIN'], $CommonConfig['SOLR_PORT'], '/solr');
         $set_search = $solr->addDocument($document);
         //$solr->commit(); //commit to see the deletes and the document
         file_get_contents('http://' . $CommonConfig['SOLR_DOMAIN'] . ':' . $CommonConfig['SOLR_PORT'] . '/solr/update/json?commit=true');
         return $set_search;
     }
 }
示例#3
0
/**
 * Executes the Solr query and returns the JSON response.
 */
function solr_proxy_main()
{
    if (isset($_GET['solrUrl'])) {
        $spliturl = parse_url($_GET['solrUrl']);
        $host = $spliturl['host'] == 'solr.smk.dk' ? 'solr-02.smk.dk' : $spliturl['host'];
        $port = $spliturl['host'] == 'solr.smk.dk' ? '8080' : $spliturl['port'];
        $core_log = $spliturl['host'] == 'solr.smk.dk' ? 'prod_search_log' : 'preprod_search_log';
        $path = explode("/", trim($spliturl['path'], '/'));
        $core = array_pop($path);
        $path = implode("/", $path);
        $solr = new Apache_Solr_Service($host, $port, '/' . $path . '/' . $core . '/');
        //$solr = new Apache_Solr_Service('csdev-seb', 8180, '/solr-example/preprod_all_dk/');
        //var_dump($solr);
        $solr_search_log = new Apache_Solr_Service($host, $port, '/' . $path . '/' . $core_log . '/');
        //$solr_search_log = new Apache_Solr_Service('solr-02.smk.dk', 8080, '/solr/prod_search_log/');
        //var_dump($solr_search_log);
        $document = new Apache_Solr_Document();
        $q_default = "-(id_s:(*/*) AND category:collections) -(id_s:(*verso) AND category:collections)";
        $fq_tag = "tag";
        $fq_prev = array();
        $q_prev = array();
        $picture_url = '';
        $numfound = 0;
        if (isset($_GET['prev_query'])) {
            $params = array();
            $params['q'] = '*:*';
            $keys = '';
            $core = '';
            //error_log($_GET['query']);
            // The names of Solr parameters that may be specified multiple times.
            $multivalue_keys = array('bf', 'bq', 'facet.date', 'facet.date.other', 'facet.field', 'facet.query', 'fq', 'pf', 'qf');
            $pairs = explode('&', $_GET['prev_query']);
            foreach ($pairs as $pair) {
                if ($pair != '') {
                    list($key, $value) = explode('=', $pair, 2);
                    $value = urldecode($value);
                    if (in_array($key, $multivalue_keys)) {
                        $params[$key][] = $value;
                    } elseif ($key == 'q') {
                        //error_log($value);
                        $keys = $value;
                    } elseif ($key == 'core') {
                        $core = "{$value}/";
                    } else {
                        $params[$key] = $value;
                    }
                }
            }
            // 		try {
            // 			$response = $solr->search($keys, $params['start'], $params['rows'], $params);
            // 		}
            // 		catch (Exception $e) {
            // 			die($e->__toString());
            // 		}
            //error_log($response->getRawResponse());
            //print $response->getRawResponse();
            /*ררררררררר*/
            $fq = array();
            $q = array();
            // proceed only if 'start' param was null ('start' is set when the user uses pagination in website, and we want to avoid duplication on search string)
            //if(!isset($params['start'])){
            // process q
            if ($keys != '') {
                $q = explode(",", $keys);
                // remove default 'q' value
                if (($key = array_search($q_default, $q)) !== false) {
                    unset($q[$key]);
                }
                array_filter($q);
            }
            // process fq
            if (isset($params['fq'])) {
                $fq = $params['fq'];
                // remove 'tag' facet
                $matches = array_filter($fq, function ($var) use($fq_tag) {
                    return preg_match("/\\b{$fq_tag}\\b/i", $var);
                });
                foreach ($matches as $key => $value) {
                    unset($fq[$key]);
                }
                array_filter($fq);
            }
            if (count($q) + count($fq) > 0) {
                $fq_prev = $fq;
                $q_prev = $q;
            }
            //}
        }
        if (isset($_GET['query'])) {
            $params = array();
            $params['q'] = '*:*';
            $keys = '';
            $core = '';
            //error_log($_GET['query']);
            // The names of Solr parameters that may be specified multiple times.
            $multivalue_keys = array('bf', 'bq', 'facet.date', 'facet.date.other', 'facet.field', 'facet.query', 'fq', 'pf', 'qf');
            $pairs = explode('&', $_GET['query']);
            foreach ($pairs as $pair) {
                list($key, $value) = explode('=', $pair, 2);
                $value = urldecode($value);
                if (in_array($key, $multivalue_keys)) {
                    $params[$key][] = $value;
                } elseif ($key == 'q') {
                    //error_log($value);
                    $keys = $value;
                } elseif ($key == 'core') {
                    $core = "{$value}/";
                } else {
                    $params[$key] = $value;
                }
            }
            try {
                $response = $solr->search($keys, $params['start'], $params['rows'], $params);
                //var_dump($response);
                $numfound = $response->response->numFound;
                foreach ($response->response->docs as $doc) {
                    foreach ($doc as $field => $value) {
                        if ($field == "medium_image_url") {
                            $picture_url = $value;
                            break;
                        }
                    }
                }
            } catch (Exception $e) {
                die($e->__toString());
            }
            //error_log($response->getRawResponse());
            //print $response->getRawResponse();
            /*ררררררררר*/
            $fq = array();
            $q = array();
            // proceed only if 'start' param was null ('start' is set when the user uses pagination in website, and we want to avoid duplication on search string)
            if (!isset($params['start'])) {
                // process q
                if ($keys != '') {
                    $q = explode(",", $keys);
                    // remove default 'q' value
                    if (($key = array_search($q_default, $q)) !== false) {
                        unset($q[$key]);
                    }
                    array_filter($q);
                }
                // process fq
                if (isset($params['fq'])) {
                    $fq = $params['fq'];
                    // remove 'tag' facet
                    $matches = array_filter($fq, function ($var) use($fq_tag) {
                        return preg_match("/\\b{$fq_tag}\\b/i", $var);
                    });
                    foreach ($matches as $key => $value) {
                        unset($fq[$key]);
                    }
                    array_filter($fq);
                }
                if (count($q) + count($fq) > 0) {
                    //$solr_search_log = new Apache_Solr_Service('csdev-seb', 8180, '/solr-example/dev_search_log/' . $core);
                    //$document = new Apache_Solr_Document();
                    $document->id = uniqid();
                    //or something else suitably unique
                    $document->q = $q;
                    $document->facet = $fq;
                    $document->ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['REMOTE_ADDR'] + "-" + $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
                    $document->last_update = gmdate('Y-m-d\\TH:i:s\\Z', strtotime("now"));
                    $document->numfound = $numfound;
                    // user called for detailed view of an artwork?
                    $artwork = "id_s";
                    $matches = array_filter($q, function ($var) use($artwork) {
                        return preg_match("/\\b{$artwork}\\b/i", $var);
                    });
                    if (count($matches) > 0) {
                        if (count($q_prev) > 0) {
                            $document->prev_q = $q_prev;
                        }
                        if (count($fq_prev) > 0) {
                            $document->prev_facet = $fq_prev;
                        }
                        if ($picture_url != '') {
                            $document->picture_url = $picture_url;
                        }
                    }
                    $solr_search_log->addDocument($document);
                    //if you're going to be adding documents in bulk using addDocuments with an array of documents is faster
                    //$solr_search_log->deleteByQuery('*:*');
                    $solr_search_log->commit();
                    //echo 'ok';
                }
            }
            /*ררררררררררר*/
            echo $_GET['callback'] . '(' . $response->getRawResponse() . ')';
        }
    }
}
示例#4
0
文件: api.php 项目: pezzabros/Loki
    $xml = simplexml_load_string($text);
    add_log('indexdocument:solrIndexing', "Found " . @count(@$xml->page) . " pages to index");
    //echo "Processamento XML in pagine:<br>";
    foreach ($xml->page as $page) {
        //echo "<br><br>Processamento pagina<br>";
        //echo "<strong>Numero</strong>: ".$page->attributes()->number."<br>";
        //echo "<strong>Testo</strong>: $page<br>";
        $doc = new Apache_Solr_Document();
        $id = $sourceId . "_PAGE_" . $page->attributes()->number;
        $doc->id = $id;
        $doc->sourceId = $sourceId;
        $doc->type = "PAGE";
        $doc->page = (int) $page->attributes()->number;
        $doc->text_extracted = $page;
        add_log('indexdocument:solrIndexing', "Inserimento pagina " . (int) $page->attributes()->number . " text: " . $page);
        $result_insert = $solr->addDocument($doc);
        add_log('indexdocument:solrIndexing', "Ris. inserimento status:" . $result_insert->getHttpStatus() . " <br>Ris. inserimento message:" . $result_insert->getHttpStatusMessage());
        //$array_insert_docs[] = $doc;
        //if you're going to be adding documents in bulk using addDocuments
    }
    //$solr->addDocuments($array_insert_docs);
    $solr->commit();
    //commit to see the deletes and the document
    add_log('indexdocument:solrIndexing', "Completed ");
    return $request->result(getProcessStarted($x_processId, 'Finished'));
}, 'getresults' => function ($request) {
    global $conn;
    if (!is_array($request->params)) {
        return $request->error(-32602);
    }
    if (!isset($request->params['processId'])) {
示例#5
0
 function set_search($data = array())
 {
     $this->onlineConfig = (require TIPASK_ROOT . '/onlineConfig.php');
     if (!class_exists('Apache_Solr_Service', false)) {
         require TIPASK_ROOT . '/api/SolrPhpClient/Apache/Solr/Service.php';
     }
     $document = new Apache_Solr_Document();
     foreach ($data as $key => $val) {
         $document->{$key} = $val;
     }
     if (is_array($this->onlineConfig['SOLR_DOMAIN'])) {
         foreach ($this->onlineConfig['SOLR_DOMAIN'] as $solrDomain) {
             $solr = new Apache_Solr_Service($solrDomain, $this->onlineConfig['SOLR_PORT'], '/solr');
             $solr->addDocument($document);
             //$solr->commit(); //commit to see the deletes and the document
             file_get_contents('http://' . $solrDomain . ':' . $this->onlineConfig['SOLR_PORT'] . '/solr/update/json?commit=true');
         }
     } else {
         $solr = new Apache_Solr_Service($this->onlineConfig['SOLR_DOMAIN'], $this->onlineConfig['SOLR_PORT'], '/solr');
         $solr->addDocument($document);
         //$solr->commit(); //commit to see the deletes and the document
         file_get_contents('http://' . $this->onlineConfig['SOLR_DOMAIN'] . ':' . $this->onlineConfig['SOLR_PORT'] . '/solr/update/json?commit=true');
     }
 }