操作index/索引

  1. <?php
  2. require 'vendor/autoload.php';
  3. use Elasticsearch\ClientBuilder;
  4. $client = ClientBuilder::create()->build();
  5. $params = [
  6. 'index' => 'il-fenci',//index名称,不可大写字母开头,_开头
  7. 'body' => [
  8. 'settings' => [
  9. 'number_of_shards' => 1,// 分片数,node不够,别写多,会黄色异常
  10. 'number_of_replicas' => 0// 副本数,node不够,别写多,会黄色异常
  11. ],
  12. 'mappings' => [
  13. '_source' => [
  14. 'enabled' => true//配置,设置字段映射开启
  15. ],
  16. 'properties' => [
  17. 'name' => [
  18. 'type' => 'keyword'
  19. ],
  20. 'title' => [
  21. 'type' => 'text',
  22. 'analyzer' => 'ik_max_word'
  23. ],
  24. 'adders' => [
  25. 'type' => 'text',
  26. 'analyzer' => 'ik_max_word'
  27. ],
  28. 'num' => [
  29. 'type' => 'integer'
  30. ]
  31. ]
  32. ]
  33. ]
  34. ];
  35. //新建索引,文档关系映射
  36. $response = $client->indices()->create($params);
  37. print_r($response);
<?php
require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();

$deleteParams = [
    'index' => 'my_index'
];
$response = $client->indices()->delete($deleteParams);
print_r($response);

操作文档

<?php
require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();
$params = [
    'index' => 'il-fenci',//index索引名
    'id'    => 'my_id',//唯一id,如果不加,ES会自己生成一个
    'body' => [
        'name' => '仙剑的',
        'title' => '数组',
        'adders' => ['维护部', '渡海前往的却唯独一起玩的委屈委屈我去额为'],//可以添加数组
        'num' => 33,
    ]
];
//最普通的添加
$response = $client->index($params);
print_r($response);

//还可以数组批量添加
for($i = 0; $i < 100; $i++) {
    $params['body'][] = [
        'index' => [
            '_index' => 'my_index',
        ]
    ];

    $params['body'][] = [
        'my_field'     => 'my_value',
        'second_field' => 'some more values'
    ];
}

$responses = $client->bulk($params);
<?php
require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();
$params = [
    'index' => 'il-fenci',
    'id'    => 'Gn44SX8Bj3FKe6UOAOhd1',
];

$response = $client->delete($params);
<?php
require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();
$params = [
    'index' => 'il-fenci',
    'id'    => 'G36PTn8Bj3FKe6UOd-hE',
    'body'  => [
        'doc' => [
            'adders' => '我说你听,就好了,皇帝呢,你几岁谁',
        ]
    ]
];

//根据id进行修改,如果字段不存在,则添加
$response = $client->update($params);

/**------------脚本更新---------------**/

$params = [
    'index' => 'my_index',
    'id'    => 'my_id',
    'body'  => [
        'script' => 'ctx._source.counter += 1',//counter 值加一
    ]
];

$response = $client->update($params);

$params = [
    'index' => 'my_index',
    'id'    => 'my_id',
    'body'  => [
        'script' => [
            'source' => 'ctx._source.counter += params.count',//params 的count
            'params' => [
                'count' => 4
            ],
        ],
        'upsert' => [
            'counter' => 1 //默认值,当更新文档不存在,就会创建,并且插入默认值
        ],
    ]
];

$response = $client->update($params);

//根据条件更新

$params = [
    'index' => 'il-fenci',
    'body' => [
        'query' => [
            'match' => [
                'name' => '仙剑的',
            ]
        ],
        'script' => [
            "inline" => "ctx._source.title='ni shi shei a '",
        ]
    ]
];

// Update doc at /my_index/_doc/my_id
$response = $client->updateByQuery($params);
print_r($response);
<?php
require 'vendor/autoload.php';

use Elasticsearch\ClientBuilder;

$client = ClientBuilder::create()->build();
/**-------id获取文档,单条,没有其他信息-------**/
$params = [
    'index' => 'my_index',
    'id'    => 'my_id'
];

$response = $client->get($params);
print_r($response);

/**-------搜索文档各种信息都有-------**/

//term 精确查找,num=4的
$params = [
    'index' => 'il-fenci',
    'body'  => [
        'query' => [
            'term' => [
                'num' => 4
            ]
        ]
    ]
];

//terms 精确查找多个数值,num=1/2/3的
$params = [
    'index' => 'il-fenci',
    'body'  => [
        'query' => [
            'terms' => [
                'num' => [1,2,3]
            ]
        ]
    ]
];
//range 范围查找,num 大于1小于3的
$params = [
    'index' => 'il-fenci',
    'body'  => [
        'query' => [
            'range' => [
                'num' => [
                    'gt' => 1,//可以单独使用一个
                    'lt' => 3
                ]
            ]
        ]
    ]
];

//exists 范围查找,查找存在nam1e字段的记录
$params = [
    'index' => 'il-fenci',
    'body'  => [
        'query' => [
            'exists' => [
                'field' => 'nam1e'
            ]
        ]
    ]
];

//match  匹配查询单个
$params = [
    'index' => 'il-fenci',
    'body'  => [
        'query' => [
            'match' => [//只能匹配一个字段,单个搜索条件
                'title' => '你'
            ]
        ]
    ]
];

//match  匹配查询多个
$params = [
    'index' => 'il-fenci',
    'body'  => [
        'query' => [
            'multi_match' => [//只能匹配多个字段,
                'query' => '皇帝',//需要搜索的关键字
                'fields' => ['title','adders']//需要搜索的字段
            ]
        ]
    ]
];

//bool 组合查找,将各种查询组合在一起
$params = [
    'index' => 'il-fenci',
    'body' => [
        'query' => [
            'bool' => [
                'must' => [//must 必须符合
                    'match' => ['title' => '我']
                ],
                'must_not' => [//must_not  必须不符合
                    'term' => [
                        'adders' => '理论'
                    ]
                ],
                'filter' => [//filter  过滤 也是一种查询 
                    'range' => [
                        'num' => ['gt' => 1]
                    ]
                ],
                'should' => [// should 或者 相当于or
                    ['term' => ['num' => 7]],
                    ['term' => ['num' => 5]]
                ]
            ]
        ]
    ]
];

//分页查询,不过好像不能超过1000条
$params = [
    'index' => 'il-fenci',
    'from' => 0, // 分页参数, 开始偏移配置
    'size' => 3, // 分页参数 - 分页大小,默认 10
    'sort' => 'num:desc',//排序
    'body'  => [
        'query' => [
            'match' => [//只能匹配一个字段,单个搜索条件
                'title' => '我'
            ]
        ]
    ]
];


$results = $client->search($params);
print_r($results);

//json形式的查询
$json = '{
    "query" : {
        "match" : {
            "title" : "你"
        }
    }
}';

$params = [
    'index' => 'il-fenci',
    'body'  => $json
];

$results = $client->search($params);

//滚动

$params = [
    'scroll' => '30s',          // 滚动请求之间的时间间隔。应该很小!
    'size' => 2,             // 您想要返回的每个分片有多少结果
    'index' => 'il-fenci',
    'body' => [
        'query' => [
            'bool' => [
                'filter' => [
                    'term' => ['adders' => '一起']
                ],
                'should' => [
                    'match' => ['title' => '你']
                ]
            ]
        ]
    ]
];

$response = $client->search($params);

while (isset($response['hits']['hits']) && count($response['hits']['hits']) > 0) {
    //处理操作
    print_r($response);

    //完成后,获取新的 scroll_id
    //您必须始终刷新您的 _scroll_id,它有时会改变
    $scroll_id = $response['_scroll_id'];

    // 执行滚动请求并重复
    $response = $client->scroll([
        'body' => [
            'scroll_id' => $scroll_id,  //使用我们之前获得的 _scroll_id
            'scroll' => '30s'
        ]
    ]);
}

查询关键字

ES中的搜索分为两个观念,匹配和搜索,这两个都是查找
一般匹配时针对text类型的,过滤是针对其他类型的
匹配适合查找模糊数据,过滤适合查找精确数据

image.png