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