问题描述
php操作elastcisearch使用分词做搜索,搜索结果总为空
问题出现的环境背景及自己尝试过哪些方法
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
<?php
/**
- Created by PhpStorm.
- User: wph
- Date: 2019/3/29
- Time: 11:17
*/
namespace AppHttpControllerApi;
use ElasticsearchClientBuilder;
use AppModelEsEsClient;
use EasySwooleCoreComponentDi;
class Test extends Base
{
//建立索引
public function createIndex(){
$esclient = EsClient::getInstance()->esClient;
$params = [
'index' => 'index',
];
$res = $esclient->indices()->create($params);
return $this->writeJson(200,'OK',$res);
}
public function createMap()
{
$esclient = EsClient::getInstance()->esClient;
$params = [
'index' => 'index',
'type' => 'fulltext',
'body' => [
'properties' => [
'content' => [
'type' => 'text', // 字符串型
'analyzer'=>'ik_max_word', //ik_max_word 最细粒度拆分 ik_smart最粗粒度拆分
'search_analyzer'=> 'ik_max_word'
]
]
]
];
$res = $esclient->indices()->putMapping($params);
return $this->writeJson(200,"OK",$res);
}
public function addData()
{
$esclient = EsClient::getInstance()->esClient;
$params = [
'index' => 'index',
'type' => 'fulltext',
'body' => [
'contents' =>'中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首' ,
]
];
$res = $esclient->index($params);
$this->writeJson(200,'OK',$res);
}
public function search()
{
$esclient = EsClient::getInstance()->esClient;
$search_params = [
'index' => 'index',
'type' => 'fulltext',
'body' => [
'query' => [
'match' => [ //必须完全匹配
'content' => '中国'
]]
$res = $esclient->search($search_params);
var_dump($res);
$this->writeJson(200,'OK',$res);
}
}
从创建索引 index 到添加数据 ,再到搜索全是php操作,使用ik分词做全文检索就搜索不到想要结果
首先排除是不是代码导致的问题,你可以在 postman 或者 kibana 中先写DSL查询,看看能否返回结果。
下面是 kibana 的查询语法,postman 将 GET 改为对应ip + 端口即可
我看你的查询写的是中括号,一般都是大括号,涉及到多条件并列时用中括号,这是php的语法规定吗?估计是这个原因吧。