PHP 操作 elasticsearch

本文详细介绍了如何使用Elasticsearch进行索引管理,包括列出所有索引、添加、删除和修改文档,以及执行查询和排序操作。同时,展示了PHP如何与Elasticsearch交互,创建索引、更新文档和执行搜索。此外,还讨论了中文分词搜索的实现,以提升搜索效果。
摘要由CSDN通过智能技术生成

列出所有索引

列出所有索引(列出所有的数据库)

GET /_cat/indices?v

添加索引

PUT /goods
{
  "settings": {
      // 副本数
    "number_of_replicas": 1,
      // 分片数
    "number_of_shards": 5
  }
}

删除索引

DELETE /goods

修改文档

POST /goods/_doc/1/_update
{
 "doc": {"price":100 }
}

查询(搜索)

GET /goods/_search

// 查询是xiaomi9的
GET /goods/_search
{
  "query": {
    "match": {
      "title": "xiaomi9"
    }
  }
}

// 排序
GET /goods/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "_id": {
        "order": "desc"
      }
    }
  ]
}


## 进行中文分词搜索

PUT /goods
{
  "mappings": {
    "_doc":{
      "properties":{
        "name":{
          "type":"text",
          "analyzer":"ik_max_word",
          "search_analyzer":"ik_max_word"
        },
        "desn":{
          "type":"text",
          "analyzer":"ik_max_word",
          "search_analyzer":"ik_max_word"
        }
      }
    }
  }
}

PHP操作ES

官网:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html

创建索引

$hosts = [
    '127.0.0.1:9200'
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();

// 创建索引   number_of_shards   分区数后期不可更改    number_of_replicas   副本数后期可更改
$params = [
    'index' => 'goods',
    'body' => [
        'settings' => [
            'number_of_shards' => 5,
            'number_of_replicas' => 1
        ],
        'mappings' => [
            '_doc' => [
                '_source' => [
                    'enabled' => true
                ],
                'properties' => [
                    'title' => [
                        'type' => 'keyword'
                    ],
                    'desn' => [
                        'type' => 'text',
                        'analyzer' => 'ik_max_word',
                        'search_analyzer' => 'ik_max_word'
                    ]
                ]
            ]
        ]
    ]
];
$response = $client->indices()->create($params);

更新文档

$hosts = [
    '127.0.0.1:9200',
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
// 写文档
$params = [
    'index' => 'goods',
    'type' => '_doc',
    'id' => $model->id,
    'body' => [
        'title' => $model->title,
        'desn' => $model->desn,
    ],
];
$response = $client->index($params);

搜索

$hosts = [
    '127.0.0.1:9200',
];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    'index' => 'goods',
    'type' => '_doc',
    'body' => [
        'query' => [
            'match' => [
                'title'=>[
                    'query' => '手机'
                ]
            ]
        ]
    ]
];
$results = $client->search($params);
dump($results);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值