Elasticsearch - sortowanie nie działa prawidłowo[SOLVED]

Witam. Znów mam problem z ES :blush: chciał nie chciał muszę się poradzić.

Mianowicie kiedy dokonuję sortowania nie zawsze ono działa. Jak spojrzycie na poniższe wyniki już po pierwszych dwóch widać, że źle posortowało.

Dodatkowo gdy w kolumnach pojawia się wartość null muszę dodać do parametru sort ignore_unmapped = true.

Wnoszę, że muszę coś jeszcze skonfigurować ale co (czyżby tajemniczy dla mnie mapping)?

curl -X GET 'http://localhost:9200/development_synchronization_report_jobs/_search' -d '{ "query":{ match: { "synchronization_report.id": 3 } }, "sort": "name" }' | python -mjson.tool
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10845  100 10772  100    73  1477k  10251 --:--:-- --:--:-- --:--:-- 1502k
{
    "_shards": {
        "failed": 0,
        "successful": 5,
        "total": 5
    },
    "hits": {
        "hits": [
            {
                "_id": "9",
                "_index": "development_synchronization_report_jobs",
                "_score": null,
                "_source": {
                    "created_at": "2015-08-14T12:34:43.115Z",
                    "end_time": "2015-07-09T13:28:05.000Z",
                    "error": null,
                    "id": 9,
                    "job_id": 0,
                    "job_type": "procedure_action_before",
                    "name": "Log before action on dataArea users.",
                    "start_time": "2015-07-09T13:28:05.000Z",
                    "synchronization_report": {
                        "created_at": "2015-08-14T12:34:43.105Z",
                        "id": 3,
                        "mode": "PULL",
                        "server": "LinuxServer",
                        "server_agent_number": 2,
                        "source_server": "LinuxServer",
                        "source_server_agent_number": 2,
                        "source_sync_id": "eecaf9a0-d715-49ba-be55-92cccd17b6d1",
                        "synch_agent_id": 1,
                        "synch_completed_time": "2015-07-09T13:28:05.000Z",
                        "updated_at": "2015-08-14T12:34:43.105Z"
                    },
                    "synchronization_report_data_area": {
                        "configuration_id": "users",
                        "created_at": "2015-08-14T12:34:43.111Z",
                        "error_text": null,
                        "error_type": null,
                        "id": 3,
                        "records_files_processed": 10,
                        "sql": null,
                        "status": "exported",
                        "synchronization_report_data_set_id": 2,
                        "updated_at": "2015-08-14T12:34:43.111Z"
                    },
                    "synchronization_report_data_area_id": 3,
                    "synchronization_report_data_set": {
                        "configuration_id": "users_and_passwords_server3",
                        "created_at": "2015-08-14T12:34:43.107Z",
                        "id": 2,
                        "synch_time": "2015-07-09T13:28:05.000Z",
                        "synchronization_report_id": 3,
                        "updated_at": "2015-08-14T12:34:43.107Z"
                    },
                    "updated_at": "2015-08-14T12:34:43.115Z"
                },
                "_type": "synchronization_report_job",
                "sort": [
                    "action"
                ]
            },
            {
                "_id": "16",
                "_index": "development_synchronization_report_jobs",
                "_score": null,
                "_source": {
                    "created_at": "2015-08-14T12:34:43.139Z",
                    "end_time": "2015-07-09T13:28:05.000Z",
                    "error": null,
                    "id": 16,
                    "job_id": 3,
                    "job_type": "procedure_action_after",
                    "name": "Log after action on dataArea oldpasswords.",
                    "start_time": "2015-07-09T13:28:05.000Z",
                    "synchronization_report": {
                        "created_at": "2015-08-14T12:34:43.105Z",
                        "id": 3,
                        "mode": "PULL",
                        "server": "LinuxServer",
                        "server_agent_number": 2,
                        "source_server": "LinuxServer",
                        "source_server_agent_number": 2,
                        "source_sync_id": "eecaf9a0-d715-49ba-be55-92cccd17b6d1",
                        "synch_agent_id": 1,
                        "synch_completed_time": "2015-07-09T13:28:05.000Z",
                        "updated_at": "2015-08-14T12:34:43.105Z"
                    },
                    "synchronization_report_data_area": {
                        "configuration_id": "oldpasswords",
                        "created_at": "2015-08-14T12:34:43.129Z",
                        "error_text": null,
                        "error_type": null,
                        "id": 4,
                        "records_files_processed": 7,
                        "sql": null,
                        "status": "exported",
                        "synchronization_report_data_set_id": 2,
                        "updated_at": "2015-08-14T12:34:43.129Z"
                    },
                    "synchronization_report_data_area_id": 4,
                    "synchronization_report_data_set": {
                        "configuration_id": "users_and_passwords_server3",
                        "created_at": "2015-08-14T12:34:43.107Z",
                        "id": 2,
                        "synch_time": "2015-07-09T13:28:05.000Z",
                        "synchronization_report_id": 3,
                        "updated_at": "2015-08-14T12:34:43.107Z"
                    },
                    "updated_at": "2015-08-14T12:34:43.139Z"
                },
                "_type": "synchronization_report_job",
                "sort": [
                    "action"
                ]
            },
            {
                "_id": "12",
                "_index": "development_synchronization_report_jobs",
                "_score": null,
                "_source": {
                    "created_at": "2015-08-14T12:34:43.125Z",
                    "end_time": "2015-07-09T13:28:05.000Z",
                    "error": null,
                    "id": 12,
                    "job_id": 3,
                    "job_type": "procedure_action_after",
                    "name": "Log before action on dataArea users.",
                    "start_time": "2015-07-09T13:28:05.000Z",
                    "synchronization_report": {
                        "created_at": "2015-08-14T12:34:43.105Z",
                        "id": 3,
                        "mode": "PULL",
                        "server": "LinuxServer",
                        "server_agent_number": 2,
                        "source_server": "LinuxServer",
                        "source_server_agent_number": 2,
                        "source_sync_id": "eecaf9a0-d715-49ba-be55-92cccd17b6d1",
                        "synch_agent_id": 1,
                        "synch_completed_time": "2015-07-09T13:28:05.000Z",
                        "updated_at": "2015-08-14T12:34:43.105Z"
                    },
                    "synchronization_report_data_area": {
                        "configuration_id": "users",
                        "created_at": "2015-08-14T12:34:43.111Z",
                        "error_text": null,
                        "error_type": null,
                        "id": 3,
                        "records_files_processed": 10,
                        "sql": null,
                        "status": "exported",
                        "synchronization_report_data_set_id": 2,
                        "updated_at": "2015-08-14T12:34:43.111Z"
                    },
                    "synchronization_report_data_area_id": 3,
                    "synchronization_report_data_set": {
                        "configuration_id": "users_and_passwords_server3",
                        "created_at": "2015-08-14T12:34:43.107Z",
                        "id": 2,
                        "synch_time": "2015-07-09T13:28:05.000Z",
                        "synchronization_report_id": 3,
                        "updated_at": "2015-08-14T12:34:43.107Z"
                    },
                    "updated_at": "2015-08-14T12:34:43.125Z"
                },
                "_type": "synchronization_report_job",
                "sort": [
                    "action"
                ]
            },
            {
                "_id": "13",
                "_index": "development_synchronization_report_jobs",
                "_score": null,
                "_source": {
                    "created_at": "2015-08-14T12:34:43.134Z",
                    "end_time": "2015-07-09T13:28:05.000Z",
                    "error": null,
                    "id": 13,
                    "job_id": 0,
                    "job_type": "procedure_action_before",
                    "name": "Log before action on dataArea oldpasswords.",
                    "start_time": "2015-07-09T13:28:05.000Z",
                    "synchronization_report": {
                        "created_at": "2015-08-14T12:34:43.105Z",
                        "id": 3,
                        "mode": "PULL",
                        "server": "LinuxServer",
                        "server_agent_number": 2,
                        "source_server": "LinuxServer",
                        "source_server_agent_number": 2,
                        "source_sync_id": "eecaf9a0-d715-49ba-be55-92cccd17b6d1",
                        "synch_agent_id": 1,
                        "synch_completed_time": "2015-07-09T13:28:05.000Z",
                        "updated_at": "2015-08-14T12:34:43.105Z"
                    },
                    "synchronization_report_data_area": {
                        "configuration_id": "oldpasswords",
                        "created_at": "2015-08-14T12:34:43.129Z",
                        "error_text": null,
                        "error_type": null,
                        "id": 4,
                        "records_files_processed": 7,
                        "sql": null,
                        "status": "exported",
                        "synchronization_report_data_set_id": 2,
                        "updated_at": "2015-08-14T12:34:43.129Z"
                    },
                    "synchronization_report_data_area_id": 4,
                    "synchronization_report_data_set": {
                        "configuration_id": "users_and_passwords_server3",
                        "created_at": "2015-08-14T12:34:43.107Z",
                        "id": 2,
                        "synch_time": "2015-07-09T13:28:05.000Z",
                        "synchronization_report_id": 3,
                        "updated_at": "2015-08-14T12:34:43.107Z"
                    },
                    "updated_at": "2015-08-14T12:34:43.134Z"
                },
                "_type": "synchronization_report_job",
                "sort": [
                    "action"
                ]
            },
            {
                "_id": "14",
                "_index": "development_synchronization_report_jobs",
                "_score": null,
                "_source": {
                    "created_at": "2015-08-14T12:34:43.136Z",
                    "end_time": "2015-07-09T13:28:05.000Z",
                    "error": null,
                    "id": 14,
                    "job_id": 1,
                    "job_type": "select",
                    "name": "queryElement",
                    "start_time": "2015-07-09T13:28:05.000Z",
                    "synchronization_report": {
                        "created_at": "2015-08-14T12:34:43.105Z",
                        "id": 3,
                        "mode": "PULL",
                        "server": "LinuxServer",
                        "server_agent_number": 2,
                        "source_server": "LinuxServer",
                        "source_server_agent_number": 2,
                        "source_sync_id": "eecaf9a0-d715-49ba-be55-92cccd17b6d1",
                        "synch_agent_id": 1,
                        "synch_completed_time": "2015-07-09T13:28:05.000Z",
                        "updated_at": "2015-08-14T12:34:43.105Z"
                    },
                    "synchronization_report_data_area": {
                        "configuration_id": "oldpasswords",
                        "created_at": "2015-08-14T12:34:43.129Z",
                        "error_text": null,
                        "error_type": null,
                        "id": 4,
                        "records_files_processed": 7,
                        "sql": null,
                        "status": "exported",
                        "synchronization_report_data_set_id": 2,
                        "updated_at": "2015-08-14T12:34:43.129Z"
                    },
                    "synchronization_report_data_area_id": 4,
                    "synchronization_report_data_set": {
                        "configuration_id": "users_and_passwords_server3",
                        "created_at": "2015-08-14T12:34:43.107Z",
                        "id": 2,
                        "synch_time": "2015-07-09T13:28:05.000Z",
                        "synchronization_report_id": 3,
                        "updated_at": "2015-08-14T12:34:43.107Z"
                    },
                    "updated_at": "2015-08-14T12:34:43.136Z"
                },
                "_type": "synchronization_report_job",
                "sort": [
                    "queryelement"
                ]
            },
            {
                "_id": "10",
                "_index": "development_synchronization_report_jobs",
                "_score": null,
                "_source": {
                    "created_at": "2015-08-14T12:34:43.118Z",
                    "end_time": "2015-07-09T13:28:05.000Z",
                    "error": null,
                    "id": 10,
                    "job_id": 1,
                    "job_type": "select",
                    "name": "queryElement",
                    "start_time": "2015-07-09T13:28:05.000Z",
                    "synchronization_report": {
                        "created_at": "2015-08-14T12:34:43.105Z",
                        "id": 3,
                        "mode": "PULL",
                        "server": "LinuxServer",
                        "server_agent_number": 2,
                        "source_server": "LinuxServer",
                        "source_server_agent_number": 2,
                        "source_sync_id": "eecaf9a0-d715-49ba-be55-92cccd17b6d1",
                        "synch_agent_id": 1,
                        "synch_completed_time": "2015-07-09T13:28:05.000Z",
                        "updated_at": "2015-08-14T12:34:43.105Z"
                    },
                    "synchronization_report_data_area": {
                        "configuration_id": "users",
                        "created_at": "2015-08-14T12:34:43.111Z",
                        "error_text": null,
                        "error_type": null,
                        "id": 3,
                        "records_files_processed": 10,
                        "sql": null,
                        "status": "exported",
                        "synchronization_report_data_set_id": 2,
                        "updated_at": "2015-08-14T12:34:43.111Z"
                    },
                    "synchronization_report_data_area_id": 3,
                    "synchronization_report_data_set": {
                        "configuration_id": "users_and_passwords_server3",
                        "created_at": "2015-08-14T12:34:43.107Z",
                        "id": 2,
                        "synch_time": "2015-07-09T13:28:05.000Z",
                        "synchronization_report_id": 3,
                        "updated_at": "2015-08-14T12:34:43.107Z"
                    },
                    "updated_at": "2015-08-14T12:34:43.118Z"
                },
                "_type": "synchronization_report_job",
                "sort": [
                    "queryelement"
                ]
            },
            {
                "_id": "11",
                "_index": "development_synchronization_report_jobs",
                "_score": null,
                "_source": {
                    "created_at": "2015-08-14T12:34:43.121Z",
                    "end_time": "2015-07-09T13:28:05.000Z",
                    "error": null,
                    "id": 11,
                    "job_id": 2,
                    "job_type": "compress_data_files",
                    "name": "",
                    "start_time": "2015-07-09T13:28:05.000Z",
                    "synchronization_report": {
                        "created_at": "2015-08-14T12:34:43.105Z",
                        "id": 3,
                        "mode": "PULL",
                        "server": "LinuxServer",
                        "server_agent_number": 2,
                        "source_server": "LinuxServer",
                        "source_server_agent_number": 2,
                        "source_sync_id": "eecaf9a0-d715-49ba-be55-92cccd17b6d1",
                        "synch_agent_id": 1,
                        "synch_completed_time": "2015-07-09T13:28:05.000Z",
                        "updated_at": "2015-08-14T12:34:43.105Z"
                    },
                    "synchronization_report_data_area": {
                        "configuration_id": "users",
                        "created_at": "2015-08-14T12:34:43.111Z",
                        "error_text": null,
                        "error_type": null,
                        "id": 3,
                        "records_files_processed": 10,
                        "sql": null,
                        "status": "exported",
                        "synchronization_report_data_set_id": 2,
                        "updated_at": "2015-08-14T12:34:43.111Z"
                    },
                    "synchronization_report_data_area_id": 3,
                    "synchronization_report_data_set": {
                        "configuration_id": "users_and_passwords_server3",
                        "created_at": "2015-08-14T12:34:43.107Z",
                        "id": 2,
                        "synch_time": "2015-07-09T13:28:05.000Z",
                        "synchronization_report_id": 3,
                        "updated_at": "2015-08-14T12:34:43.107Z"
                    },
                    "updated_at": "2015-08-14T12:34:43.121Z"
                },
                "_type": "synchronization_report_job",
                "sort": [
                    null
                ]
            },
            {
                "_id": "15",
                "_index": "development_synchronization_report_jobs",
                "_score": null,
                "_source": {
                    "created_at": "2015-08-14T12:34:43.138Z",
                    "end_time": "2015-07-09T13:28:05.000Z",
                    "error": null,
                    "id": 15,
                    "job_id": 2,
                    "job_type": "compress_data_files",
                    "name": "",
                    "start_time": "2015-07-09T13:28:05.000Z",
                    "synchronization_report": {
                        "created_at": "2015-08-14T12:34:43.105Z",
                        "id": 3,
                        "mode": "PULL",
                        "server": "LinuxServer",
                        "server_agent_number": 2,
                        "source_server": "LinuxServer",
                        "source_server_agent_number": 2,
                        "source_sync_id": "eecaf9a0-d715-49ba-be55-92cccd17b6d1",
                        "synch_agent_id": 1,
                        "synch_completed_time": "2015-07-09T13:28:05.000Z",
                        "updated_at": "2015-08-14T12:34:43.105Z"
                    },
                    "synchronization_report_data_area": {
                        "configuration_id": "oldpasswords",
                        "created_at": "2015-08-14T12:34:43.129Z",
                        "error_text": null,
                        "error_type": null,
                        "id": 4,
                        "records_files_processed": 7,
                        "sql": null,
                        "status": "exported",
                        "synchronization_report_data_set_id": 2,
                        "updated_at": "2015-08-14T12:34:43.129Z"
                    },
                    "synchronization_report_data_area_id": 4,
                    "synchronization_report_data_set": {
                        "configuration_id": "users_and_passwords_server3",
                        "created_at": "2015-08-14T12:34:43.107Z",
                        "id": 2,
                        "synch_time": "2015-07-09T13:28:05.000Z",
                        "synchronization_report_id": 3,
                        "updated_at": "2015-08-14T12:34:43.107Z"
                    },
                    "updated_at": "2015-08-14T12:34:43.138Z"
                },
                "_type": "synchronization_report_job",
                "sort": [
                    null
                ]
            }
        ],
        "max_score": null,
        "total": 8
    },
    "timed_out": false,
    "took": 1
}

z tego co widzę w dokumentacji to sort powinno być tablicą - w Twoim przypadku jednoelementową, przykładowo:

sort: [ {"name": "asc" } ]

W przypadku gdy podam string zamiast listy elastiacsearch połapie się, że chce posortować po tym polu rosnąco. W aplikacji jednak stosuje zapis wspomniany przez ciebie. Więc na pewno to nie to :wink: Po prostu wstawiony przeze mnie wynik był na szybko generowany z konsoli aby pokazać, że coś jest nie tak. Martwi mnie null obecny w polu _score.

Tak jak podejrzewałem, Problem leżał w mappingu, którego nie miałem. Mianowicie domyślnie jeśli tekst wyszukiwany ma białe znaki t elasticsearch oddziela sobie po nich słowa sortuje je i dopiero sortuje tekst po tych słowach. Sugeruje to pole “sort”. A więc aby wszystko działało jak należy muszę poprawić domyślny mapping tak jak poniżej.

settings do
  mapping _all: {} do
    indexes :name, analyzer: 'keyword'
  end
end

Problem rozwiązany.