PHP's SOLR operation class and demo

Source: Internet
Author: User
Tags solr
PHP's SOLR operation class and demo

First, the SOLR class

' 127.0.0.1 ', ' port ' = ' 8080 '); /** * Set SOLR library selection * @param $core String Library name */public static function Setcore ($core) {if ($core) Self::    $options [' Path ']= ' solr/'. $core; /** * Add SOLR Index * @param $fieldArr indexed fields and values * @return bool True */public static function Adddocument ($        Fieldarr=array ()) {$client = new solrclient (self:: $options);        $doc = new Solrinputdocument ();         foreach ($fieldArr as $k = + $v) {$doc->addfield ($k, $v);        } $client->adddocument ($doc);        $client->commit ();    return true; }/** * Delete index * @param $id primary key ID ID can be used as an array for multiple selections * @return bool True */public static function Deldocu        ment ($ids) {$client = new solrclient (self:: $options);        if (Is_array ($ids)) $client->deletebyids ($ids);        else $client->deletebyid ($ids);        $client->commit ();    return true;   }/** * Query data * @param $qwhere  Keywords * @param $fqwhere additional conditions, based on range retrieval, for numeric type * @param $getField query field * @param $sort Sort Array (' duration ' = ' A SC ') ASC: Ascending, desc: Descending * @param $pageindex query pages * @param $pagesize number of bars per page */public static function SELECTQ Uery ($qwhere =array (), $fqwhere =array (), $getField =array (), $sort =array (), $pageindex =1, $pagesize =20) {$client = new so        Lrclient (self:: $options);        $query = new Solrquery ();        $sel = ";            foreach ($qwhere as $k = + $v) {//$sel. = ' + '. $k. ': ' $v;        $sel = "{$k}: \" {$v}\ "";        } $query->setquery ($sel);            Keyword search//additional conditions, based on range retrieval, for numeric if ($fqwhere) {$query->setfacet (true);            foreach ($fqwhere as $k = $v) $query->addfacetquery ($v);        $query->addfacetquery (' price:[* to 500] '); }//Query field if ($getField) {foreach ($getField as $key + = $val) $query->addfield        ($val);     }   Sort if ($sort) {foreach ($sort as $k = = $v) {if ($v = = ' ASC ') $que                Ry->addsortfield ($k, SOLRQUERY::ORDER_ASC);            ElseIf ($v = = ' desc ') $query->addsortfield ($k, Solrquery::order_desc);        }}//Paging $query->setstart (Self::getpageindex ($pageindex, $pagesize));                 $query->setrows ($pagesize);        $query _response = $client->query ($query);        $response = $query _response->getresponse ();    return $response;            }/** * Paging data processing */private static function Getpageindex ($pageindex, $pagesize) {if ($pageindex <=1)        $pageindex = 0;        else $pageindex = ($pageindex-1) * $pagesize;    return $pageindex; } }

second, the operation of the demo

"Wang Jing Jie",);p Rint_r (Phpsolr::selectquery ($qwhere)); Add $fieldarr = Array (    "id" = +,    "username" = "Si sheng Chao",    "usertype" = 1,    "last_update _time "=" 2016-01-05t03:35:13z ",);p hpsolr::adddocument ($FIELDARR); Delete//PHPSOLR::d eldocument (15);

The above describes the PHP operation of the SOLR and demo, including the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

Related articles:

Installing the PHP-SOLR Extension

Search Scenario solr+php How do I install the configuration?

Integrated PHP application and SOLR search engine

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.