【发布时间】:2018-07-08 05:26:06
【问题描述】:
美好的一天:
我的 POCO 在 NEST 的搜索中遇到问题。目前我可以搜索序列化到我的 POCO 的文档,但是我的 id 属性为空:
IMultiSearchResponse responses = await this._elasticClient.MultiSearchAsync(ms => ms.Search<Facility>("FacilityWithReviews",s =>
s.Query(q => q.Nested(n => n.Path(p => p.Reviews)
.Query(nq => nq.Term(t => t.Field(f=>f.Reviews.First().Id).Value(review.Id.ToString())))))
)
.Search<Facility>("FacilityWithoutReviews", s => s.Query(q =>
q.Ids(c => c.Values(facilityId)))));
所以我的FacilityWithoutReviews 已设置:
var facilityWithOutReviews = responses.GetResponse<Facility>("FacilityWithoutReviews");
这是我的 POCO:
[ElasticsearchType(Name="facility", IdProperty ="Id")]
public class Facility
{
public string Id { get; set; }
这是我获取设施
的 ElasticSearch 请求和响应Successful low level call on POST: /_msearch?typed_keys=true
# Audit trail of this API call:
- [1] HealthyResponse: Node: http://localhost:9200/ Took: 00:00:00.2014849
# Request:
{"index":"dev","type":"doc"}
{"query":{"nested":{"query":{"term":{"reviews.id":{"value":"00000000-0000-0000-0000-000000000000"}}},"path":"reviews"}}}
{"index":"dev","type":"doc"}
{"query":{"ids":{"values":["s5NjYWQBRsDWVSSXM-W0"]}}}
# Response:
{"responses":[{"took":27,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]},"status":200},{"took":23,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"dev","_type":"doc","_id":"s5NjYWQBRsDWVSSXM-W0","_score":1.0,"_source":{"name":"facility 3","types":[1],"status":1,"registrationDate":"0001-01-01T00:00:00","capacity":0,"rating":0.0,"licenseStatus":0,"licenseDate":"0001-01-01T00:00:00","licenseCloseDate":"0001-01-01T00:00:00","address":"7 thomas avenue*","zipCode":"12345","rentLow":0.0,"rentHigh":0.0,"basePriceLow":0.0,"basePriceHigh":0.0,"oneTimeFee":0.0,"levelOfCareRangeMinimum":0.0,"levelOfCareRangeMaximum":0.0,"city":"new york city","state":"New York","facilityManagements":[{"userId":"7c7f50b3-1d9d-48d0-8e86-352a84f0840b","operations":1,"isOwner":true,"isActive":true,"createdDate":"2018-07-03T18:24:07.9175473Z","id":"5af82e77-707d-49b8-be35-b570962557c0"}],"facilityRequests":[{"userId":"7c7f50b3-1d9d-48d0-8e86-352a84f0840b","createdDate":"2018-07-03T18:24:07.9175473Z","status":1,"id":"7538a645-0776-4799-83c4-c6a8459bf4c5"}],"slug":"facility-3-7-thomas-avenue","approved":false,"businessLocation":{"lat":40.7143548,"lon":-74.0059738},"suggest":{"input":["new york city","New York","facility","3"]}}}]},"status":200}]}
【问题讨论】:
-
哪个
id属性为空?review.Id?facilityId?Facility实例上的Id属性? -
@RussCam Facility 实例上的
Id属性。 -
好的。您在索引文档时是否为
Facility.Id设置了值?如果没有,则 Elasticsearch 中的_source中不会有Id。如果请求中没有 id,Elasticsearch 将在索引文档时为其生成一个 id。此 id 保存在元数据中,并作为文档命中的命中元数据中的_id字段返回(发送的原始文档作为_source字段返回)。 NEST 在反序列化时不会将元数据中的_id值分配给 POCO 的Id值。 -
NEST 所做的是查看您想要索引的 POCO,如果它包含
Id属性(或者您告诉它哪个属性被视为“id”属性),它会将这个属性的值作为 id 值发送到 Elasticsearch。在这种情况下,查询此文档时,在 hits 元数据中返回为_id的 id 值将与在_source文档上返回的id值相同。 NEST 中的此功能意味着您可以愉快地将文档建模为带有 Id 的 POCO -
@RussCam 了解。谢谢。
标签: elasticsearch nest