创建索引
<?php
/**
* https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index_management.html
* https://open.163.com/newview/movie/free?pid=XGG8PCIUJ&mid=HGG8PFDRB
*/
use Elasticsearch\ClientBuilder;
require './vendor/autoload.php';
/**
* @param $num 生成汉字的数量
* @return string
*/
function getChar($num)
{
$b = '';
for ($i = 0; $i < $num; $i++) {
// 使用chr()函数拼接双字节汉字,前一个chr()为高位字节,后一个为低位字节
$a = chr(mt_rand(0xB0, 0xD0)) . chr(mt_rand(0xA1, 0xF0));
// 转码
$b .= iconv('GB2312', 'UTF-8', $a);
}
return $b;
}
$client = ClientBuilder::create()
->setHosts(['127.0.0.1:9200'])
->setRetries(2)
->build();
$index = [
'index' => 'shop',
'body' => [
'settings' => [
'number_of_shards' => 5,
'number_of_replicas' => 0,
'analysis' => [
'analyzer' => [
'text_analyzer' => [
'tokenizer' => 'ik_max_word',
'filter' => 'py'
],
'completion_analyzer' => [
'tokenizer' => 'keyword',
'filter' => 'py'
]
],
'filter' => [
'py' => [
"type"=> "pinyin",
"keep_full_pinyin"=> false,
"keep_joined_full_pinyin"=> true,
"keep_original" => true,
"limit_first_letter_length" => 16,
"none_chinese_pinyin_tokenize"=> false,
"remove_duplicated_term"=> true
]
]
]
],
'mappings' => [
'_source' => [
'enabled' => true
],
'properties' => [
'article_id' => [
'type' => 'integer' // long
],
'title' => [
'type' => 'text',
'analyzer' => 'text_analyzer',
'search_analyzer' => 'ik_smart',
],
'title_completion' => [
'type' => 'completion',
'analyzer' => 'completion_analyzer',
// 狮子 虱子 search:我掉到了狮子笼里怎么办
// 人民 人民日报 search: 日美 -> rm
'search_analyzer' => 'ik_smart',
],
'content' => [
'type' => 'text',
'analyzer' => 'text_analyzer',
'search_analyzer' => 'ik_smart',
]
]
]
]
];
//$response = $client->indices()->create($index);
//print_r($response);
//exit;
$doc = [
'index' => 'shop',
'id' => 2,
'body' => [
'article_id' => 2,
'title' => '人民日报再次点名阿里,不要妄想大而不倒',
'title_completion' => ['人民日报', '阿里', '点名', '妄想', '不倒'],
'content' => '据吉林省委政法委官方微信公众号“平安吉林”消息:8月31日,省委召开省委政法委员会干部会议,宣布省委政法委主要领导任职决定。省委副书记高广滨,省委常委、省委政法委书记范锐平出席会议并讲话。省委常委、省委组织部部长张恩惠主持会议并宣布决定。副省长、省公安厅厅长刘金波,省法院院长徐家新,省检察院检察长尹伊君,省武警总队司令员赵洪炜出席会议。'
]
];
//$client->index($doc);
//exit;
$docs = [];
for ($i = 11; $i <= 1000; $i++) {
$docs['body'][] = [
'index' => [
'_index' => 'article',
'_id' => $i,
],
];
$docs['body'][] = [
'article_id' => $i,
'title' => getChar(20),
'content' => getChar(200),
'title_completion' => getChar(100)
];
}
//$client->bulk($docs);
//exit;
$params = [
'index' => 'shop',
'body' => [
'query' => [
'match' => [
'title' => '阿里'
]
],
'stored_fields' => ['title']
]
];
/* select * from article where title like '%国际%' or title like '%test%'*/
// $result = $client->search($params);
// var_dump($result);
// exit;
$params = [
'index' => 'article',
'body' => [
'query' => [
'bool' => [
'must' => [
["match" => ['title' => 'test']],
["match" => ['title' => 'elasticsearch']]
]
]
]
]
];
/* select * from article where title like '%国际%' and title like '%test%'*/
// $result = $client->search($params);
// var_dump($result);
$params = [
'index' => 'article',
'body' => [
'query' => [
'bool' => [
'must' => [
["match" => ['title' => '阿里']],
["match" => ['content' => '阿里']]
]
]
],
'_source' => ['article_id', 'title'],
'sort' => [['article_id' => 'desc']]
]
];
/* select article_id, title from article where title like '%国际%' and content like '%test%' order by article_id desc*/
// $result = $client->search($params);
// var_dump($result);
$params = [
'index' => 'article',
'body' => [
'query' => [
'bool' => [
'must' => [
["match" => ['title' => '阿里']],
["match" => ['content' => '阿里']]
]
]
],
// '_source' => ['article_id', 'title'],
'_source' => [''],
// 'sort' => [['article_id' => 'desc']],
'highlight' => [
// 'fields' => ['title' => ['type' => 'unified'], 'content' => ['type' => 'unified']],
'fields' => ['title' => new \stdClass(), 'content' => new \stdClass()],
'pre_tags' => '<b style="color: red">',
'post_tags' => '</b>'
]
]
];
/* select article_id, title from article where title like '%国际%' and content like '%test%' order by article_id desc*/
// $result = $client->search($params);
// var_dump($result);
// 搜索建议,前缀检索
$params = [
'index' => 'shop',
'body' => [
'suggest' => [
'my-suggest' => [
'text' => 'w想',
'completion' => [
'field' => 'title_completion',
'skip_duplicates' => true,
'size' => 5
],
]
],
]
];
$result = $client->search($params);
var_dump($result);
echo "<hr/>";
//exit;
$params = [
'index' => 'shop',
'body' => [
'query' => [
'match' => [
'title' => 'zaici'
]
],
// 'stored_fields' => ['title']
]
];
/* select * from article where title like '%国际%' or title like '%test%'*/
$result = $client->search($params);
var_dump($result);
//exit;suggest autocomplete自动完成
<?php
use Elasticsearch\ClientBuilder;
require './vendor/autoload.php';
$keyword = $_GET['keyword'] ?? 'rm';
$client = ClientBuilder::create()
->setHosts(['127.0.0.1:9200'])
->setRetries(2)
->build();
// 搜索建议,前缀检索
$params = [
'index' => 'shop',
'body' => [
'suggest' => [
'my-suggest' => [
'text' => $keyword,
'completion' => [
'field' => 'title_completion',
'skip_duplicates' => true,
'size' => 5,
],
],
],
'_source' => ['title_completion']
]
];
$result = $client->search($params);
//var_dump($result['suggest']['my-suggest']);exit;
//echo json_encode($result['suggest']['my-suggest'], JSON_UNESCAPED_UNICODE);
$words = [];
foreach ($result['suggest']['my-suggest'] as $suggest) {
foreach ($suggest['options'] as $option) {
$words = array_merge($words, [$option['text']]);
}
}
echo json_encode($words, JSON_UNESCAPED_UNICODE);<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
</head>
<body>
<input type="text" id="keyword" name="keywords" />
</body>
</html>
<!--<script type="text/javascript" src="http://localhost/public/js/jquery.min.js"></script>-->
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
$(function(){
var $searchBox = $('#keyword');
$searchBox.autocomplete({
minLength: 2, // 最小字符数,默认1
delay: 300, // 延迟加载300ms
source: function (request, response) { // 数据源
console.log(request.term);
$.ajax({
url: 'http://localhost/elasticsearch-php/auto_complete.php?keyword=' + request.term,
success: function (res) {
res = JSON.parse(res);
response(res);
}
});
},
select: function (event, ui) { // 选中事件
$('#keyword').text(ui.item.value);
// window.location.href = locate_url(start, size);
}
});
});
</script>
fffffasdfasdfasdfasdfas;kdfjlaskl;dfjaskl;dfjakls;dfjkal;sfdjlaks;df als;dfj awkls;dfj asl;dfj aslk;dfjaskl;dfjas;ldf
访客评论