PHP-ElasticSearch学习记录

目录

一.ElasticSearch安装

1.下载ElasticSearch(window)服务端

 2.PHP安装es扩展

二.ElasticSearch属性单词介绍

三.Elasticsearch-PHP api简介


文档地址:https://www.elastic.co/guide/cn/index.html

基于 window 学习记录

一.ElasticSearch安装

1.下载ElasticSearch(window)服务端

①.下载地址:https://www.elastic.co/cn/downloads/elasticsearch Downloads window版本

②.下载到本地目录结构如下:双击打开 bin下的 elasticsearch.bat 开启es服务

③.浏览器访问 127.0.0.1:9200  如下:安装成功

 2.PHP安装es扩展

①.安装es扩展需基于composer,如果没有需先安装composer

composer的安装移驾到 https://www.runoob.com/w3cnote/composer-install-and-usage.html

②.安装es扩展

在项目地址下,例如“WWW”下新建文件夹“ES”,在“ES”目录下“cmd”调出命令行执行:

composer require elasticsearch/elasticsearch

③. 安装完成后在“ES”目录下新建“index.php”如下:

 ④.浏览器访问下项目地址,查看打印信息,如下,说明配置成功。

二.ElasticSearch属性单词介绍

took :该请求花了多少毫秒 

 _shards:查询分片的信息

 hits:搜索的结果,total是全部的满足的文档数目,hits是返回的实际数目(默认是10)

 max_score和_score :查询结果的评分值,分越高,代表权重越大,排名越靠前

range:范围查找 

term:精准查找

 `bool`把各种其它查询通过`must`(必须 and )、`must_not`(必须不)、`should`(应该 or)的方式进行组合

 查找组装的条件还有很多种:去官网看看吧,自我感觉组装条件就是es的核心了

 便于记忆,与mysql的比较

三.Elasticsearch-PHP api简介

映射数据说明:
    字段名:任意填写,下面指定许多属性,例如:title、subtitle、images、price
    type:类型,Elasticsearch 中支持的数据类型非常丰富,说几个关键的:
        String 类型,又分两种:
                text:可分词
                keyword:不可分词,数据会作为完整字段进行匹配
        Numerical:数值类型,分两类
                基本数据类型:long、integer、short、byte、double、float、half_float
                浮点数的高精度类型:scaled_float
        Date:日期类型
        Array:数组类型
        Object:对象
    index:是否索引,默认为 true,也就是说你不进行任何配置,所有字段都会被索引。
         true:字段会被索引,则可以用来进行搜索
         false:字段不会被索引,不能用来搜索
    store:是否将数据进行独立存储,默认为 false
    原始的文本会存储在_source 里面,默认情况下其他提取出来的字段都不是独立存储的,      是从_source 里面提取出来的。当然你也可以独立的存储某个字段,只要设置"store": true      即可,获取独立存储的字段要比从_source 中解析快得多,但是也会占用更多的空间,所      以要根据实际业务需求来设置
    analyzer:分词器,这里的 ik_max_word 即使用 ik 分词器

 创建索引                             $client->indices()->create()

$params = [
	'index' => 'user_index', //索引名称
	'body' => [
		'settings'=> [ //配置
			'number_of_shards'=> 3,//主分片数
			'number_of_replicas'=> 1 //主分片的副本数
		],
		'mappings'=> [  //映射                   
				'_source'=>[   //  存储原始文档
					'enabled' => 'true'
				],
				'properties'=> [ //配置数据结构与类型
					'userid'=> [ //字段1
						'type'=>'integer',
						'index'=> 'false',
					],
					'username'=> [ //字段1
						'type'=>'text',//类型 text、integer、float、double、boolean、date
						'index'=> 'true',//是否索引
					],
					'age'=> [ //字段2
						'type'=>'integer',
					],
					'sex'=> [ //字段3
						'type'=>'keyword',
					],
				]
			
		],
	]
];
$data = $client->indices()->create($params);

获取索引信息                             $client->indices()->getSettings()

$params = [
    'index' => 'user_index',
    'client' => [
        'ignore' => [404,400]//ignore可以忽略异常,其值是需要忽略的异常对应的返回码,常见的有400表示索引已存在,404表示索引没找到
    ]
];
$res = $client->indices()->getSettings($params);//获取库索引设置信息

 获取Mapping信息                             $client->indices()->getMapping()

$params = [
    'index' => 'user_index',
    'client' => [
        'ignore' => [404,400]//ignore可以忽略异常,其值是需要忽略的异常对应的返回码,常见的有400表示索引已存在,404表示索引没找到
    ]
];
$res = $client->indices()->getMapping($params);   //获取mapping信息

 添加mapping信息                             $client->indices()->putMapping()

//注意事项:已经建立好的字段类型是不能更改的!!

$params = [
    'index' => 'user_index',  //索引名(相当于mysql的数据库)
    'body'  =>  [          
		'properties'=> [ //配置数据结构与类型
			'height'=> [ 
				'type'=>'integer',
			],
		]
	],
];
$res = $client->indices()->putMapping($params);

 删除索引                             $client->indices()->delete()

$params = [
    'index' => 'user_index',  //索引名(相当于mysql的数据库)
];
$res = $client->indices()->delete($params);

 插入一条数据                             $client->index()

$params = [
    'index' => 'user_index',
    'id' => 'user_id', // 不填则es会自动生成唯一的id
    'body'  => ['userid'=>1,'username' => '张三','age'=>42,'sex'=>'男']
];
$res = $client->index($params);

 更新一条数据                           $client->update()

$params = [  
    'index' => 'user_index',
    'id' => '39',
    'body' => [  
        'doc' => [  // 必须带上这个.表示是文档操作
            'age' => 150  
        ]  
    ]  
];  
$res = $client->update($params); 

 删除一条数据                            $client->delete()

$params = [
    'index' => 'user_index',
    'id'    => 'user_id'
];
$res = $client->delete($params);

 批量插入索引数据                           $client->bulk()

$params = [];
for($i = 1; $i < 40; $i++) {  
	$params['body'][] = [
		'index' => [
			'_index' => 'user_index',
			'_id'    => $i,
		]
	];  
	$params['body'][] = [ 
		'userid' => $i,
		'username' => '编程'.$i,  
		'sex' => $i%2 ? '男' : '女',
		'age'=>(20+$i),
	];
}  
$res = $client->bulk($params);

 分页查询 + 高亮显示+条件查询+排序                           $client->search()

$params = [
    'index' => 'user_index', 
];
$params['body'] = array(
    'query' => array(
    	'match' => array(
	            'sex' => '男'
	    ), 
    ),
	'from' => '0',  // 分页 默认从 0 开始。 from = (pageNum - 1) * size (页码-1)*每页条数, 第一页:(1-1)*2=0, 第二页:(2-1)*2=2
    'size' => '5', // 每页数量 默认为 10
    'sort' => array(  // 排序
        'age' => 'desc'   //对age字段进行降序排序  
    ),  
    'highlight' => array( //高亮没弄明白,没看出来有什么变化
        'fields' => array(
            'username' => new \stdClass() //空对象 (object)[] //另一种写法(优雅)
        )
    )
);
 
$res = $client->search($params);

 json查询                         $client->search()

$params = [
    'index' => 'user_index',
];
$json='{
  "query": {
    "bool": {
      "must": { "match_all": {} }
    }
  },
  "sort" : { "age" : "desc" },
  "size" : 10 
}';
$params['body'] = $json;
$res = $client->search($params);

 使用json查询,可以参考 https://blog.csdn.net/robinhunan/article/details/106693313

 复合查询                         $client->search()

$query = [
    'query' => [
        'bool' => [
            'must' => [
                'match' => [
                    'username' => '编',
                ],
                'match' => [
                    'sex' => '男',
                ]
            ],
            'filter' => [
                'range' => [
                    'age' => ['gt' => 76]
                ]
            ]
        ]
    ]
];
$params = [
    'index' => 'user_index',
//  'index' => 'm*', #index 是可以模糊匹配的,甚至这个参数是可选的
    '_source' => ['username','age','sex'], // 请求指定的字段
    'body' => array_merge([
        'from' => 0,
        'size' => 5
    ],$query)
];
$res = $client->search($params);

就这点东西,皮毛都算不上!哇的一下子就哭了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值