项目迁移前在es上需要实现的操作
分页
size=x&from=x
排序
{
"sort": { "title": { "order": "asc" }}
}
多字段都含有不同的字段
{
"query": {
"bool": {
"should": [
{ "match": { "title": "邪气" }},
{ "match": { "author": "七星" }}
]
}
}
}
多字段含有相同的字段或
{
"query": {
"bool": {
"should": [
{ "match": { "title": "War and Peace" }},
{ "match": { "author": "Leo Tolstoy" }}
]
}
}
}
过滤条件
POST /book/fulltext/_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "青春" }},
{ "match": { "author": "青春" }}
]
}
}
}
搜索权重设置
POST /book/fulltext/_search
{
"query":{
"multi_match": {
"query": "青春",
"type": "best_fields",
"fields": [ "title^2", "author" ],
"tie_breaker": 0.3,
"minimum_should_match": "30%"
}
}
}
{
"query": {
"multi_match": {
"type": "best_fields", //完全匹配权重高
"query" : "我的宝马多少马力",
"fields" : ["title", "content"]
}
}
}
简单的过滤
{
"query":{
"multi_match": {
"query": "青春",
"type": "best_fields",
"fields": [ "title^2", "author" ],
"tie_breaker": 0.3,
"minimum_should_match": "30%"
}
},
"post_filter": {
"term":{
"title":"青春期"
}
}
}
A filtered query affects both search results and aggregations.filtered查询会影响搜索结果和聚合。
filter桶只影响聚合。
post_filter只影响搜索结果。
使用php操作es
require_once('vendor/autoload.php');
$params = array('hosts' => array (
'127.0.0.1:9200',
));
$client = new Elasticsearch\Client($params);
$params = array();
$params['index'] = 'book';
$params['type'] = 'log_type';
$params['body']['query']['match']['src_ip'] = '1.122.33.141';
$params = array(
'index'=>'book',
'type'=>'fulltext',
'body' => array (
'query' =>array (
'multi_match' =>array (
'query' => '青春',
'type' => 'best_fields',
'fields' => array (
0 => 'title^2',
1 => 'author',
),
'tie_breaker' => 0.29999999999999999,
'minimum_should_match' => '30%',
),
),
)
);
//es 搜索
$start_time = microtime(true);
$res = $client->search($params);
$end_time = microtime(true);
echo "cost time:".($end_time - $start_time);
print_r($res);
//es 创建索引
$params = array();
$params['body'] = array (
'title' => '征婚女人',
'author' => '青青翠竹',
);
$params['id'] = 32;
$params['index'] = 'book';
$params['type'] = 'fulltext';
$ret = $client->c