2017-03-10 7 views
1

I이 질문은 2 년 전부터 Index a dynamic object using NEST에서 발견되었습니다.NEST 5.0을 사용하여 동적 객체를 색인화하십시오.

기본적으로 정확하게 동일한 질문이지만 NEST 5.0을 사용합니다. 제안 된 해결책은 최신 버전에서 더 이상 작동하지 않습니다. 다음 인덱스 오브젝트와 캐스팅

  • 소스 태그
  • 내의 필드를 갖는 elasticsearch 문서 결과가 esClient.Raw.Index API는 동적 유형 작업
+0

당신이받을 수 있나요으로 링크 된 질문에 비슷한 작업을 수행 할 수 있습니다 오작동/오류 메시지? 그것을 해결하기 위해 뭔가를 시도해 봤어? [ask]를 읽으십시오. –

답변

2

누락되어 유사 NEST 1.x에서와 같이 NEST 5.x; 클라이언트 API 중 일부는이 버전들 사이에 약간 변경되었지만 전제는 여전히 동일합니다.

다음은 이에 대한

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200")); 
var defaultIndex = "default-index"; 
var connectionSettings = new ConnectionSettings(pool) 
    .DefaultIndex(defaultIndex); 

var client = new ElasticClient(connectionSettings); 

// delete the index if it already exists 
if (client.IndexExists(defaultIndex).Exists) 
    client.DeleteIndex(defaultIndex); 

client.CreateIndex(defaultIndex); 

// create an anonymous type assigned to a dynamically typed variable 
dynamic instance = new 
{ 
    Name = "Russ", 
    CompanyName = "Elastic", 
    Date = DateTimeOffset.UtcNow 
}; 

// cast the instance to object to index, explicitly 
// specify the document type and index 
var indexResponse = client.Index((object)instance, i => i 
    .Type("my_type") 
    .Index(defaultIndex) 
); 

// fetch the document just indexed 
var getResponse = client.Get<dynamic>(indexResponse.Id, g => g 
    .Type(indexResponse.Type) 
    .Index(indexResponse.Index) 
); 

요청 및 응답 JSON은

HEAD http://localhost:9200/default-index?pretty=true 

Status: 200 

------------------------------ 

DELETE http://localhost:9200/default-index?pretty=true 

Status: 200 
{ 
    "acknowledged" : true 
} 

------------------------------ 

PUT http://localhost:9200/default-index?pretty=true 
{} 

Status: 200 
{ 
    "acknowledged" : true, 
    "shards_acknowledged" : true 
} 

------------------------------ 

POST http://localhost:9200/default-index/my_type?pretty=true 
{ 
    "name": "Russ", 
    "companyName": "Elastic", 
    "date": "2017-03-11T04:03:53.0561954+00:00" 
} 

Status: 201 
{ 
    "_index" : "default-index", 
    "_type" : "my_type", 
    "_id" : "AVq7iXhpc_F3ya7MTJiU", 
    "_version" : 1, 
    "result" : "created", 
    "_shards" : { 
    "total" : 2, 
    "successful" : 1, 
    "failed" : 0 
    }, 
    "created" : true 
} 

------------------------------ 

GET http://localhost:9200/default-index/my_type/AVq7iXhpc_F3ya7MTJiU?pretty=true 

Status: 200 
{ 
    "_index" : "default-index", 
    "_type" : "my_type", 
    "_id" : "AVq7iXhpc_F3ya7MTJiU", 
    "_version" : 1, 
    "found" : true, 
    "_source" : { 
    "name" : "Russ", 
    "companyName" : "Elastic", 
    "date" : "2017-03-11T04:03:53.0561954+00:00" 
    } 
} 

------------------------------ 

이 예상대로 문서가 색인되고 원래 소스 문서를 검색 할 수 있다는 것을 보여줍니다 같이 예입니다.

낮은 수준의 클라이언트

.LowLevel 속성을 통해 NEST 2.x 및 5.x를에서 높은 수준의 클라이언트에서 액세스 할 수 있습니다, 그래서 당신은

dynamic instance = new 
{ 
    Id = "id", 
    Index = defaultIndex, 
    Type = "my_type", 
    Document = new 
    { 
     Name = "Russ", 
     CompanyName = "Elastic", 
     Date = DateTimeOffset.UtcNow 
    } 
}; 

string documentJson = client.Serializer.SerializeToString((object)instance.Document); 

var result = client.LowLevel.Index<string>(instance.Index, instance.Type, instance.Id, documentJson); 
+0

많은 grattitude – Sixx