fielddata es true
时间: 2023-10-03 19:03:37 浏览: 55
在 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`)。可以根据具体的需求调整搜索条件和聚合操作。
在启动elasticsearch集群时第一台机子启动elasticsearch后报这条错误java.lang.IllegalArgumentException: Fielddata is disabled on text fields by default. Set fielddata=true on [type] 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.
这个错误提示是因为你在使用text类型的字段上启用了聚合(aggregation)或排序(sorting)等需要使用fielddata的功能。fielddata是一种用于在内存中加载倒排索引数据的机制,但它需要占用相当大的内存。
解决这个问题的方法有两个:
1. 在mapping中将该字段的fielddata设置为true,例如:
```
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"my_field": {
"type": "text",
"fielddata": true
}
}
}
}
}
```
2. 使用keyword类型的字段进行聚合或排序,例如:
```
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"my_field": {
"type": "keyword"
}
}
}
}
}
```
这样做的话,你就可以在使用聚合或排序功能时避免使用fielddata。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)