美文网首页
PHP+kibana+es用法

PHP+kibana+es用法

作者: 表弟_212 | 来源:发表于2020-08-22 13:22 被阅读0次

    列出所有索引

    列出所有索引(列出所有的数据库)
    
    GET /_cat/indices?v
    

    添加索引

    PUT /goods
    {
      "settings": {
          // 副本数
        "number_of_replicas": 1,
          // 分片数
        "number_of_shards": 5
      }
    }
    

    删除索引

    DELETE /goods
    

    修改文档

    POST /goods/_doc/1/_update
    {
     "doc": {"price":100 }
    }
    

    查询(搜索)

    GET /goods/_search
    
    // 查询是xiaomi9的
    GET /goods/_search
    {
      "query": {
        "match": {
          "title": "xiaomi9"
        }
      }
    }
    
    // or
    GET /goods/_search
    {
      "query": {
          'bool':{
              'should':[
                  {'match':{'field1':'aaa'}},
                  {'match':{'field2':'bbb'}}
              ]
          }
      }
    }
    
    // 排序
    GET /goods/_search
    {
      "query": {
        "match_all": {}
      },
      "sort": [
        {
          "_id": {
            "order": "desc"
          }
        }
      ]
    }
    
    
    ## 进行中文分词搜索
    
    PUT /goods
    {
      "mappings": {
        "_doc":{
          "properties":{
            "name":{
              "type":"text",
              "analyzer":"ik_max_word",
              "search_analyzer":"ik_max_word"
            },
            "desn":{
              "type":"text",
              "analyzer":"ik_max_word",
              "search_analyzer":"ik_max_word"
            }
          }
        }
      }
    }
    

    PHP操作ES

    官网:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html

    创建索引

    $hosts = [
        '127.0.0.1:9200'
    ];
    // 实例化es对象
    $client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
    // 创建索引
    $params = [
        // 索引名
        'index' => 'fangs',
        'body' => [
            // 指定副本和分片
            'settings' => [
                // 分片 后续不可修改
                'number_of_shards' => 5,
                // 副本后续可修改
                'number_of_replicas' => 1
            ],
            'mappings' => [
                '_source' => [
                    'enabled' => true
                ],
                // 字段
                'properties' => [
                    'xiaoqu' => [
                        // 精确查询
                        'type' => 'keyword'
                    ],
                    'desn' => [
                        // 模糊搜索
                        'type' => 'text',
                        // 插件 中文分词插件  需要安装
                        'analyzer' => 'ik_max_word',
                        'search_analyzer' => 'ik_max_word'
                    ]
                ]
            ]
        ]
    ];
    $response = $client->indices()->create($params);
    
    dump($response);
    

    更新文档

    $hosts = [
        '127.0.0.1:9200',
    ];
    $client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
    // 写文档
    $params = [
        'index' => 'goods',
        'id' => $model->id,
        'body' => [
            'title' => $model->title,
            'desn' => $model->desn,
        ],
    ];
    $response = $client->index($params);
    
    image.png

    搜索

    $hosts = [
        '127.0.0.1:9200',
    ];
    $client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
    $params = [
        'index' => 'goods',
        'body' => [
            'query' => [
                'match' => [
                    'title'=>[
                        'query' => '手机'
                    ]
                ]
            ]
        ]
    ];
    $results = $client->search($params);
    dump($results);
    
    image.png

    相关文章

      网友评论

          本文标题:PHP+kibana+es用法

          本文链接:https://www.haomeiwen.com/subject/qorvjktx.html