从sphinx迁移到elasticsearch常用的功能

项目迁移前在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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值