ElasticSearchController.class.php

<?php

namespace Home\Controller;

use Think\Controller;
use Elasticsearch\ClientBuilder;


class ElasticSearchController extends Controller
{

    public function _initialize()
    {
        require 'vendor/autoload.php';
        $hosts = array('127.0.0.1:9200');
        $this->client = ClientBuilder::create()// Instantiate a new ClientBuilder
        ->setHosts($hosts)// Set the hosts
        ->build();              // Build the client object
    }

    //没有映射时,会自动创建
    /*public function TestcreateDoc()
    {
        $params = array(
            'index' => 'my_index',
            'type' => 'my_type',
            'id' => 'my_id2',
            'body' => array('testField' => 'abc2')
        );

        $retDoc = $this->client->index($params);
        print_r($retDoc);
    }*/



    /**
     * 获取单条数据
     */
    public function TestGetDocById()
    {
        $params = array(
            'index' => 'case_all',
            'type' => 'jdbc',
            'id' => 1348254,
        );

        $retDoc = $this->client->get($params);
        print_r($retDoc);
    }

    public function testSearch()
    {
        $params = array(
            'index' => 'case_all',
            'type' => 'jdbc',
            'body' => array(
                'query' => array(
                    'match' => array('court_opinion_txt' => '本院认为')
                )
            )
        );

        $retDoc = $this->client->search($params);
        print_r($retDoc);
    }

    public function deleteTestDoc()
    {
        $params = array(
            'index' => 'my_index',
            'type' => 'my_type',
        );

        $response = $this->client->delete($params);
        print_r($response);
    }

    /*****************************************************************************************************/


    public function SearchCase()
    {
        $params = array(
            'index' => 'case_all',
            'type' => 'jdbc',
            'body' => array(
                '_source' => array(
                    'include' => array("title", "case_no", "city_id","year_id")
                ),
                'query' => array(
                    'match' => array('title' => '大学')
                )
            ),
            'from' => '0',  // 分页
            'size' => '10',  // 每页数量
        );

        $retDoc = $this->client->search($params);
        echo json_encode($retDoc);
    }

    /**
     * 查询搜索结果
     */
    public function SearchCase2()
    {
        $params = array(
            'index' => 'case_all',
            'type' => 'case',
            'body' => array(
                '_source' => array(
                    'include' => array("title", "case_no", "city_id","year_id")
                ),
                'query' => array(
                    'bool' => array(
                        'must' => array(
                            'multi_match' => array(
                                "fields" => array("title"),
                                'query' => "第号",
//                                'type' => 'most_fields',
                                'tie_breaker' => 0.3,
                                "minimum_should_match" => "30%",
                            ),
                        ),
                        'must_not' => array(),
                        'should' => array(),
                    )
                ),
            ),

            'from' => '0',  // 分页
            'size' => '10',  // 每页数量
        );

        $retDoc = $this->client->search($params);
        echo json_encode($retDoc);
    }


    /**
     * 结果中搜索
     */
    public function SearchCaseAndFilter()
    {
        $params = array(
            'index' => 'case_all',
            'type' => 'jdbc',
            'body' => array(
                '_source' => array(
                    'include' => array("title", "case_no", "city_id","year_id")
                ),
                'query' => array(
                    'bool' => array(
                        'must' => array(
                            'multi_match' => array(
                                'query' => "京东 东岳老年公寓",
                                "fields" => array("title"),
                                "operator"=> "and",
                                'type' => 'most_fields',
                                'tie_breaker' => 0.3,
                                "minimum_should_match" => "30%",
                            ),
                        ),
                        'must_not' => array(),
                        'should' => array(),
                    )
                ),
            ),

            'from' => '0',  // 分页
            'size' => '10',  // 每页数量
        );

        $retDoc = $this->client->search($params);
        echo json_encode($retDoc);
    }

    /**
     * 对查询结果进行分组
     */
    public function SearchCaseGroup()
    {
        $params = array(
            'index' => 'case_all',
            'type' => 'case',
            'body' => array(
                '_source' => array(
                    'include' => array("judgement_id")
                ),
                'aggs' => array(
                    'judgement_id'=>array(
                        'terms'=>array(
                            'field'=>'judgement_id'
                        )
                    )
                )
            ),

            'from' => '0',  // 分页
            'size' => '10',  // 每页数量
        );

        $retDoc = $this->client->search($params);
        echo json_encode($retDoc);
    }

    public function SearchCaseGroup2()
    {
        $params = array(
            'index' => 'case_all',
            'type' => 'case',
            'body' => array(
                '_source' => array(
                    'include' => array("case_level")
                ),
                'aggs' => array(
                    'judgement_id'=>array(
                        'terms'=>array(
                            'field'=>'case_level'
                        )
                    )
                )
            ),

            'from' => '0',  // 分页
            'size' => '10',  // 每页数量
        );

        $retDoc = $this->client->search($params);
        echo json_encode($retDoc);
    }









    /*******************************************************************************/
   
}

 

<?php
namespace Home\Controller;
use Think\Controller;use Elasticsearch\ClientBuilder;

class ElasticSearchController extends Controller{
    public function _initialize()    {        require 'vendor/autoload.php';        $hosts = array('101.201.81.200:9200');        $this->client = ClientBuilder::create()// Instantiate a new ClientBuilder        ->setHosts($hosts)// Set the hosts        ->build();              // Build the client object    }
    //没有映射时,会自动创建    /*public function TestcreateDoc()    {        $params = array(            'index' => 'my_index',            'type' => 'my_type',            'id' => 'my_id2',            'body' => array('testField' => 'abc2')        );
        $retDoc = $this->client->index($params);        print_r($retDoc);    }*/


    /**     * 获取单条数据     */    public function TestGetDocById()    {        $params = array(            'index' => 'case_all',            'type' => 'jdbc',            'id' => 1348254,        );
        $retDoc = $this->client->get($params);        print_r($retDoc);    }
    public function testSearch()    {        $params = array(            'index' => 'case_all',            'type' => 'jdbc',            'body' => array(                'query' => array(                    'match' => array('court_opinion_txt' => '本院认为')                )            )        );
        $retDoc = $this->client->search($params);        print_r($retDoc);    }
    public function deleteTestDoc()    {        $params = array(            'index' => 'my_index',            'type' => 'my_type',        );
        $response = $this->client->delete($params);        print_r($response);    }
    /*****************************************************************************************************/

    public function SearchCase()    {        $params = array(            'index' => 'case_all',            'type' => 'jdbc',            'body' => array(                '_source' => array(                    'include' => array("title", "case_no", "city_id","year_id")                ),                'query' => array(                    'match' => array('title' => '大学')                )            ),            'from' => '0',  // 分页            'size' => '10',  // 每页数量        );
        $retDoc = $this->client->search($params);        echo json_encode($retDoc);    }
    /**     * 查询搜索结果     */    public function SearchCase2()    {        $params = array(            'index' => 'case_all',            'type' => 'case',            'body' => array(                '_source' => array(                    'include' => array("title", "case_no", "city_id","year_id")                ),                'query' => array(                    'bool' => array(                        'must' => array(                            'multi_match' => array(                                "fields" => array("title"),                                'query' => "第号",//                                'type' => 'most_fields',                                'tie_breaker' => 0.3,                                "minimum_should_match" => "30%",                            ),                        ),                        'must_not' => array(),                        'should' => array(),                    )                ),            ),
            'from' => '0',  // 分页            'size' => '10',  // 每页数量        );
        $retDoc = $this->client->search($params);        echo json_encode($retDoc);    }

    /**     * 结果中搜索     */    public function SearchCaseAndFilter()    {        $params = array(            'index' => 'case_all',            'type' => 'jdbc',            'body' => array(                '_source' => array(                    'include' => array("title", "case_no", "city_id","year_id")                ),                'query' => array(                    'bool' => array(                        'must' => array(                            'multi_match' => array(                                'query' => "京东 东岳老年公寓",                                "fields" => array("title"),                                "operator"=> "and",                                'type' => 'most_fields',                                'tie_breaker' => 0.3,                                "minimum_should_match" => "30%",                            ),                        ),                        'must_not' => array(),                        'should' => array(),                    )                ),            ),
            'from' => '0',  // 分页            'size' => '10',  // 每页数量        );
        $retDoc = $this->client->search($params);        echo json_encode($retDoc);    }
    /**     * 对查询结果进行分组     */    public function SearchCaseGroup()    {        $params = array(            'index' => 'case_all',            'type' => 'case',            'body' => array(                '_source' => array(                    'include' => array("judgement_id")                ),                'aggs' => array(                    'judgement_id'=>array(                        'terms'=>array(                            'field'=>'judgement_id'                        )                    )                )            ),
            'from' => '0',  // 分页            'size' => '10',  // 每页数量        );
        $retDoc = $this->client->search($params);        echo json_encode($retDoc);    }
    public function SearchCaseGroup2()    {        $params = array(            'index' => 'case_all',            'type' => 'case',            'body' => array(                '_source' => array(                    'include' => array("case_level")                ),                'aggs' => array(                    'judgement_id'=>array(                        'terms'=>array(                            'field'=>'case_level'                        )                    )                )            ),
            'from' => '0',  // 分页            'size' => '10',  // 每页数量        );
        $retDoc = $this->client->search($params);        echo json_encode($retDoc);    }








    /*******************************************************************************/    private function getChange($type){        switch ($type) {            case '1' :                $magicType = 'qbnr';                break;            case '2' :                $magicType = 'byrw';                break;            case '3' :                $magicType = 'cpjg';                break;            case '4' :                $magicType = 'ygsc';                break;            case '5' :                $magicType = 'bfgd';                break;            case 'a' :                $magicType = 'dsr';                break;            case 'g' :                $magicType = 'cpyz';                break;            case 'h' :                $magicType = 'zyjd';                break;            case 'd' :                $magicType = 'title';                break;            case 'i' :                $magicType = 'sljg';                break;            case 'f' :                $magicType = 'spry';                break;            case 'm' :                $magicType = 'bycm';                break;                return $type;
        }    }    /**     * 结果中搜索     */    public function searchJson()    {        $page = I('post.page', 0);        $page = $page - 1;        $searchNum = I('post.searchNum', 20);        $nowReason = I('post.nowReason', 0);        $sortType = I('post.sortType', 'caseWeight');//        $keyword = I('post.keyword');        $TypeKey = I('post.TypeKey');

        /*$typekey_arr=explode($TypeKey,":");        foreach ( $typekey_arr as $eachTK ) {            $keyword_arr = explode ( ' ', $eachTK );            foreach ( $keyword_arr as $keyword ) {
            }
        }*///        $this->getChange();        $typekey_arr="莱姆顿 学院";


        $sub_param=array(            'qbnr'=>array(                'multi_match' => array(                    "query" => $typekey_arr,                    "fields" => array(                        "title",                        "court_opinion_txt",                        "court_findout_r1",                        "focus",                        "accused_say",                        "court_findout_r2",                        "result_txt",                        "case_no",                        "opponents",                        "others_txt",                        "judges",                        "sljg",                        "plain_say"                    ),                    "operator"=> "and",                    'type' => 'most_fields',                    'tie_breaker' => 0.3,                    "minimum_should_match" => "30%",                ),            )        );


        $params = array(            'index' => 'case_all',            'type' => 'jdbc',            'body' => array(                '_source' => array(                    'include' => array(                        "title",                    )                ),                'query' => array(                    'bool' => array(                        'must' => array(                            $sub_param['qbnr'],                            year_id                        ),                        'must_not' => array(),                        'should' => array(),                    )                ),            ),
            'from' => $page,  // 分页            'size' => $searchNum,  // 每页数量        );
        $retDoc = $this->client->search($params);        echo json_encode($retDoc);    }}

posted on 2018-12-24 17:07  ziyi_ang  阅读(214)  评论(0编辑  收藏  举报

导航