dongwenxiu5200 2017-04-06 17:02
浏览 442

ElasticSearch - 按分数和位置排序

I use elasticsearch. I need to find the results by word and sort them by deception (_score), and then by distance (_geo_distance) By coincidence of phrases, I find the following sensations and sort them out by coincidence (_score).

Array
(
    [took] => 23
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 469
            [max_score] => 5.350864
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => salon
                            [_type] => services
                            [_id] => 7686
                            [_score] => 5.350864
                            [_source] => Array
                                (
                                    [service_name] => Found a string 1
                                    [service_id] => 10493
                                    [location] => Array
                                        (
                                            [lat] => 55.701328
                                            [lon] => 37.507412
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [_index] => salon
                            [_type] => services
                            [_id] => 8350
                            [_score] => 5.350864
                            [_source] => Array
                                (
                                    [service_name] => Found a string 2
                                    [service_id] => 11171
                                    [location] => Array
                                        (
                                            [lat] => 55.869915
                                            [lon] => 37.613728
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [_index] => salon
                            [_type] => services
                            [_id] => 14883
                            [_score] => 5.237593
                            [_source] => Array
                                (
                                    [service_name] => Found a string 3
                                    [service_id] => 17851
                                    [location] => Array
                                        (
                                            [lat] => 55.691734
                                            [lon] => 37.728164
                                        )

                                )

                        )

...

But I do not understand how to add more and sorting by coordinates, for example, so that would be sorted by the nearest to 55.69,37.72

Here is my search code in elasticsearch:

require 'vendor/autoload.php';
function searchES($word) {

    $client = \Elasticsearch\ClientBuilder::create()->setHosts([ES_HOST])->build();

    $IDs = [];
    $params = [];
    $params['index'] = ES_INDEX;

    if ($client->indices()->exists($params)) {

        $params['type'] = ES_TYPE;
        $params['size'] = 10000;
        $params['body']['sort'] = ['_score' => 'desc'];
        $params['body']['query']['match']['service_name'] = trim($word);


        $result = $client->search($params);

        if ($result['hits']['total'] > 0) {

            $result = $result['hits']['hits'];

            foreach ($result as $val) {

                $k = $val['_source']['service_id'];
                $IDs[$k] = $val['_source']['service_name'];

            }

        }

    }

    return $IDs;
}

Params:

Array
(
    [index] => salon
    [body] => Array
        (
            [settings] => Array
                (
                    [analysis] => Array
                        (
                            [filter] => Array
                                (
                                    [ru_stop] => Array
                                        (
                                            [type] => stop
                                            [stopwords] => _russian_
                                        )

                                    [ru_stemmer] => Array
                                        (
                                            [type] => stemmer
                                            [language] => russian
                                        )

                                )

                            [analyzer] => Array
                                (
                                    [default] => Array
                                        (
                                            [tokenizer] => standard
                                            [filter] => Array
                                                (
                                                    [0] => lowercase
                                                    [1] => ru_stop
                                                    [2] => ru_stemmer
                                                )

                                        )

                                )

                        )

                )

            [mappings] => Array
                (
                    [_default_] => Array
                        (
                            [properties] => Array
                                (
                                    [service_name] => Array
                                        (
                                            [type] => string
                                            [analyzer] => default
                                        )

                                )

                        )

                )

        )

)
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 MATLAB动图的问题
    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名