新建索引
$es->indices()->create(['index' => '库名']);
新进索引文档
$es->index([
'index'=> '库名',
'type' => '表名',
'body' => [
'field' => 'field data',
'field' => 'field data',
'field' => 'field data',
'field' => 'field data',
'field' => 'field data',
'field' => 'field data',
]
]);
fielddata = true 设置
$x= $es->indices()->putMapping([
'index'=> '库名',
'type' => '表名',
'body' => [
'properties' => ['called' => ['type' => 'text', 'fielddata' => true],]
]
]);
设置字段类型
$es->indices()->putMapping([
'type' => 'vos_data',
'body' => [
'properties' => [
'id' => ['type' => 'integer'],
'vos_id' => ['type' => 'integer'],
'vos_ip' => ['type' => 'string', 'index' => 'not_analyzed'],
'vos_ip_from' => ['type' => 'string', 'analyzer' => 'ik_max_word', 'search_analyzer' => 'ik_max_word'],
'callere164' => ['type' => 'string'],
]
]
]);
删除指定索引下的全部文档
POST 库名/索引名/_delete_by_query
{
"query": {
"match_all": {}
}
}
统计数量
$es->count([
'index' => '库名',
'type' => '索引名',
'body' => [
'query' => [查询条件]
]
]);
查询
$es->search([
'size' => '每页显示数量,默认10条',
'from' => '开始位置,默认0'
'index' => '库名',
'type' => '索引名',
'body' => [
'query' => ['查询条件'],
'aggs' => [
'es_field' => [
'terms' => [
'field' => 'group by field'
]
]
],
//或者 统计
'aggs' => [
'num' => [
'value_count' => [
'count' => '_index'
]
]
],
//排序
'sort' => ['aa' => 'asc', 'bb' => 'desc''],
//返回字段
'_source' => ['aa', 'bb', 'cc']
]
]);
删除指定文档
$es->indices()->delete([
'index' => '库名',
'type' => '表名',
'id' => '索引文档id'
]);
获取一条数据
$es->get([
'index' => '库名',
'type' => '表名',
'id' => '索引文档id',
'body' => ['查询条件']
]);
IK分词类型
- ik_smart 会做最粗粒度的拆分;已被分出的词语将不会再次被其它词语占有
- ik_max_word 会将文本做最细粒度的拆分;尽可能多的拆分出词语
深度分页
- 深度分页,es默认是【size+from == 10000】超出会出现内存溢出问题,那么可以用Search After 类似scroll。
- 使用search after,必须设置from=0或者-1。
- search after 只能向下翻页,向上翻页,失去排序效果。
$es->search([
'index' => 'xx'
'type' => 'oo',
'body' => [
'query' => [
'bool' => [
'must' => [
['match' => ['field' => 'value']]
]
]
],
'sort' => [
'field' => [
'order' => 'desc'
]
],
'search_after' => [1234567890]
]
]);
设置查询缓存
PUT /索引名/_settings
{ "index.requests.cache.enable": true }
给已存在的文档新增字段
POST 库名/_update_by_query
{
"script":{
"lang":"painless",
"inline":"if (ctx._source.字段名 == null) {ctx._source.字段名 = ''}"
}
}