【发布时间】:2014-08-29 16:14:35
【问题描述】:
我正在尝试使用 NEST(.NET elasticsearch 包装器)查询以下实体:
public class TourIndex
{
public Guid ProductId { get; set; }
public Guid[] Periodes { get; set; }
public Guid[] Countries { get; set; }
...
}
我想获取其中一个国家为 guid x 的所有 TourIndex 对象。
我的索引是正确的,我可以在浏览到http://localhost:9200/my-index/_search?pretty=true&q=*:* 时看到我的 TourIndex 对象
我的查询尝试是:
System.Func<QueryDescriptor<TourIndex>, QueryContainer> elasticSearchQuery = (q => q.Term(x => x.Countries.First(), myGuidX));
var client = ElasticSearchHelper.CreateNestClient();
var searchResponse = client.Search<TourIndex>(s => s.Query(elasticSearchQuery));
return searchResponse.Documents;
很遗憾,我没有得到任何结果。回复有效,但没有返回任何文件。我已经仔细检查了我的索引中有一个有效的 TourIndex 有我的国家/地区指南。
一个索引对象的例子:
{
"_index" : "my-index",
"_type" : "tour",
"_id" : "WRAG141004",
"_score" : 1.0,
"_source":{
"erpTourId": "SNG141004",
"productId": "9d28694e-d705-e411-93f9-00155d01210a",
"countries": [
"01cad5a4-caf4-4d01-88e7-936c0827b13e"
],
"productType": "88e436ff-d605-e411-93f9-00155d01210a",
"subProductType": "ece1fa1b-d705-e411-93f9-00155d01210a",
"dayCount": 8,
"price": 7852,
"groupCount": 0,
"isPromotion": true,
"composition": []
}
例如,myGuidX 可以是 "01cad5a4-caf4-4d01-88e7-936c0827b13e"。
分析器/映射:
{
"live-to-travel-index": {
"mappings": {
"tour": {
"properties": {
"available": {
"type": "long"
},
"booked": {
"type": "long"
},
"capacity": {
"type": "long"
},
"composition": {
"type": "string"
},
"countries": {
"type": "string"
},
"dayCount": {
"type": "long"
},
"globusTourId": {
"type": "string"
},
"groupCount": {
"type": "long"
},
"isPromotion": {
"type": "boolean"
},
"options": {
"type": "long"
},
"periodes": {
"type": "string"
},
"price": {
"type": "long"
},
"productId": {
"type": "string"
},
"productType": {
"type": "string"
},
"queue": {
"type": "long"
},
"subProductType": {
"type": "string"
}
}
}
}
}
}
我没有在c#中配置任何东西,我认为这都是默认的。
更新
我找到了解决问题的方法,但我认为这是一种不好的做法。
这段代码给了我结果:
elasticSearchQuery = (q => q.Match(m => { m.Query("*" + query.CountryId.Value.ToString() + "*"); }));
var client = ElasticSearchHelper.CreateNestClient();
var searchResponse = client.Search<TourIndex>(s => s.Query(elasticSearchQuery));
我的猜测是通配符会降低性能。
【问题讨论】:
-
抱歉,我遇到了同样的问题,无法理解您的解决方案。 query.CountryId.Value.ToString() 中的这个查询是什么?
标签: c# elasticsearch nest