2017-03-08 70 views
0

我正在從ES 1.7遷移到5.2並重構以下代碼。Elasticsearch Java API遷移形式TermsFacetBuilder with AggregationBuilders

ES 1.7

public void prepare(final SearchRequestBuilder searchRequestBuilder) { 
     final TermsFacetBuilder labelsFacet = FacetBuilders 
       .termsFacet("labels") 
       .field("labels"); 
     searchRequestBuilder.addFacet(labelsFacet); 
     searchRequestBuilder.setFrom(start); 
     searchRequestBuilder.setSize(size); 
    } 

與ES 5.2

public void prepare(final SearchRequestBuilder searchRequestBuilder) { 
     TermsAggregationBuilder aggregation = AggregationBuilders.terms("labels").field("labels"); 
     searchRequestBuilder.addAggregation(aggregation); 
     searchRequestBuilder.setFrom(start); 
     searchRequestBuilder.setSize(size); 
    } 

當我火了搜索查詢,我收到以下異常

nested: IllegalArgumentException[Fielddata is disabled on text fields by default. Set fielddata=true on [labels] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.] 

我也試着設置上SearchRequestBuilder的fieldDocValue但沒有運氣。

+0

您是否在標籤上設置了fielddata = true,如錯誤消息所示? – Jeremy

+0

@Jeremy,我們如何設置它? – Santosh

回答

1

根據錯誤消息,您需要在您的映射中使用enable fielddata

例如,以下是text字段中使用fielddata=true的新映射。

PUT my_index/_mapping/my_type 
{ 
    "properties": { 
    "my_field": { 
     "type":  "text", 
     "fielddata": true 
    } 
    } 
} 
+0

謝謝@Jeremy。其實,我想用Java API來設置它。我試圖搜索ES文檔,但沒有找到。 – Santosh