Сортировка Elasticsearch с использованием API Java - PullRequest
0 голосов
/ 18 сентября 2018

Я пытаюсь отсортировать свой документ на основе поля totalEmployee, где индекс index_db , тип отделов и поле totalEmployee

QueryBuilder qb = QueryBuilders.matchAllQuery();
            SearchResponse response = client.prepareSearch("index_db").setTypes("departments")
                    .addSort(SortBuilders.fieldSort("totalEmployee").order(SortOrder.ASC)).setQuery(qb)
                    .setSize(100).execute().actionGet();
            for(SearchHit hits :response.getHits()) {
                System.out.print("id = "+hits.getId());
                System.out.println(hits.getSourceAsString());
            }

Но я получаю ошибку:

Exception in thread "main" Failed to execute phase [query], all shards failed; shardFailures {[q9B7Qs-DSXWC14pjc5zlNg][index_db][0]: RemoteTransportException[[elasticsearch_node][10.64.216.92:9375][indices:data/read/search[phase/query]]]; nested:

IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [totalEmployee] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.]; }{[q9B7Qs-DSXWC14pjc5zlNg][index_db][1]: RemoteTransportException[[elasticsearch_node][10.64.216.92:9375][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [totalEmployee] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.]; }{[q9B7Qs-DSXWC14pjc5zlNg][piyush_db][2]: RemoteTransportException[[elasticsearch_node][10.64.216.92:9375][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [totalEmployee] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.]; }{[q9B7Qs-DSXWC14pjc5zlNg][piyush_db][3]: RemoteTransportException[[elasticsearch_node][10.64.216.92:9375][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [totalEmployee] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.]; }{[q9B7Qs-DSXWC14pjc5zlNg][piyush_db][4]: RemoteTransportException[[elasticsearch_node][10.64.216.92:9375][indices:data/read/search[phase/query]]]; nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [totalEmployee] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.]; }

Ответы [ 2 ]

0 голосов
/ 26 сентября 2018

Так как поле totalEmployee было проанализировано, значение данных поля эластичных значений равно true для выполнения операции сортировки. Вместо этого вы можете использовать totalEmployee.keyword для достижения желаемых результатов.

Ниже приведен рабочий код.

QueryBuilder qb = QueryBuilders.matchAllQuery();

SearchResponse response = client.prepareSearch("index_db").setTypes("departments")
                    .addSort(SortBuilders.fieldSort("totalEmployee.keyword")
                    .order(SortOrder.ASC)).setQuery(qb)
                    .setSize(100).execute().actionGet();

for(SearchHit hits : response.getHits())
{
     System.out.print("id = " + hits.getId());
     System.out.println(hits.getSourceAsString());
}
0 голосов
/ 26 сентября 2018

Наконец-то я получил ответ на свой запрос.Проблема была связана с динамическим отображением во время индексации в Elasticsearch.Лучше использовать собственное сопоставление и выполнять сортировку по типу поля long (в моем случае я пытался сортировать по типу поля keyword (string)).

...