elasticsearch php

2017-07-27  本文已影响0人  花钱约下

新建索引

<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分词类型

深度分页

设置查询缓存

<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>

上一篇下一篇

猜你喜欢

热点阅读