asticsearch сортирует только по нескольким полям и выдает ошибку другим - PullRequest
0 голосов
/ 29 марта 2020

У меня есть эта структура данных

{
  "id": "1777410299",
  "name": "Фий",
  "moniker": "Страна Россия, Республика Дагестан, Район Ахтынский, Село Фий",
  "fias": "b8cfa34e-465b-41c0-af72-6c03d3e08fdd",
  "macrocity": null,
  "hasMetro": false,
  "shopLink": "/stores/1777410299",
  "urlCode": "1777410299",
  "receivingMethods": [],
  "geoPoint": {
    "lat": 41.28528,
    "lon": 47.565
  },
  "kind": "Село",
  "kindShort": "с",
  "hierarchyLevels": {
    "region": {
      "id": "1775690299",
      "name": "Дагестан",
      "kind": "Республика",
      "kindShort": "Респ"
    },
    "district": {
      "id": "1777270299",
      "name": "Ахтынский",
      "kind": "Район",
      "kindShort": "р-н"
    },
    "city": null,
    "settlement": null
  }
}

Мне нужно отсортировать по долготе и широте, и первое, что будет элементом, ближайшим к координатам, которые я получил

SearchRequest searchRequest = new SearchRequest()
                                    .source(SearchSourceBuilder.
                                            searchSource()
                                            .query(sourceQuery)
                                            .sort(SortBuilders.geoDistanceSort("geoPoint", lat, lon))
                                    );

Этот код выдает эту ошибку

ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]; nested: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]]; nested: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]];
    at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177)
    at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1406)
    at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1382)
    at org.elasticsearch.client.RestHighLevelClient$1.onFailure(RestHighLevelClient.java:1357)
    at org.elasticsearch.client.RestClient$FailureTrackingResponseListener.onDefinitiveFailure(RestClient.java:844)
    at org.elasticsearch.client.RestClient$1.completed(RestClient.java:548)
    at org.elasticsearch.client.RestClient$1.completed(RestClient.java:529)
    at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:122)
    at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:181)
    at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:448)
    at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:338)
    at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265)
    at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81)
    at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39)
    at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315)
    at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276)
    at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104)
    at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:591)
    at java.lang.Thread.run(Thread.java:748)
    Suppressed: org.elasticsearch.client.ResponseException: method [POST], host [http://elastic-geo-1.geogatetst.marathon.mesos.sportmaster.ru:9200], URI [/territory/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 500 Internal Server Error]
{"error":{"root_cause":[{"type":"class_cast_exception","reason":"org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"territory_20200318_023324","node":"eE2gbidUQNiHlq8TxbieGA","reason":{"type":"class_cast_exception","reason":"org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData"}}],"caused_by":{"type":"class_cast_exception","reason":"org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData","caused_by":{"type":"class_cast_exception","reason":"org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData"}}},"status":500}
        at org.elasticsearch.client.RestClient$1.completed(RestClient.java:540)
        ... 16 more
Caused by: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]]; nested: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]];
    at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:509)
    at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:420)
    at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:450)
    at org.elasticsearch.ElasticsearchException.failureFromXContent(ElasticsearchException.java:616)
    at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:169)
    ... 21 more
Caused by: ElasticsearchException[Elasticsearch exception [type=class_cast_exception, reason=org.elasticsearch.index.fielddata.plain.SortedNumericDVIndexFieldData cannot be cast to org.elasticsearch.index.fielddata.IndexGeoPointFieldData]]
    at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:509)
    at org.elasticsearch.ElasticsearchException.fromXContent(ElasticsearchException.java:420)
    at org.elasticsearch.ElasticsearchException.innerFromXContent(ElasticsearchException.java:450)
    ... 25 more

Также, если я пытаюсь сортировать по имени поля (сортирует только по id и коду URL)

             SearchRequest searchRequest = new SearchRequest()
                                    .source(SearchSourceBuilder.
                                            searchSource()
                                            .query(sourceQuery)
                                            .sort("geoPoint.lat")
                                    );

Этот код работает хорошо

Почему это так? Что мне не хватает? И как мне это исправить?

1 Ответ

1 голос
/ 29 марта 2020

Вы должны определить свойство geoPoint как geo_point в вашем отображении:

PUT deen
{
  "mappings": {
    "properties": {
      "geoPoint": {
        "type": "geo_point"
      }
    }
  }
}
PUT deen/_doc/1
{"id":"1777410299","name":"Фий","moniker":"Страна Россия, Республика Дагестан, Район Ахтынский, Село Фий","fias":"b8cfa34e-465b-41c0-af72-6c03d3e08fdd","macrocity":null,"hasMetro":false,"shopLink":"/stores/1777410299","urlCode":"1777410299","receivingMethods":[],"geoPoint":{"lat":41.28528,"lon":47.565},"kind":"Село","kindShort":"с","hierarchyLevels":{"region":{"id":"1775690299","name":"Дагестан","kind":"Республика","kindShort":"Респ"},"district":{"id":"1777270299","name":"Ахтынский","kind":"Район","kindShort":"р-н"},"city":null,"settlement":null}}
GET deen/_search
{
  "sort": [
    {
      "_geo_distance": {
        "geoPoint": {
          "lat": 41.28528,
          "lon": 47.565
        },
        "order": "asc"
      } 
    }
  ]
}

Когда оно не проиндексировано как geo_point, вы не допускается _geo_distance на него, как говорит ваше исключение.

...