ElasticSearch - order by score and location

时间:2017-04-06 17:02:11

标签: php 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 个答案:

没有答案