如何在弹性搜索中突出显示我的结果?

时间:2015-08-17 14:09:09

标签: php elasticsearch

我无法突出显示我的结果,我的查询部分是错误的? 弹性搜索的PHPClient会在执行时抛出异常。

$query = [

    "query" => [
    "filtered" => [
        "query" => [
            "bool" => [
                "should" => [
                    [
                        'query_string' => [
                            'fields' => [
                                'Title.title^4',
                                'Title.ngrams_front^2',
                                'Title.ngrams_back'
                            ],
                            'defaultOperator' => 'or',
                            'query' => $paramsObj->q
                        ]
                    ],
                    [
                        'query_string' => [
                            'auto_generate_phrase_queries' => 0,
                            'enable_position_increments' => false,
                            'fields' => [
                                'Title.title',
                                'Address',
                                'keys'
                            ],
                            'query' => $paramsObj->q,
                            'use_dis_max' => false,
                            'boost' => 2
                        ]
                    ],
                    [
                        'fuzzy' => [
                            'Title.title' => [
                                'value' => $paramsObj->q,
                                'boost' => 1,
                                'min_similarity' => 0.5,
                                'max_expansions' => 20,
                                'prefix_length' => 0
                            ]
                        ]
                    ]
                ]   
            ]
        ],
        "filter" => $filters
        ]
    ],
    "highlight" => [
        "fields" => [
            'Title.title' => [ "pre_tags" => "<em>", "post_tags" => "</em>" ]
        ]
    ]

];

首先我尝试在过滤级别突出显示,然后我用Google搜索并发现我需要在过滤级别顶部的查询级别执行,所以我做了但仍然会抛出异常。

Fatal error: Uncaught exception 'Guzzle\Http\Exception\ClientErrorResponseException' 

如果有人可以提供帮助,请提供帮助。

1 个答案:

答案 0 :(得分:5)

尝试这样的事情:

$query = array(
    'query' => array(
        'bool' => array(
            'should' => array(
                'fuzzy' => array(
                    'name' => array(
                        'value' => $serachstring,
                        'boost' => 1,
                        'min_similarity' => 0.5,
                        'max_expansions' => 20,
                        'prefix_length' => 0
                    ),
                ),
                // ...
            )
        ),
    ),
    'highlight' => array(
        "pre_tags" => "<em>", 
        "post_tags" => "</em>",
        'fields' => array(
            'name' => (object) array()
        )
    ),
);