我刚打酱油去了,不好意思

ES问题:JAVA排序时如何设置Fielddata

Elasticsearch | 作者 yuan327159409 | 发布于2017年03月21日 | 阅读数:20274

错误描述:
Caused by: java.lang.IllegalArgumentException: Fielddata is disabled on text fields by default. Set fielddata=true on [orderCode] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.
at org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:336)
at org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:111)
at org.elasticsearch.index.query.QueryShardContext.getForField(QueryShardContext.java:165)
at org.elasticsearch.search.sort.FieldSortBuilder.build(FieldSortBuilder.java:277)
at org.elasticsearch.search.sort.SortBuilder.buildSort(SortBuilder.java:156)
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:688)
at org.elasticsearch.search.SearchService.createContext(SearchService.java:540)
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:516)
at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:251)
at org.elasticsearch.action.search.SearchTransportService$6.messageReceived(SearchTransportService.java:298)
at org.elasticsearch.action.search.SearchTransportService$6.messageReceived(SearchTransportService.java:295)
at org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor$ProfileSecuredRequestHandler$1.doRun(SecurityServerTransportInterceptor.java:237)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
at org.elasticsearch.common.util.concurrent.EsExecutors$1.execute(EsExecutors.java:109)
at org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor$ProfileSecuredRequestHandler.lambda$messageReceived$0(SecurityServerTransportInterceptor.java:289)
at org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor$ProfileSecuredRequestHandler$$Lambda$1538/2013783967.accept(Unknown Source)
at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:56)
at org.elasticsearch.xpack.security.transport.ServerTransportFilter$NodeProfile.lambda$null$2(ServerTransportFilter.java:164)
at org.elasticsearch.xpack.security.transport.ServerTransportFilter$NodeProfile$$Lambda$1546/895003706.accept(Unknown Source)
at org.elasticsearch.xpack.security.authz.AuthorizationUtils$AsyncAuthorizer.maybeRun(AuthorizationUtils.java:127)
at org.elasticsearch.xpack.security.authz.AuthorizationUtils$AsyncAuthorizer.setRunAsRoles(AuthorizationUtils.java:121)
at org.elasticsearch.xpack.security.authz.AuthorizationUtils$AsyncAuthorizer.authorize(AuthorizationUtils.java:109)
at org.elasticsearch.xpack.security.transport.ServerTransportFilter$NodeProfile.lambda$inbound$3(ServerTransportFilter.java:166)
at org.elasticsearch.xpack.security.transport.ServerTransportFilter$NodeProfile$$Lambda$1540/1721144311.accept(Unknown Source)
at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:56)
at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$authenticateAsync$0(AuthenticationService.java:182)
at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator$$Lambda$1542/1882373383.accept(Unknown Source)
at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lambda$lookForExistingAuthentication$2(AuthenticationService.java:201)
at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator$$Lambda$1543/1075989798.run(Unknown Source)
at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.lookForExistingAuthentication(AuthenticationService.java:213)
at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.authenticateAsync(AuthenticationService.java:180)
at org.elasticsearch.xpack.security.authc.AuthenticationService$Authenticator.access$000(AuthenticationService.java:142)
at org.elasticsearch.xpack.security.authc.AuthenticationService.authenticate(AuthenticationService.java:114)
at org.elasticsearch.xpack.security.transport.ServerTransportFilter$NodeProfile.inbound(ServerTransportFilter.java:142)
at org.elasticsearch.xpack.security.transport.SecurityServerTransportInterceptor$ProfileSecuredRequestHandler.messageReceived(SecurityServerTransportInterceptor.java:296)
at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:69)
at org.elasticsearch.transport.TransportService$7.doRun(TransportService.java:610)
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:596)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
已邀请:

kennywu76 - Wood

赞同来自: mn_1127

遇到这个错误是因为你尝试对一个text类型的字段做排序,而text类型的字段是要分词的。 一来词典很大,性能会很差;二来排序结果是词典里的词,而并非整个text的内容。 出于这2点原因,ES5.x以后对于text类型默认禁用了fielddata,防止对text字段一些错误的操作(排序,聚合,script)而给heap造成很大的压力。
 
如果一定有对该字段按照文本字母序做排序的需求,可以将该字段定义为multi-filed,例如:


PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "city": {
          "type": "text",
          "fields": {
            "raw": { 
              "type":  "keyword"
            }
          }
        }
      }
    }
  }
}


上面的city是text类型,适合做全文搜索,然后排序的时候可以用其keyword类型即city.raw。  这样排序结果是正确的,并且keyword字段是通过doc values排序的,内存消耗远小于fielddata。

yuan327159409 - 90后开发

赞同来自:

敢问楼上,有java相关的说明文档么!

要回复问题请先登录注册