使用java API进行Elasticsearch排序

oxcyiej7  于 2023-11-17  发布在  ElasticSearch
关注(0)|答案(3)|浏览(160)

我尝试根据totalEmployee字段对文档进行排序,其中index为index_db,type为departments,a字段为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());
            }

字符串
但我犯了错误:
第一个月
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.]; }

c2e8gylq

c2e8gylq1#

由于分析的是totalEmployee字段,弹性要求字段数据值为true以执行排序操作。相反,您可以使用totalEmployee.关键字来实现所需的结果。
下面是工作代码。

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

字符串

fafcakar

fafcakar2#

最后,我得到了我上面查询的答案。问题是由于Elasticsearch中索引过程中的动态Map。最好使用自己的Map并对long类型字段进行排序(在我的情况下,我试图根据keyword(string)字段类型进行排序)。

xjreopfe

xjreopfe3#

您可以在搜索请求中指定排序选项:

SearchRequest request = SearchRequest.of(req -> req
  .index(indexName)
  .query(query)
  .sort(SortOptions.of(so -> so
    .field(f -> f
      .field(sortField)
      .order(SortOrder.Asc)
    )
  ))
);

字符串

相关问题