Home Backend Development PHP Tutorial PHP's solr operation classes and demo

PHP's solr operation classes and demo

Mar 24, 2017 pm 01:25 PM

php’s solr operation class and demo

1. Solr class

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

'127.0.0.1','port' => '8080');

    /**

     * 设置solr库选择

     * @param $core string 库名称

     */

    public static function setCore($core){

        if($core) self::$options['path']='solr/'.$core;

    }

  

    /**

    * 增加solr索引

    * @param $fieldArr 索引字段及值

    * @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;

    }

  

    /**

    * 删除索引

    * @param $id 主键id id可以为数组形式,应用于多选的情况

    * @return bool true

    */

    public static function delDocument($ids){

        $client = new SolrClient(self::$options);

        if(is_array($ids))

            $client->deleteByIds($ids);

        else

            $client->deleteById($ids);

        $client->commit();

        return true;

    }

  

    /**

    * 查询数据

    * @param $qwhere     关键字

     * @param $fqwhere 附加条件,根据范围检索,适用于数值型

    * @param $getField    查询字段

     * @param $sort 排序 array('duration'=>'asc')  asc:升序,desc:降序

    * @param $pageindex   查询页数

    * @param $pagesize    每页显示条数

    */

    public static function selectQuery($qwhere=array(),$fqwhere=array(),$getField=array(),$sort=array(),$pageindex=1,$pagesize=20){

        $client = new SolrClient(self::$options);

        $query = new SolrQuery();

        $sel = '';

        foreach($qwhere as $k => $v){

//            $sel .= ' +'.$k.':'.$v;

            $sel = "{$k} : \"{$v}\"";

        }

        $query->setQuery($sel);

        //关键字检索

  

        //附加条件,根据范围检索,适用于数值型

        if($fqwhere){

            $query->setFacet(true);

            foreach($fqwhere as $k => $v)

                $query->addFacetQuery($v);

            //$query->addFacetQuery('price:[* TO 500]');

        }

  

        //查询字段

        if($getField){

            foreach($getField as $key => $val)

                $query->addField($val);

        }

        //排序

        if($sort){

            foreach($sort as $k => $v){

                if($v == 'asc')

                    $query->addSortField($k,SolrQuery::ORDER_ASC);

                elseif($v == 'desc')

                    $query->addSortField($k,SolrQuery::ORDER_DESC);

            }

        }

        //分页

        $query->setStart(self::getPageIndex($pageindex,$pagesize));

        $query->setRows($pagesize);

          

        $query_response = $client->query($query);

        $response = $query_response->getResponse();

        return $response;

    }

  

    /**

    * 分页数据处理

    */

    private static function getPageIndex($pageindex,$pagesize){

        if($pageindex<=1)

            $pageindex = 0;

        else

            $pageindex = ($pageindex-1)*$pagesize;

        return $pageindex;

    }

  

}

Copy after login

2. Operation demo

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

"wang jing jie",

);

print_r(phpSolr::selectQuery($qwhere));

  

//添加

$fieldArr = array(

    "id" => 15,

    "username" => "si sheng chao",

    "usertype" => 1,

    "last_update_time" => "2016-01-05T03:35:13Z",

);

phpSolr::addDocument($fieldArr);

  

//删除

//phpsolr::delDocument(15);

Copy after login

The above introduces the php’s solr operation class and demo, including aspects Content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!

Related articles:

Install php-solr extension

Search solution How to install and configure solr+php?

Integrated PHP application and Solr search engine

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What does demo mean? What does demo mean? Feb 12, 2024 pm 09:12 PM

The word demo is no longer unfamiliar to friends who like to sing, but many users who have never been exposed to it are curious about what demo means. Now let’s take a look at the meaning of the demo brought by the editor. What does demo mean? Answer: Demo tape. 1. The pronunciation of demo is ['deməʊ] in English and ['demoʊ] in America. 2. Demo is the abbreviation of "demonstration", which generally refers to the preliminary effect of listening to a song before it is officially recorded. 3. Demo is used as a noun to refer to sample tapes and sample records. The meaning of verb is trial (especially software), demonstration and demonstration;

How to use demo of python random library How to use demo of python random library May 05, 2023 pm 08:13 PM

Simple use of pythonrandom library demo When we need to generate random numbers or randomly select elements from a sequence, we can use Python's built-in random library. The following is an annotated example that demonstrates how to use the random library: #Import random library importrandom #Generate a random decimal between 0 and 1 random_float=random.random()print(random_float)#Generate a random decimal within the specified range Random integer (including endpoints) random_int=random.randint(1,10)print(random_int)#

Full text search using Solr in PHP Full text search using Solr in PHP Jun 20, 2023 am 09:12 AM

Solr is a Lucene-based search engine that can be used to implement full-text search. Using Solr in PHP for full-text search can help us quickly query relevant data through keywords and improve the accuracy and reliability of search results. This article will introduce how to use Solr in PHP for full-text search. 1. Installation and configuration of Solr First, we need to install Solr and the Solr extension of PHP on the server. For Solr installation steps, please refer to Solr’s official documentation.

Using Solr for full-text search in Java API development Using Solr for full-text search in Java API development Jun 18, 2023 am 10:41 AM

With the development of the Internet and the explosive growth of information, the amount of information we can now access and obtain is very large. Whether it is information obtained from web pages, documents, or daily life, it requires an efficient way to process and manage it. Full-text search is a very efficient and commonly used method. It can locate and extract the information we need through keywords or phrases, and Solr is a tool that is very suitable for full-text search development. This article will introduce the basic concepts of Solr and how to use it in Java API development.

Realme's 300W fast-charging demo sees battery juiced up from 0 to 17% in only 35 seconds Realme's 300W fast-charging demo sees battery juiced up from 0 to 17% in only 35 seconds Aug 10, 2024 pm 10:14 PM

Realme turned heads recently when it announced 300W fast-charging for its supposedGT7 Pro(as detailed in previous leaks). It's not the first brand to make such an announcement, though - Xiaomi has also been teasing its 300W charging for a while now.

Search and query using Solr in Beego Search and query using Solr in Beego Jun 23, 2023 am 10:54 AM

Beego is a fast Go language web framework, and Solr is a Lucene-based search and query server. Using the two together can provide efficient search capabilities to web applications. This article will introduce how to use Solr for search and query in Beego. Step 1: Install Solr Before you start using Solr, you need to install Solr. Solr can be downloaded and downloaded from the official website (https://lucene.apache.org/solr/)

SpringBoot basic web development demo method SpringBoot basic web development demo method Jun 02, 2023 am 10:22 AM

1. Import Lombok's dependency org.projectlomboklombok1.18.62 in pom.xml in the created springboot project. Install the Lombok plug-in 3. Create a package of the entity class at the same level as the main startup class, create the entity class in the package, and Use Lombokpackagecom.hxy.bean;importcom.fasterxml.jackson.annotation.JsonFormat;importlombok.AllArgsConstructor;importlombok.Data;importlom on entity classes

How to reproduce the RCE vulnerability in Apache Solr JMX service How to reproduce the RCE vulnerability in Apache Solr JMX service May 14, 2023 pm 02:25 PM

0x00 Introduction Solr is an independent enterprise-level search application server that provides an API interface similar to Web-service. Users can submit XML files in a certain format to the search engine server through http requests to generate indexes; they can also make search requests through HttpGet operations and get returned results in XML format. The vulnerability stems from the security risk in the ENABLE_REMOTE_JMX_OPTS configuration option in the default configuration file solr.in.sh. ENABLE_REMOTE_JMX_OPTS= exists in the built-in configuration file solr.in.sh of versions 8.1.1 and 8.2.0 of ApacheSolr.

See all articles