我正在尝试根据totalEmployee字段对文档进行排序,其中index为 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.]; }

最佳答案

当totalEmployee被分析字段时, flex 将字段数据值查询为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());
}

10-07 14:30