# Elasticsearch 关于store字段的处理

**URL:** <https://discuss.elastic.co/t/elasticsearch-store/66236>\
**Category:** 中文提问与讨论\
**Created:** [November 16, 2016, 10:08am UTC](https://discuss.elastic.co/t/elasticsearch-store/66236 "2016-11-16T10:08:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sshling](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sshling/32/24052_2.png) [@sshling](https://discuss.elastic.co/u/sshling)\
**Post date:** [November 16, 2016, 10:08am UTC](https://discuss.elastic.co/t/elasticsearch-store/66236/1 "2016-11-16T10:08:26Z")

</div>

环境：ES2.1.2 kibana4.3.3

问题：  
有一个文档的时间字段设置如下：

```
"time": {
    "format": "epoch_millis",
    "store": true,
    "type": "date"
  }

```

即 store属性设置为true

当使用kibana 检索这个文档的时候，请求的查询语句，摘录部分如下：

```
{
  "fields": [
    "*",
    "_source"
  ],
  "fielddata_fields": [
    "time"
  ]
}

```

这样查询返回的结果，摘录部分如下：

```
"time": [
    "1479144300000",
     1479144300000
  ]

```

即，同个值出现了2次，这样kibana就无法解析了，

原因：个人认为是ES的bug，经测试es最新版本5.1也是这样。

因为是设置为store=true，所以fields子句查询出来了一个值，  
随后通过fielddata\_fields也查到了这个值，在ES源码中是List数据结构，所以就放了2次，  
其实2个相同的值，放2次完全没有意义。

跟踪的源代码是ES 2.1.2版本的，定位代码：  
`org.elasticsearch.search.fetch.fielddata.FieldDataFieldsFetchSubPhase#hitExecute`

```
public void hitExecute(SearchContext context, HitContext hitContext) {
  for (FieldDataFieldsContext.FieldDataField field : context.getFetchSubPhaseContext(CONTEXT_FACTORY).fields()) {
      if (hitContext.hit().fieldsOrNull() == null) {
          hitContext.hit().fields(new HashMap<String, SearchHitField>(2));
      }
      SearchHitField hitField = hitContext.hit().fields().get(field.name());
      if (hitField == null) {
          hitField = new InternalSearchHitField(field.name(), new ArrayList<>(2));
          hitContext.hit().fields().put(field.name(), hitField);
      }
      MappedFieldType fieldType = context.mapperService().smartNameFieldType(field.name());
      if (fieldType != null) {
          AtomicFieldData data = context.fieldData().getForField(fieldType).load(hitContext.readerContext());
          ScriptDocValues values = data.getScriptValues();
          values.setNextDocId(hitContext.docId());
          hitField.values().addAll(values.getValues());//问题在这里，我觉得应该去重
      }
  }
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 14, 2016, 10:08am UTC](https://discuss.elastic.co/t/elasticsearch-store/66236/2 "2016-12-14T10:08:40Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
