ES学习笔记2:权限,索引管理,文档管理,php_client

访问权限

新版默认开启了权限,包括用户权限和传输加密权限:user:pwd和https

方便起见可以关闭:

elasticsearch.yml
xpack.security.enabled: false

索引管理

参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/explicit-mapping.html(官方)

新建索引并设置mapping:使用PUT

#创建索引
PUT /my-index-000001
#创建并设置mapping
PUT /my-index-000001
{ "mappings": { "properties": { "age": { "type": "integer" }, "email": { "type": "keyword" }, "name": { "type": "text" } } } }

修改索引mapping:PUT

复制代码
PUT /my-index-000001/_mapping
{
  "properties": {
    "employee-id": {
      "type": "keyword",
      "index": false
    }
  }
}
复制代码

删除索引:DELETE

#删除索引
DELETE /my-index-000001

文档管理

参考:https://www.elastic.co/guide/en/elasticsearch/reference/8.13/docs-index_.html(官方)

索引写入数据:使用POST

有的配置:必须先创建好索引,有的会自动生成索引

复制代码
#自定义id
curl -H "Content-Type: application/json" -X POST 'http://localhost:9200/test1/_doc/2' -d '{
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}'

#系统生成id
curl -H "Content-Type: application/json" -X POST 'http://localhost:9200/test1/_doc/2' -d '{
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"35.99"
}'

#修改文档:通过id会自动判断为新增和修改
curl -H "Content-Type: application/json" -X POST 'http://localhost:9200/test1/_doc/2' -d '{
  "title": "Elasticsearch Blueprints",
  "name" : {
    "first" : "Vineeth",
    "last" : "Mohan"
  },
  "publish_date":"2015-06-06",
  "price":"100"
}'
复制代码

删除文档:DELETE和POST

参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html(官方:按查询删除)

复制代码
#按id删除
curl -H "Content-Type: application/json" -X DELETE http://localhost:9200/test1/_doc/2
#按查询删除
POST /my-index-000001/_delete_by_query
{
    "query": {
        "match": {
            "user.id": "elkbee"
        }
    }
}
复制代码

查看文档:在官方文档的搜索模块:支持GET和POST

参考:https://www.elastic.co/guide/en/elasticsearch/reference/8.13/search-search.html(官方)

复制代码
查看一个文档
GET /my-index-000001/_doc/0

查询多个多个文档
GET /<target>/_search
POST /<target>/_search
GET /_search
POST /_search

GET /my-index-00001/_search
复制代码

php客户端

参考:

https://www.elastic.co/guide/cn/elasticsearch/php/current/_quickstart.html(官方php包)

https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html(选择es-php版本)

安装


//
1,安装composer包 "elasticsearch/elasticsearch": "~7.0" //2,初始化es客户端 require 'vendor/autoload.php'; $client = Elasticsearch\ClientBuilder::create()->build();

 

索引操作(以7.16版本为例)

创建索引

$client = ClientBuilder::create()->build();
$params = [
    'index' => 'my_index'
];

// Create the index
$response = $client->indices()->create($params);

删除索引

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

索引存储设置

复制代码
//查看
// Get settings for one index
$params = ['index' => 'my_index'];
$response = $client->indices()->getSettings($params);

// Get settings for several indices
$params = [
    'index' => [ 'my_index', 'my_index2' ]
];
$response = $client->indices()->getSettings($params);

//设置
$params = [
    'index' => 'my_index',
    'body' => [
        'settings' => [
            'number_of_replicas' => 0,
            'refresh_interval' => -1
        ]
    ]
];

$response = $client->indices()->putSettings($params);
复制代码

索引文档数据结构类型设置

复制代码
//查看
// 全部索引Get mappings for all indices
$response = $client->indices()->getMapping();

// 单个Get mappings in 'my_index'
$params = ['index' => 'my_index'];
$response = $client->indices()->getMapping($params);

// 多个Get mappings for two indices
$params = [
    'index' => [ 'my_index', 'my_index2' ]
];
$response = $client->indices()->getMapping($params);

//设置
$params = [
    'index' => 'my_index',
    'body' => [
        '_source' => [
            'enabled' => true
        ],
        'properties' => [
            'first_name' => [
                'type' => 'text',
                'analyzer' => 'standard'
            ],
            'age' => [
                'type' => 'integer'
            ]
        ]
    ]
];

// Update the index mapping
$client->indices()->putMapping($params);
复制代码

 

文档操作

添加文档

$params = [
    'index' => 'my_index',
    'id'    => 'my_id',//如果不设置id,会es自动生成一个id
    'body'  => [ 'testField' => 'abc']
];

// Document will be indexed to my_index/_doc/my_id
$response = $client->index($params);

获取文档

$params = [
    'index' => 'my_index',
    'id'    => 'my_id'
];

// Get doc at /my_index/_doc/my_id
$response = $client->get($params);

删除文档

$params = [
    'index' => 'my_index',
    'id'    => 'my_id'
];

// Delete doc at /my_index/_doc_/my_id
$response = $client->delete($params);

修改文档

复制代码
//部分修改
$params = [
    'index' => 'my_index',
    'id'    => 'my_id',
    'body'  => [
        'doc' => [
            'new_field' => 'abc'
        ]
    ]
];

// Update doc at /my_index/_doc/my_id
$response = $client->update($params);

//脚本修改
$params = [
    'index' => 'my_index',
    'id'    => 'my_id',
    'body'  => [
        'script' => 'ctx._source.counter += count',
        'params' => [
            'count' => 4
        ]
    ]
];

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

//修改或新增
$params = [
    'index' => 'my_index',
    'id'    => 'my_id',
    'body'  => [
        'script' => [
            'source' => 'ctx._source.counter += params.count',
            'params' => [
                'count' => 4
            ],
        ],
        'upsert' => [
            'counter' => 1
        ],
    ]
];

$response = $client->update($params);
复制代码

查询文档

参考:https://www.jianshu.com/p/1e970765198c(bool多条件查询)

复制代码
//字段
$params
= [ 'index' => 'my_index', 'body' => [ 'query' => [ 'match' => [ 'testField' => 'abc' ] ] ] ]; $results = $client->search($params); //bool组合 $params = [ 'index' => 'my_index', 'body' => [ 'query' => [ 'bool' => [ 'must' => [ [ 'match' => [ 'testField' => 'abc' ] ], [ 'match' => [ 'testField2' => 'xyz' ] ], ] ] ] ] ]; $results = $client->search($params);
//filter
$params = [ 'index' => 'my_index', 'body' => [ 'query' => [ 'bool' => [ 'filter' => [ 'term' => [ 'my_field' => 'abc' ] ], 'should' => [ 'match' => [ 'my_other_field' => 'xyz' ] ] ] ] ] ]; $results = $client->search($params); $milliseconds = $results['took']; $maxScore = $results['hits']['max_score']; $score = $results['hits']['hits'][0]['_score']; $doc = $results['hits']['hits'][0]['_source'];
复制代码

 

posted @   指令跳动  阅读(124)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示