fielddata es true
时间: 2023-10-03 08:03:37 浏览: 97
在 Elasticsearch 中,fielddata 代表着一个字段的值是否被加载到内存中以进行聚合、排序等操作。当一个字段被标记为 fielddata=true 时,Elasticsearch 会将该字段的所有值加载到内存中,以便在后续的聚合、排序等操作中快速访问。但是,开启 fielddata 也会带来一些负面影响,如占用更多的内存资源、导致查询速度下降等。因此,应该根据具体的业务场景和数据规模,慎重考虑是否开启 fielddata。
相关问题
用java实现fielddata=true
好的,以下是Java代码实现:
```java
SearchRequest searchRequest = new SearchRequest("my_index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(0);
TermsAggregationBuilder aggregationBuilder = AggregationBuilders.terms("my_agg")
.field("my_field")
.fielddata(true);
searchSourceBuilder.aggregation(aggregationBuilder);
searchRequest.source(searchSourceBuilder);
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
try {
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
// 处理搜索结果
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
```
这段代码使用Elasticsearch Java客户端实现了一个搜索请求,其中包含一个`terms`聚合操作,并且启用了字段数据(`fielddata=true`)。可以根据具体的需求调整搜索条件和聚合操作。
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Fielddata is disabled on text fields by default. Set fielddata=true on [category] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.]]; nested: ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Fielddata is disabled on text fields by default. Set fielddata=true on [category] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.]];] with root cause
这个异常信息是关于 Elasticsearch 的,大致意思是:在执行搜索时,所有分片都失败了。原因是在查询中使用了文本字段(text fields)的聚合(aggregation)功能,但是默认情况下,文本字段是禁用了 Fielddata(一个 ElasticSearch 中的内存结构,用于支持聚合、排序等操作)的。要解决这个问题,可以在查询中设置 `fielddata=true`,或者使用关键字字段(keyword field)代替文本字段。
阅读全文