所以我已成功创建了“Package”对象的索引,并且直接的文本查询工作正常。
我想知道我是否可以使用部分填充的对象(“Package”类型)作为搜索条件?
包看起来像:
var packages = new List<Package> {
new Package {
Name = "Maverick",
TargetBusiness = new Business {
Industry = "Retail",
BusinessType = BusinessType.Product,
LocationType = LocationType.Store
},
Description = "Standard package for retail shops"
},
new Package {
Name = "Goose",
TargetBusiness = new Business {
Industry = "Retail",
BusinessType = BusinessType.Product,
LocationType = LocationType.Online
},
Description = "Standard package for ecommerce shops"
},
new Package {
Name = "Viper",
TargetBusiness = new Business {
Industry = "Advertising",
BusinessType = BusinessType.Service,
LocationType = LocationType.Office
},
Description = "Standard package test retail"
}
}
查询目前看起来像:
var result = client.Search<Package>(x => x.Query(q => q.QueryString(qs => qs.Query("q=retail"))));
但是我喜欢这样的东西:
var result = client.Search<Package>(x => x.Query(q => q.Object(new Package{...etc ...})));
我希望我有道理:D 提前致谢
答案 0 :(得分:2)
var result = client.Search<Package>(x => x.Query(q => q.Object(new Package{...etc ...})));
永远无法工作,因为Nest无法推断出对象上每个属性使用的查询类型(即term,prefix,wildcard,query_string等等)。
在您的示例中q=retail
仅有效,因为elasticsearch
会将查询分解为q OR retail
。您可以使用常规的lucene语法(targetBusiness.industry:retail
)来定位字段。
在elasticsearch中,如果您的查询字符串未绑定到某个字段,它将默认搜索_all
字段,该字段将包含对象所有属性的所有项。这就是为什么如果你真的有很多数据关闭{{1}}支持通常是个好主意。
Nest目前没有这样的功能,可以将部分填充的对象转换为elasticsearch _all
查询。
假设这是您的查询:
query_string
在上面的示例中,您必须创建自己的方法,根据您的包提供在查询字符串查询中搜索的所有术语。
即
client.Search<Package>(s=>s
.From(0)
.Size(10)
.Filter(f=>
f.Term(p=>p.TargetBusiness.Industry, "Advertising")
&& f.Exists(p=>p.Name)
)
.Query(q=>
q.QueryString(qs=>qs
.Query("ecommerce")
.Operator(Operator.and)
)
)
)
然后
public string MyPackageQueryString(Package package)
{
var myTerms = List<string>();
myTerms.Add(package.Name);
if (package.TargetBusiness != null)
{
myTerms.Add(package.Industry)
....
}
...
return string.Join(" ", myTerms.Where(t=>!string.IsNullOrWhiteSpace(t)));
}
答案 1 :(得分:0)
这确实帮助我走上了正确的轨道 - 最终看起来像是一种交叉搜索:用我不想要的其他所有领域搜索每个领域。
结束(可能不正确)继续:
return _searchClient.Search<Package>(s => s.Query(q =>
q.Term("industry", criteriaPackage.TargetBusiness.Industry.ToLower()) ||
q.Term("description", criteriaPackage.TargetBusiness.Description.ToLower()) ||
q.Term("businessType",((int)criteriaPackage.TargetBusiness.BusinessType).ToString()) ||
q.Term("locationType", ((int)criteriaPackage.TargetBusiness.LocationType).ToString()) ||
q.Term("marketSegment", criteriaPackage.TargetBusiness.MarketSegment.ToLower()) ||
q.Term("offer", criteriaPackage.TargetBusiness.Offer.ToLower()))
).Documents;
经过几次单元测试后,似乎产生了我想要的结果。