문제

2015-01-13 1 views
1

나는 다음과 같은 C#을 모델이 : YYYY-MM-dd'T'HH :문제

[ElasticType(Name = "myType")] 
public class MyType 
{ 
    ... 
    [ElasticProperty(Name = "ElasticId")] 
    [DataMember(Name = "ElasticId")] 
    public string ElasticId { get; set; } 
    ... 
    [ElasticProperty(Name = "DateToBeUsed", Type = FieldType.Date, DateFormat = "date_hour_minute_second_millis")] 
    public string DateToBeUsed { get; set; } 
    ... 
} 

은 "date_hour_minute_second_millis는"형식을 다음에 해당 mm를 : ss.SSS (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-date-format.html)

ES가 둥지는 "지도"방법과 그에 해당하는 사용하여 수행 매핑은 :

"mappings": { 
    "myType": { 
     "properties": { 
      ..., 
      "ElasticId": { 
       "type": "string" 
      }, 
      ..., 
      "DateToBeUsed": { 
       "type": "date", 
       "format": "date_hour_minute_second_millis" 
      }, 
      ... 
     } 
    } 
} 

나는이 공업 내부에 문서를 삽입 예 :

"_source": { 
     ..., 
     "ElasticId": "2", 
     ..., 
     "DateToBeUsed": "2012-05-21T09:51:34.073", 
     ... 
     } 

내 문제는 Nest를 통해이 개체를 검색하려고 할 때입니다.

DateToBeUsed의 값은 항상 다음과 같은 형식으로 포맷

: MM/DD/YYYY의 HH : MM : SS (예 : 2012년 5월 21일 9시 51분 34초)

(사용 감의 값은 형식이 잘 지정되어 있습니다.)

1 °입니까?

내가 ES에 준 것과 동일한 날짜 형식을 검색해야합니다. (매핑에 설명 된 것과 동일한 형식을 사용하는 것이 정상적이어야한다고 생각합니다.)

2 °)이 문제를 해결할 수있는 "깨끗한"솔루션이 있습니까? (문서를 가져온 후 날짜를 다시 포맷하면 "깨끗한"해결책이 아닙니다 ...)

답변 해 주셔서 감사합니다! 안녕하세요.

나는 당신이보고 다음 코드를 사용하지만, 예상대로 날짜 값이 Get 호출에서 반환되고있는 것을 재현하기 위해 노력했습니다

답변

1

:

string indexName = "so-27927069"; 

// --- create index --- 
client.CreateIndex(cid => cid.Index(indexName)); 
Console.WriteLine("created index"); 

// --- define map --- 
client.Map<MyType>(m => m 
    .Index(indexName) 
    .Type("myType") 
    .MapFromAttributes()); 
Console.WriteLine("set mapping"); 

// ---- index ----- 
client.Index<MyType>(
    new MyType 
    { 
     DateToBeUsed = new DateTime(2012, 5, 21, 9, 51, 34, 73) 
      .ToString("yyyy-MM-ddThh:mm:ss.fff"), 
     ElasticId = "2" 
    }, 
    i => i 
     .Index(indexName) 
     .Type("myType") 
     .Id(2) 
); 
Console.WriteLine("doc indexed"); 

// ---- get ----- 
var doc = client.Get<MyType>(i => i 
     .Index(indexName) 
     .Type("myType") 
     .Id(2) 
    ); 

Console.WriteLine(); 
Console.WriteLine("doc.Source.DateToBeUsed: "); 
Console.WriteLine(doc.Source.DateToBeUsed); 
Console.WriteLine(); 
Console.WriteLine("doc.RequestInformation.ResponseRaw: "); 
Console.WriteLine(Encoding.UTF8.GetString(doc.RequestInformation.ResponseRaw)); 

을 나는 다음과 같은 결과를보고 있어요 출력 :

created index 
set mapping 
doc indexed 

doc.Source.DateToBeUsed: 
2012-05-21T09:51:34.073 

doc.RequestInformation.ResponseRaw: 
{"_index":"so-27927069","_type":"myType","_id":"2","_version":1,"found":true,"_source":{ 
    "ElasticId": "2", 
    "DateToBeUsed": "2012-05-21T09:51:34.073" 
}} 

(필자는 ResponseRaw 값과 Get 요청에 대한 응답의 페이로드 사이의 일치를보고 있어요, Fiddler를 통해 트래픽을보고.)

나는 Elasticsearch 1.5.2 버전과 NEST 1.6.0 버전을 사용 중입니다. (어쩌면 당신이보고 있던 문제는 중간에 언젠가 고쳐졌습니다 ....)

+0

답변 주셔서 감사합니다 :) 나는 네스트 1.4.1 및 ES 1.4.x에있었습니다. 어쩌면 문제가 해결되었거나 뭔가 잘못하고 있었어 .. 어쨌든, 감사합니다 :) –