Elasticsearch study notes - installation, initial use

foreword

  I have long admired the name of elasticsearch. In recent years, the search functions of large websites such as Facebook and baidu have begun to use elasticsearch, which shows its excellent performance in processing big data and high concurrent search. Many other websites have also begun to use elasticsearch as an important option in the search function, which can be seen from the skill requirements of the personnel in the job postings. Although elasticsearch is developed based on java, it provides the form of Restful interface for other programs to call, which is very convenient, and also has corresponding extension support for other languages ​​such as php and python. So it is necessary to learn elasticsearch.

Install

  The author installed it in the centos6.8 environment, and installed elasticsearch-6.2.4. Reference: Installation and use of the PHP_elasticsearch search engine  , but a large number of errors occur after the installation, and you need to search for the corresponding solution, which is troublesome, but it can be solved smoothly. It can be seen that there are always many problems when a new program comes out, and the installation and use of php is relatively smooth.

  After that, I installed elasticsearch-head, a plugin for graphical query management elasticsearch. The installation process is not very smooth, and it is solved by searching.

initial use

  The first thing to understand is that the correspondence between some nouns in elasticsearch and traditional relational databases (such as mysql) is actually a one-to-one correspondence. If you change the name, you need to re-remember it.

Relational DB -> Databases(数据库) -> Tables(表) -> Rows(行) -> Columns(列)
Elasticsearch -> Indices(索引)   -> Types(类型)  -> Documents(文档) -> Fields(字段)

  I am used to the php language, so I use the elasticsearch-php6.0 extension to operate elastcsearch. Its official documentation address , very useful. Pay attention to the installed elasticsearch-php version. If the version is too low, you may not be able to operate elasticsearch.

  After the installation is complete, you can operate it. The following is a simple example that reads the data in the database, dumps it to elasticsearch, and can query the data, and can also query the data fuzzy (like query in sql). Note that you need to sleep after the transfer, otherwise the result cannot be queried immediately.

<?php
/*
 * Simple use of elasticsearch instances to create indexes and query data.
 */
require_once('vendor/autoload.php');  
use Elasticsearch\ClientBuilder;  
  
  
function get_conn(){  
    $host = '127.0.0.1';  
    $dbname = 'test';  
    $user = 'root';  
    $passwd = '123456';  
  
  
    $conn = new PDO("mysql:dbname=$dbname;host=$host",$user,$passwd, array(PDO::ATTR_PERSISTENT => true)); 
    $flag = $conn->exec('set names utf8');
    return $conn;  
}  
  
  
function create_index(){  
    //Elastic search php client  
  
    $client = Elasticsearch\ClientBuilder::create()->build();  
    $sql    = "SELECT * FROM tb_voteoption";  
    $conn   = get_conn();  
    $stmt   = $conn->query($sql);  
    $rtn    = $stmt->fetchAll();  
  
  
    //delete index which already created  
    $params = array();  
    $params['index'] = 'test';  
    $client->indices()->delete($params);  
      
    //create index 
    $rtnCount = count($rtn);  
    for($i=0;$i<$rtnCount;$i++){  
        $params = array();  
        $params['body'] = array(  
            'voteOptionID'       => $rtn[$i]['voteOptionID'],  
            'voteID'   => $rtn[$i]['voteID'],  
            'voteOptionName'    => $rtn[$i]['voteOptionName'],  
            'ticketNum' => $rtn[$i]['ticketNum']  
        );  
        $params['index'] = 'test';  
        $params['type']  = 'tb_voteoption';  
          
        //Document will be indexed to test/tb_voteoption/autogenerate_id       
    var_dump($params);   
        $client->index($params);  
    }  
    echo 'create index done!';  
}  
  
  
function search(){  
    //Elastic search php client  
    $client = Elasticsearch\ClientBuilder::create()->build();  
    $params = array();  
    $params['index'] = 'test';  
    $params['type'] = 'tb_voteoption';  
    $params['body']['query']['match']['voteOptionID'] = '54';   

    $rtn = $client->search($params);  
    var_dump($rtn);  
}  

/*类似于sql里面的like查询*/
function like_search(){  
    //Elastic search php client  
    $client = Elasticsearch\ClientBuilder::create()->build();  
    $params = array();  
    $params['index'] = 'test';  
    $params['type'] = 'tb_voteoption';  
    $params['body']['query']['wildcard'] = array('voteOptionName'=>'王*');

    $rtn = $client->search($params);  
    var_dump($rtn);  
}   
set_time_limit(0);  
error_reporting(E_ALL);
ini_set('display_errors','on');
 create_index();
sleep (5); // After the index is built, sleep is required, otherwise the data cannot be queried 
search();
like_search();
?>  
View Code

The database data used in it can be created with the following sql statement.

DROP TABLE IF EXISTS `tb_voteoption`;
CREATE TABLE `tb_voteoption` (
  `voteOptionID` int(11) NOT NULL AUTO_INCREMENT,
  `voteID` int(11) DEFAULT NULL,
  `voteOptionName` varchar(255) CHARACTER SET gb2312 DEFAULT NULL,
  `ticketNum` int(11) DEFAULT '0',
  PRIMARY KEY (`voteOptionID`),
  KEY `voteID` (`voteID`)
) ENGINE = InnoDB AUTO_INCREMENT = 55  DEFAULT CHARSET = utf8 COMMENT = ' Voting options table ' ;

INSERT INTO `tb_voteoption` VALUES ('51', '13', '朱芳宇', '0');
INSERT INTO `tb_voteoption` VALUES ('52', '13', '王治郅', '1');
INSERT INTO `tb_voteoption` VALUES ('53', '13' , ' Yao Ming ' , ' 0 ' );
 INSERT  INTO `tb_voteoption` VALUES ( ' 54 ' , ' 13 ' , ' Yi Jianlian ' , ' 0 ' );
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325057269&siteId=291194637