elastic search 多关键词查询实践(名称,日期,省份)

   // 查询文档 (分页,排序,权重,过滤)
    public function search_doc($keywords = "",$year='', $province_arr=[],  $from = 0, $size = 10, $order = ['id' => ['order' => 'asc']])
    {

        $must_arr = [];
        $filter_arr = [];
        $keys = '';
        if ($keywords) {
            /*获取单词的分词情况*/
            $analyzer = $this->client->indices()->analyze(
                [
                    'index' => $this->index,
                    'body' =>
                        [
                            'text' => $keywords,
                            'analyzer' => 'ik_max_word'
                        ]
                ]
            );
            if ($analyzer) {
                $tokens = $analyzer['tokens'];
                $keys = array_column($tokens, 'token', NULL);

                $keys_OR = implode('AND', $keys);

                $must_arr['query_string'] =   ['default_field' => 'name', 'query' => $keys_OR];
            }
        }

        if($year){
            $filter_arr[]['range'] =   ['create_date' => ['gte'=>$year."-01-01",'lte'=>$year."-12-31"]];
        }

        if(count($province_arr) > 0){
            $filter_arr[]['terms'] =   ['province.keyword' => $province_arr];
        }



        if ($keywords  || $year || count($province_arr) > 0) {

            /*----------------------------------------------------------*/

            /*----------------------------------------------------------*/

            $params = [
                'index' => $this->index,
                //  'type' => $this->type,
                'body' => [
                    'query' => [
//                          'match' =>
//                          [
//                              'name'=>$keywords
//                          ]
                        'bool' =>
                            [
                              //  'filter'=>[
                                    'must' =>$must_arr,
                                    'filter'=>$filter_arr,
                              //  ],
                            ]
                    ],

                    'sort' => [$order],
                    'from' => $from,
                    'size' => $size
                ]
            ];

            /*echo "<pre>";
            print_r(json_encode($params['body']['query']));
            exit();*/

            /*----------------------------------------------------------*/


        } // ------------------------------------------------------------------------


        else {
            $params = [
                'index' => $this->index,
                'body' => [
                    'sort' => [$order]
                    , 'from' => $from, 'size' => $size
                ]
            ];
        }

        try {

            $re = $this->client->search($params);

        } catch (\Exception $e) {
            echo '<pre>';
            print_r($e->getMessage());
            exit();
        }
        return ['re' => $re, 'keys' => $keys];
    }

  

上一篇:定位系统算法新突破


下一篇:linux – 如何通过SSH隧道路由备用网络接口?