# Elasticsearch-net 5.0RC4でAttachmentの一部が結果に格納されない

**URL:** <https://discuss.elastic.co/t/elasticsearch-net-5-0rc4-attachment/68133>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [December 6, 2016, 7:51am UTC](https://discuss.elastic.co/t/elasticsearch-net-5-0rc4-attachment/68133 "2016-12-06T07:51:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [December 6, 2016, 7:51am UTC](https://discuss.elastic.co/t/elasticsearch-net-5-0rc4-attachment/68133/1 "2016-12-06T07:51:24Z")

</div>

# 概要

検索結果のjsonには含まれているのに、elasticsearch-nestでアクセスした場合、AttachmentのTitleに値が格納されていない。  
取得するのに必要な設定があれば教えてほしい。

# 試した環境

- ElasticSearch 5.0.1 (公式のDockerイメージを利用）
- .NETCore 1.1
- NEST 5.0.0 RC4

# 再現用情報

## ElasticSearchに入れたデータと手順

```auto
DELETE documents
POST documents/document/1
{
    "id" : 1,
    "path": "/a/b/c/d/",
    "attachment":{
      "author": "作者",
      "content": "I have a pen. I have an apple",
      "content_length": 12345,
      "content_type": "plain/txt",
      "date" : "2016-12-06T12:34:56Z",
      "language": "ja",
      "name": "名前",
      "title": "タイトル",
      "keywords": "キーワード",
      "indexed_chars": 12345,
      "detect_language" : "ja"
    }
}

```

## 検索クライアント側

Documentクラスは、以下のブログを参考にした。Futureのところ。

> **[The Future of Attachments for Elasticsearch and .NET](https://www.elastic.co/blog/the-future-of-attachments-for-elasticsearch-and-dotnet)**
>
> Working with attachments using NEST, the official .NET Elasticsearch high level client.

  
マッピングの設定をしなくても再現しているため、上のデータ投入手順では省きました。

```auto
public class Document
{
  public int Id { get; set; }
  public string Path { get; set; }
  public string Content { get; set; }
  public Attachment Attachment { get; set; }
}

```

そして、検索してDocumentを取得する

```auto
var connectionSettings = new ConnectionSettings(
                             new Uri("http://xxx.xxx.xxx.xxx:9200"))
                             .DisableDirectStreaming();
var client = new ElasticClient(connectionSettings);
var response = client .Get<Document>(1, idx => idx.Index("documents"));

Console.WriteLine(response.DebugInformation);

var document = response.Source;

```

標準出力に検索結果として取得されたJSONが以下のように表示され、Titleも取得できていますが、  
documentの中のAttachmentのTitleはNullのになっています。

```auto
# Response:
{"_index":"documents","_type":"document","_id":"1","_version":1,"found":true,"_source":{
    "id" : 1,
    "path": "/a/b/c/d/",
    "attachment":{
      "author": "作者",
      "content": "I have a pen. I have an apple",
      "content_length": 12345,
      "content_type": "plain/txt",
      "date" : "2016-12-06T12:34:56Z",
      "language": "ja",
      "name": "名前",
      "title": "タイトル",
      "keywords": "キーワード",
      "indexed_chars": 12345,
      "detect_language" : "ja"
    }
  
}
}

```

検索結果として取得したマッピングされたDocumentオブジェクトの中身を見たとき、Title [string] : null となっています。

![](https://us1.discourse-cdn.com/elastic/original/2X/d/d461da75e89defd48048f3a9f737c3dba5b80014.png)

# IMHO

[Githubのレポジトリ](https://github.com/elastic/elasticsearch-net/blob/master/src/Nest/Mapping/Types/Specialized/Attachment/Attachment.cs)で、  
取得したJSONからAttachmentを生成しているところがありますが、ここのswitch case文でtitleを判定していないので、  
何も入らないのでは？ と感じた次第です。

---

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [December 6, 2016, 9:21am UTC](https://discuss.elastic.co/t/elasticsearch-net-5-0rc4-attachment/68133/2 "2016-12-06T09:21:44Z")

</div>

# 補足

Nest/Mapping/Types/Specialized/Attachment.csの160行目前後にtitleのときの追加を行うと、結果に含まれるようになりました。

```auto
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//省略
	if (reader.TokenType == JsonToken.PropertyName)
	{
		var propertyName = (string)reader.Value;
		switch (propertyName.ToLowerInvariant())
	 	{
                        /* add start*/
        		case "title":
				attachment.Title = reader.ReadAsString();
				break;
                        /* add end*/
	                case "_content":
			case "content":
//以下略

```

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [December 6, 2016, 10:04am UTC](https://discuss.elastic.co/t/elasticsearch-net-5-0rc4-attachment/68133/3 "2016-12-06T10:04:23Z")

</div>

検証ありがとうございます！  
GitHubにIssueを上げていただけると助かります。

> **[elastic/elasticsearch-net](https://github.com/elastic/elasticsearch-net/issues)**
>
> elasticsearch-net - Elasticsearch.Net & NEST

---

<div class="post-metadata">

**Author:** ![tsgkdt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsgkdt/32/39151_2.png) [@tsgkdt](https://discuss.elastic.co/u/tsgkdt)\
**Post date:** [December 7, 2016, 5:04am UTC](https://discuss.elastic.co/t/elasticsearch-net-5-0rc4-attachment/68133/4 "2016-12-07T05:04:29Z")

</div>

Google翻訳の手を借りて登録しておきました。

> <https://github.com/elastic/elasticsearch-net/issues/2466>

ユニットテスト/Integrateテストの書き方が今一つわからなかったので、テスト/PRは出すに至らずです。

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [December 7, 2016, 5:35am UTC](https://discuss.elastic.co/t/elasticsearch-net-5-0rc4-attachment/68133/5 "2016-12-07T05:35:35Z")

</div>

ありがとうございます！

---

<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:** [January 4, 2017, 5:35am UTC](https://discuss.elastic.co/t/elasticsearch-net-5-0rc4-attachment/68133/6 "2017-01-04T05:35:39Z")

</div>

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