简体   繁体   English

使用Java API进行Elasticsearch排序

[英]Elasticsearch sorting using java API

I am trying to sort my document based on totalEmployee field, where index is index_db , the type is departments and a field is totalEmployee 我正在尝试根据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());
            }

But I am getting error : 但是我得到了错误:

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.]; }

Finally , I got the answer of my above query. 最后,我得到了上面查询的答案。 The problem was due to dynamic mapping during indexing in Elasticsearch. 问题是由于在Elasticsearch中建立索引期间的动态映射所致。 Better use your own mapping and do sorting on long type field (In my case I was trying to sort on the basis of keyword (string) field type). 更好地使用自己的映射并在类型字段上进行排序(在我的情况下,我试图根据关键字 (字符串)字段类型进行排序)。

As totalEmployee was analyzed field, elastic requries field data value as true to perform sort operation. 当totalEmployee被分析字段时,弹性将字段数据值查询为true以执行排序操作。 Instead you can use totalEmployee.keyword to achieve desired results. 相反,您可以使用totalEmployee.keyword获得所需的结果。

Below is the working code. 以下是工作代码。

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM