【问题标题】:Elasticsearch Mapping Types in PHPPHP 中的 Elasticsearch 映射类型
【发布时间】:2019-10-18 20:48:03
【问题描述】:

我想使用官方PHP Client 连接到我们的elasticsearch Server (V7.0.0)。 elasticsearch 文档指出映射类型在版本 6.0.0 中为 removed

但在整个官方 PHP 客户端文档类型中仍然提到并且似乎是必要的(例如):

// Set the index and type
$params = [
    'index' => 'my_index',
    'type' => 'my_type2',
    'body' => [
        'my_type2' => [
            '_source' => [
                'enabled' => true
            ],
            'properties' => [
                'first_name' => [
                    'type' => 'keyword',
                    'analyzer' => 'standard'
                ],
                'age' => [
                    'type' => 'integer'
                ]
            ]
        ]
    ]
];

// Update the index mapping
$client->indices()->putMapping($params);

Source

谁能向我解释为什么在类型被删除后我仍然必须使用它们?

【问题讨论】:

    标签: php elasticsearch elasticsearch-php


    【解决方案1】:

    映射 types 已在 Elasticsearch 7.x 中删除。在以前的版本中(Elasticsearch 6.x 每个索引只需要指定一种类型),而在版本2.x - 5.6 中,每个索引可以使用多个类型。请检查您的文档版本。

    既然你有Elasticsearch 6.0.0,你需要指定映射类型,我不太确定这个API 甚至可以与更新版本的Elasticsearch 7.X 一起使用

    这是php API 的版本矩阵:

    Elasticsearch Version   Elasticsearch-PHP Branch
    >= 6.6, < 7.0                              6.7.x
    >= 6.0, < 6.6                              6.5.x
    >= 5.0, < 6.0                                5.0
    >= 2.0, < 5.0                         1.0 or 2.0
    >= 1.0, < 2.0                         1.0 or 2.0
    <= 0.90.x                                    0.4
    

    【讨论】:

    • 感谢您的回复。你说的对。客户端还不兼容Source
    【解决方案2】:

    Elasticsearch 7.15 使用 php 8.0 版本。 TO在localhost中映射Elasticsearch 7.15中的数据库表

    <?php
        require 'includes/connection.php';
        require 'vendor/autoload.php';
    
        class SearchElastic {
            
            private $elasticclient = null;
            
            public function __construct(){
                $db = new Connection();
                $this->con = $db->connect();
                //echo "<pre>";print_r($this->con); //die;
                
                $hosts = [
                    'http://localhost:9200'        // SSL to localhost
                ];
                
                $this->elasticclient = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
            }
            
            public function Mapping(){
                $params = ['index' => 'employees'];
                $response = $this->elasticclient->indices()->delete($params);
                $params = [
                    'index' => 'employees',
                    'body' => [             
                        'mappings' => [             
                            'properties' => [
                                'code' => [
                                    'type' => 'integer'
                                ],
                                'name' => [
                                    'type' => 'text'
                                ],
                                'created_at' => [
                                    'type' => 'text'
                                ],
                                'last_updated' => [
                                    'type' => 'text'
                                ],
                                'rank' => [
                                    'type' => 'integer'
                                ],
                            ]                   
                        ]
                    ]
                ];
                //echo "<pre>"; print_r($params); //die;
                $this->elasticclient->indices()->create($params);
    
            }
            
            public function Search($query){
                $client = $this->elasticclient;
                $result = array();
    
                $i = 0;
    
                $params = [
                    'index' => 'employees',
                    'type'  => '_doc',
                    'body'  => [
                        'query' => [
                            'match' => ['name' => $query],
                        ],
                        'size' => 9,
                        'sort' => [
                            ['rank' =>  'desc'],
                        ],
                    ],
                ];
                
                $query = $client->search($params);
                $hits  = sizeof($query['hits']['hits']);
                $hit   = $query['hits']['hits'];
                
                $result['searchfound'] = $hits;
                while ($i < $hits) {
    
                    $result['result'][$i] = $query['hits']['hits'][$i]['_source'];
    
                    $i++;
                }
    
                return  $result;
            }
            
            public function InsertData(){
                $this->Mapping();   
                $client = $this->elasticclient;
                $stmt = "SELECT * FROM `table_name` limit 1";
                $result = $this->con->query($stmt);
                
                $params = null;
                while ($row = $result->fetch_assoc()){  
                    $params['body'][] = array(
                      'index' => array(
                        '_index' => 'employees',
                        '_type' => '_doc',
                        '_id' => $row['id'],
                      ) ,
                    );
                    $params['body'][] = [
                        'id'         => $row['id'],
                        'name'         => $row['name'],
                        'created_at'   => $row['created_at'],
                        'last_updated' => $row['last_updated'],
                        'rank'         => $row['rank'],
                    ];
                }
                $responses = $client->bulk($params);
                //echo "<pre>"; print_r($responses); die;
                return true;
            }
            
            public function UpdateData(){      
                $client = $this->elasticclient;
                $stmt = "SELECT * FROM `table_name` limit 1, 1000";
                $result = $this->con->query($stmt);
                
                $params = null;
                while ($row = $result->fetch_assoc()){  
                    $params['body'][] = array(
                      'index' => array(
                        '_index' => 'employees',
                        '_type' => 'rules_employee',
                        '_type' => '_doc',
                        '_id' => $row['id'],
                      ) ,
                    );
                    $params['body'][] = [
                        'id'         => $row['id'],
                        'name'         => $row['name'],
                        'created_at'   => $row['created_at'],
                        'last_updated' => $row['last_updated'],
                        'rank'         => $row['rank'],
                    ];
                    
                }
                $responses = $client->bulk($params);
                //echo "<pre>"; print_r($responses); die;
                return true;
            }
        }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      相关资源
      最近更新 更多